Skip to content

Commit

Permalink
Add ProjectionsManager::reset method
Browse files Browse the repository at this point in the history
resolves #41
  • Loading branch information
prolic committed Nov 25, 2018
1 parent 15b5f4e commit 4bcb9f8
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
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 4bcb9f8

Please sign in to comment.