Skip to content

Commit

Permalink
Merge pull request #42 from prooph/reset_pj
Browse files Browse the repository at this point in the history
Add ProjectionsManager::reset method
  • Loading branch information
prolic authored Nov 25, 2018
2 parents 15b5f4e + bf38db9 commit 3736f15
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
"require-dev": {
"phpspec/prophecy": "^1.7",
"phpunit/phpunit": "^7.4",
"doctrine/instantiator": "^1.1",
"sebastian/object-enumerator": "^3.0.3",
"prooph/php-cs-fixer-config": "^0.3",
"satooshi/php-coveralls": "^1.0"
},
Expand Down
19 changes: 19 additions & 0 deletions src/Projections/ProjectionsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,25 @@ public function updateQuery(
);
}

public function reset(
EndPoint $endPoint,
string $name,
?UserCredentials $userCredentials = null,
string $httpSchema = EndpointExtensions::HTTP_SCHEMA
): Promise {
return $this->sendPost(
EndpointExtensions::formatStringToHttpUrl(
$endPoint,
$httpSchema,
'/projection/%s/command/reset',
$name
),
'',
$userCredentials,
HttpStatusCode::OK
);
}

public function delete(
EndPoint $endPoint,
string $name,
Expand Down
17 changes: 17 additions & 0 deletions src/Projections/ProjectionsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,4 +418,21 @@ public function deleteAsync(
$this->httpSchema
);
}

/**
* Asynchronously resets a projection
*/
public function resetAsync(string $name, ?UserCredentials $userCredentials = null): Promise
{
if ('' === $name) {
throw new InvalidArgumentException('Name is required');
}

return $this->client->reset(
$this->httpEndPoint,
$name,
$userCredentials,
$this->httpSchema
);
}
}
93 changes: 93 additions & 0 deletions tests/when_resetting_projections.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

/**
* This file is part of `prooph/event-store-client`.
* (c) 2018-2018 prooph software GmbH <contact@prooph.de>
* (c) 2018-2018 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ProophTest\EventStoreClient;

use Amp\Delayed;
use Generator;
use PHPUnit\Framework\TestCase;
use Prooph\EventStoreClient\Util\Guid;
use Throwable;

class when_resetting_projections extends TestCase
{
use ProjectionSpecification;

/** @var string */
private $projectionName;
/** @var string */
private $streamName;
/** @var string */
private $query;

public function given(): Generator
{
$id = Guid::generateAsHex();
$this->projectionName = 'when_resetting_projections-' . $id;
$this->streamName = 'test-stream-' . $id;

yield $this->postEvent($this->streamName, 'testEvent', '{"A": 1}');
yield $this->postEvent($this->streamName, 'testEvent', '{"A": 2}');

$this->query = $this->createStandardQuery($this->streamName);

yield $this->projectionsManager->createContinuousAsync(
$this->projectionName,
$this->query,
false,
'JS',
$this->credentials
);
}

protected function when(): Generator
{
yield $this->projectionsManager->resetAsync(
$this->projectionName,
$this->credentials
);
}

/**
* @test
* @throws Throwable
*/
public function should_reset_the_projection(): void
{
$this->execute(function () {
$projectionStatus = \json_decode(
yield $this->projectionsManager->getStatusAsync(
$this->projectionName,
$this->credentials
),
true
);
$status = $projectionStatus['status'];

$this->assertStringStartsWith('Preparing', $status);

yield new Delayed(500);

$projectionStatus = \json_decode(
yield $this->projectionsManager->getStatusAsync(
$this->projectionName,
$this->credentials
),
true
);
$status = $projectionStatus['status'];

$this->assertSame('Running', $status);
});
}
}

0 comments on commit 3736f15

Please sign in to comment.