Skip to content

Commit

Permalink
- remove empty line after control structure
Browse files Browse the repository at this point in the history
- add tests for new functions in UriController and UriManager
- fix code style for the tests
  • Loading branch information
patrick-blom committed Aug 31, 2019
1 parent de8a29a commit 9bae514
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 112 deletions.
1 change: 0 additions & 1 deletion src/Controller/UriController.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public function getUri(Request $request, UriManager $manager): RedirectResponse
try {
$uri = $manager->getGuaranteedUri(new GetUriRequest($shortCode));
return $this->createRedirectResponseTo($uri->getOriginalUrl());

} catch (NonUniqueResultException $exception) {
}

Expand Down
56 changes: 27 additions & 29 deletions tests/Functional/Controller/UriControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,23 @@ class UriControllerTest extends WebTestCase
*/
private $entityManager;

/**
* {@inheritDoc}
*/
protected function setUp()
{
$kernel = self::bootKernel();

$this->initDatabase($kernel);

$this->entityManager = $kernel->getContainer()
->get('doctrine')
->getManager();
}


public function testIndexAction()
public function testIndexAction(): void
{
$client = static::createClient();
$client->request('GET', '/');

$this->assertEquals('418', $client->getResponse()->getStatusCode());
}

public function testGetUriActionWithInvalidShortCode()
public function testGetUriActionWithInvalidShortCode(): void
{
$client = static::createClient();
$client->request('GET', '/foobarbz');

$this->assertEquals('301', $client->getResponse()->getStatusCode());
}

public function testGetUriActionWithShortCode()
public function testGetUriActionWithShortCode(): void
{
$url = 'https://www.example.com';

Expand All @@ -74,7 +59,7 @@ public function testGetUriActionWithShortCode()
$this->assertEquals($url, $response->getTargetUrl());
}

public function testPutUriAction()
public function testPutUriAction(): void
{
$url = 'https://www.example.com';

Expand All @@ -91,7 +76,7 @@ public function testPutUriAction()
$this->assertEquals(201, $client->getResponse()->getStatusCode());
}

public function testPutUriActionWithNoCredentials()
public function testPutUriActionWithNoCredentials(): void
{
$url = 'https://www.example.com';

Expand All @@ -108,9 +93,9 @@ public function testPutUriActionWithNoCredentials()
$this->assertEquals(400, $client->getResponse()->getStatusCode());
}

public function testPutUriActionWithNoVaildUrl()
public function testPutUriActionWithNoVaildUrl(): void
{
$url = 'this is no a url';
$url = 'this is not a url';

$client = static::createClient();
$client->request(
Expand All @@ -128,19 +113,23 @@ public function testPutUriActionWithNoVaildUrl()
/**
* {@inheritDoc}
*/
protected function tearDown()
protected function setUp()
{
parent::tearDown();
$kernel = self::bootKernel();

$this->entityManager->close();
$this->entityManager = null; // avoid memory leaks
$this->initDatabase($kernel);

$this->entityManager = $kernel->getContainer()
->get('doctrine')
->getManager();
}

/**
* @param KernelInterface $kernel
*
* @throws \Exception
*/
private function initDatabase(KernelInterface $kernel)
private function initDatabase(KernelInterface $kernel): void
{
$application = new Application($kernel);
$application->setAutoExit(false);
Expand All @@ -162,13 +151,22 @@ private function initDatabase(KernelInterface $kernel)

// run migrations
$input = new ArrayInput([
'command' => 'doctrine:migrations:migrate',
'command' => 'doctrine:migrations:migrate',
'--no-interaction' => true

]);
$application->run($input, new NullOutput());

}

/**
* {@inheritDoc}
*/
protected function tearDown()
{
parent::tearDown();

$this->entityManager->close();
$this->entityManager = null; // avoid memory leaks
}
}

76 changes: 38 additions & 38 deletions tests/Functional/Repository/UriRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,7 @@ class UriRepositoryTest extends WebTestCase
*/
private $entityManager;

/**
* {@inheritDoc}
*/
protected function setUp()
{
$kernel = self::bootKernel();

$this->initDatabase($kernel);

$this->entityManager = $kernel->getContainer()
->get('doctrine')
->getManager();
}

public function testRepositoryCanBeInstantiated()
public function testRepositoryCanBeInstantiated(): void
{
$repository = $this->entityManager->getRepository(Uri::class);
$this->assertInstanceOf(UriRepository::class, $repository);
Expand All @@ -43,7 +29,7 @@ public function testRepositoryCanBeInstantiated()
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function testNewEntityCanBeSavedThroughRepository()
public function testNewEntityCanBeSavedThroughRepository(): void
{
/** @var UriRepository $repository */
$repository = $this->entityManager->getRepository(Uri::class);
Expand All @@ -69,7 +55,7 @@ public function testNewEntityCanBeSavedThroughRepository()
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function testUriCanBeFoundByShortCode()
public function testUriCanBeFoundByShortCode(): void
{
$this->createDemoRecord();

Expand All @@ -85,12 +71,31 @@ public function testUriCanBeFoundByShortCode()
);
}

/**
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
private function createDemoRecord(): void
{
/** @var UriRepository $repository */
$repository = $this->entityManager->getRepository(Uri::class);

$putRequest = new PutUriRequest('www.bar.com');

$entity = new Uri();
$entity->setOriginalUrl($putRequest->getUrl());
$entity->setUrlHash($putRequest->getUrlHash());
$entity->setShortCode($putRequest->getShortCode());

$repository->saveUri($entity);
}

/**
* @throws \Doctrine\ORM\NonUniqueResultException
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
*/
public function testUriCanBeFoundByUrlHash()
public function testUriCanBeFoundByUrlHash(): void
{
$this->createDemoRecord();

Expand All @@ -109,19 +114,23 @@ public function testUriCanBeFoundByUrlHash()
/**
* {@inheritDoc}
*/
protected function tearDown()
protected function setUp()
{
parent::tearDown();
$kernel = self::bootKernel();

$this->entityManager->close();
$this->entityManager = null; // avoid memory leaks
$this->initDatabase($kernel);

$this->entityManager = $kernel->getContainer()
->get('doctrine')
->getManager();
}

/**
* @param KernelInterface $kernel
*
* @throws \Exception
*/
private function initDatabase(KernelInterface $kernel)
private function initDatabase(KernelInterface $kernel): void
{
$application = new Application($kernel);
$application->setAutoExit(false);
Expand All @@ -143,30 +152,21 @@ private function initDatabase(KernelInterface $kernel)

// run migrations
$input = new ArrayInput([
'command' => 'doctrine:migrations:migrate',
'command' => 'doctrine:migrations:migrate',
'--no-interaction' => true

]);
$application->run($input, new NullOutput());

}

/**
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
* {@inheritDoc}
*/
private function createDemoRecord()
protected function tearDown()
{
/** @var UriRepository $repository */
$repository = $this->entityManager->getRepository(Uri::class);

$putRequest = new PutUriRequest('www.bar.com');

$entity = new Uri();
$entity->setOriginalUrl($putRequest->getUrl());
$entity->setUrlHash($putRequest->getUrlHash());
$entity->setShortCode($putRequest->getShortCode());
parent::tearDown();

$repository->saveUri($entity);
$this->entityManager->close();
$this->entityManager = null; // avoid memory leaks
}
}
Loading

0 comments on commit 9bae514

Please sign in to comment.