Skip to content

Commit

Permalink
Merge pull request #15 from michaelpetri/fix-missing-reset-after-fail…
Browse files Browse the repository at this point in the history
…ed-ack

fixed handling for failed ack calls
  • Loading branch information
michaelpetri committed Apr 19, 2023
2 parents b0f5463 + 58ebb2f commit a3b776c
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 13 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"symfony/process": "^6.2.7",
"symfony/dependency-injection": "^6.2.7",
"symfony/framework-bundle": "^6.2.7",
"michaelpetri/php-git": "^0.3.1"
"michaelpetri/php-git": "^0.4.1"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.15.1",
Expand Down
16 changes: 8 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions src/Infrastructure/Transport/EventReceiver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MichaelPetri\SymfonyFileWatcher\Infrastructure\Transport;

use MichaelPetri\Git\Exception\FileNotCommitted;
use MichaelPetri\Git\GitRepositoryInterface;
use MichaelPetri\Git\Value\File;
use MichaelPetri\Git\Value\Status;
Expand Down Expand Up @@ -93,10 +94,16 @@ public function ack(Envelope $envelope): void
{
$stamp = self::getStampFromEnvelope(FilenameStamp::class, $envelope);

$this->repository->commit(
'Successfully processed file',
File::from($stamp->filename)
);
$file = File::from($stamp->filename);

try {
$this->repository->commit(
'Successfully processed file',
$file
);
} catch (FileNotCommitted) {
$this->repository->reset($file);
}
}

public function reject(Envelope $envelope): void
Expand Down
62 changes: 62 additions & 0 deletions tests/Unit/Infrastructure/Transport/EventReceiverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace Tests\MichaelPetri\SymfonyFileWatcher\Unit\Infrastructure\Transport;

use MichaelPetri\Git\Exception\FileNotCommitted;
use MichaelPetri\Git\GitRepositoryInterface;
use MichaelPetri\Git\Value\File;
use MichaelPetri\SymfonyFileWatcher\Domain\Event\FileCreated;
use MichaelPetri\SymfonyFileWatcher\Infrastructure\Transport\EventReceiver;
use MichaelPetri\SymfonyFileWatcher\Infrastructure\Transport\Stamp\FilenameStamp;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Envelope;

final class EventReceiverTest extends TestCase
{
private GitRepositoryInterface&MockObject $repository;
private EventReceiver $receiver;

protected function setUp(): void
{
$this->repository = $this->createMock(GitRepositoryInterface::class);

$this->receiver = new EventReceiver(
$this->repository
);
}

public function testAckResetsFileOnFailure(): void
{
$file = File::from('bar.baz');

$this->repository
->expects(self::once())
->method('commit')
->with(
'Successfully processed file',
$file
)
->willThrowException(
FileNotCommitted::fromDirectoryAndFiles(
$file->directory,
[$file]
)
);

$this->repository
->expects(self::once())
->method('reset')
->with($file);

$this->receiver->ack(
Envelope::wrap(
new FileCreated($file->name)
)->with(
new FilenameStamp($file->name)
)
);
}
}

0 comments on commit a3b776c

Please sign in to comment.