Skip to content

Commit

Permalink
add new exception and more fixes for mutation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-blom committed Nov 16, 2019
1 parent 500b59c commit 5c664d1
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
6 changes: 5 additions & 1 deletion infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"source": {
"directories": [
"src"
],
"excludes": [
"Migrations",
"Kernel.php"
]
},
"logs": {
Expand All @@ -11,4 +15,4 @@
"mutators": {
"@default": true
}
}
}
12 changes: 12 additions & 0 deletions src/Exception/UriCouldNotBeSavedByUriManagerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace App\Exception;

use Exception as OriginException;

final class UriCouldNotBeSavedByUriManagerException extends OriginException
{

}
8 changes: 7 additions & 1 deletion src/Service/UriManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Service;

use App\Entity\Uri;
use App\Exception\UriCouldNotBeSavedByUriManagerException;
use App\Repository\UriRepository;
use App\Struct\DeleteUriRequest;
use App\Struct\GetUriRequest;
Expand Down Expand Up @@ -68,6 +69,7 @@ public function getUri(UriHashInterface $request): ?Uri
* @throws \Doctrine\ORM\NonUniqueResultException
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
* @throws UriCouldNotBeSavedByUriManagerException
*/
public function putUri(PutUriRequest $request): ?Uri
{
Expand All @@ -83,7 +85,11 @@ public function putUri(PutUriRequest $request): ?Uri
$uri->setUrlHash($request->getHash());
$uri->setShortCode($request->getShortCode());

$this->uriRepository->saveUri($uri);
if (false === $this->uriRepository->saveUri($uri)) {
throw new UriCouldNotBeSavedByUriManagerException(
'The putUri-Request for: ' . $uri->getOriginalUrl() . ' could not be saved'
);
}
}

return $uri;
Expand Down
52 changes: 51 additions & 1 deletion tests/Unit/Service/UriManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace App\Tests\Unit\Service;

use App\Entity\Uri;
use App\Exception\UriCouldNotBeSavedByUriManagerException;
use App\Repository\UriRepository;
use App\Service\UriManager;
use App\Struct\GetUriRequest;
use App\Struct\PutUriRequest;
use PHPUnit\Framework\TestCase;


Expand All @@ -16,7 +18,21 @@ class UriManagerTest extends TestCase
*/
private $uriRepository;

public function testUriManagerWillReturnGuaranteedUri(): void
public function testUriManagerWillReturnDefaultUriByPassingRubbish(): void
{
$uriManager = new UriManager(
$this->createMock(UriRepository::class)
);
$this->assertInstanceOf(UriManager::class, $uriManager);

$uri = $uriManager->getGuaranteedUri(
new GetUriRequest('00000000')
);

$this->assertSame('/', $uri->getOriginalUrl());
}

public function testUriManagerWillReturnCorrectUriByPassingShortCode(): void
{
$uriManager = new UriManager($this->uriRepository);
$this->assertInstanceOf(UriManager::class, $uriManager);
Expand All @@ -28,6 +44,35 @@ public function testUriManagerWillReturnGuaranteedUri(): void
$this->assertSame('http://www.blabla.com', $uri->getOriginalUrl());
}

public function testUriManagerCanHandlePutRequestAndReturnsExpectedUriObejct(): void
{
$uriManager = new UriManager($this->uriRepository);
$this->assertInstanceOf(UriManager::class, $uriManager);

$uri = $uriManager->putUri(
new PutUriRequest('http://www.blabla.com')
);

$this->assertSame('http://www.blabla.com', $uri->getOriginalUrl());
$this->assertSame('a078b1f4', $uri->getShortCode());
$this->assertSame('a078b1f4bc1ec3defc681e5f3fc89c7b71a65369', $uri->getUrlHash());
}

public function testUriManagerWillThrowAnExceptionIfUriCanNotBeSaved(): void
{
/** @var UriRepository $brokenUriRepository */
$brokenUriRepository = $this->createMock(UriRepository::class);
$uriManager = new UriManager($brokenUriRepository);

$this->assertInstanceOf(UriManager::class, $uriManager);
$this->expectException(UriCouldNotBeSavedByUriManagerException::class);
$this->expectExceptionMessage('The putUri-Request for: http://www.blabla.com could not be saved');

$uriManager->putUri(
new PutUriRequest('http://www.blabla.com')
);
}

/**
* {@inheritDoc}
*/
Expand All @@ -44,5 +89,10 @@ protected function setUp()
->willReturn(
$uri
);

$this->uriRepository->method('saveUri')
->willReturn(
true
);
}
}

0 comments on commit 5c664d1

Please sign in to comment.