Skip to content

Commit 9bae514

Browse files
committed
- remove empty line after control structure
- add tests for new functions in UriController and UriManager - fix code style for the tests
1 parent de8a29a commit 9bae514

10 files changed

+174
-112
lines changed

src/Controller/UriController.php

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public function getUri(Request $request, UriManager $manager): RedirectResponse
3333
try {
3434
$uri = $manager->getGuaranteedUri(new GetUriRequest($shortCode));
3535
return $this->createRedirectResponseTo($uri->getOriginalUrl());
36-
3736
} catch (NonUniqueResultException $exception) {
3837
}
3938

tests/Functional/Controller/UriControllerTest.php

+27-29
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,23 @@ class UriControllerTest extends WebTestCase
1616
*/
1717
private $entityManager;
1818

19-
/**
20-
* {@inheritDoc}
21-
*/
22-
protected function setUp()
23-
{
24-
$kernel = self::bootKernel();
25-
26-
$this->initDatabase($kernel);
27-
28-
$this->entityManager = $kernel->getContainer()
29-
->get('doctrine')
30-
->getManager();
31-
}
32-
33-
34-
public function testIndexAction()
19+
public function testIndexAction(): void
3520
{
3621
$client = static::createClient();
3722
$client->request('GET', '/');
3823

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

42-
public function testGetUriActionWithInvalidShortCode()
27+
public function testGetUriActionWithInvalidShortCode(): void
4328
{
4429
$client = static::createClient();
4530
$client->request('GET', '/foobarbz');
4631

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

50-
public function testGetUriActionWithShortCode()
35+
public function testGetUriActionWithShortCode(): void
5136
{
5237
$url = 'https://www.example.com';
5338

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

77-
public function testPutUriAction()
62+
public function testPutUriAction(): void
7863
{
7964
$url = 'https://www.example.com';
8065

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

94-
public function testPutUriActionWithNoCredentials()
79+
public function testPutUriActionWithNoCredentials(): void
9580
{
9681
$url = 'https://www.example.com';
9782

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

111-
public function testPutUriActionWithNoVaildUrl()
96+
public function testPutUriActionWithNoVaildUrl(): void
11297
{
113-
$url = 'this is no a url';
98+
$url = 'this is not a url';
11499

115100
$client = static::createClient();
116101
$client->request(
@@ -128,19 +113,23 @@ public function testPutUriActionWithNoVaildUrl()
128113
/**
129114
* {@inheritDoc}
130115
*/
131-
protected function tearDown()
116+
protected function setUp()
132117
{
133-
parent::tearDown();
118+
$kernel = self::bootKernel();
134119

135-
$this->entityManager->close();
136-
$this->entityManager = null; // avoid memory leaks
120+
$this->initDatabase($kernel);
121+
122+
$this->entityManager = $kernel->getContainer()
123+
->get('doctrine')
124+
->getManager();
137125
}
138126

139127
/**
140128
* @param KernelInterface $kernel
129+
*
141130
* @throws \Exception
142131
*/
143-
private function initDatabase(KernelInterface $kernel)
132+
private function initDatabase(KernelInterface $kernel): void
144133
{
145134
$application = new Application($kernel);
146135
$application->setAutoExit(false);
@@ -162,13 +151,22 @@ private function initDatabase(KernelInterface $kernel)
162151

163152
// run migrations
164153
$input = new ArrayInput([
165-
'command' => 'doctrine:migrations:migrate',
154+
'command' => 'doctrine:migrations:migrate',
166155
'--no-interaction' => true
167156

168157
]);
169158
$application->run($input, new NullOutput());
170-
171159
}
172160

161+
/**
162+
* {@inheritDoc}
163+
*/
164+
protected function tearDown()
165+
{
166+
parent::tearDown();
167+
168+
$this->entityManager->close();
169+
$this->entityManager = null; // avoid memory leaks
170+
}
173171
}
174172

tests/Functional/Repository/UriRepositoryTest.php

+38-38
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,7 @@ class UriRepositoryTest extends WebTestCase
1818
*/
1919
private $entityManager;
2020

21-
/**
22-
* {@inheritDoc}
23-
*/
24-
protected function setUp()
25-
{
26-
$kernel = self::bootKernel();
27-
28-
$this->initDatabase($kernel);
29-
30-
$this->entityManager = $kernel->getContainer()
31-
->get('doctrine')
32-
->getManager();
33-
}
34-
35-
public function testRepositoryCanBeInstantiated()
21+
public function testRepositoryCanBeInstantiated(): void
3622
{
3723
$repository = $this->entityManager->getRepository(Uri::class);
3824
$this->assertInstanceOf(UriRepository::class, $repository);
@@ -43,7 +29,7 @@ public function testRepositoryCanBeInstantiated()
4329
* @throws \Doctrine\ORM\ORMException
4430
* @throws \Doctrine\ORM\OptimisticLockException
4531
*/
46-
public function testNewEntityCanBeSavedThroughRepository()
32+
public function testNewEntityCanBeSavedThroughRepository(): void
4733
{
4834
/** @var UriRepository $repository */
4935
$repository = $this->entityManager->getRepository(Uri::class);
@@ -69,7 +55,7 @@ public function testNewEntityCanBeSavedThroughRepository()
6955
* @throws \Doctrine\ORM\ORMException
7056
* @throws \Doctrine\ORM\OptimisticLockException
7157
*/
72-
public function testUriCanBeFoundByShortCode()
58+
public function testUriCanBeFoundByShortCode(): void
7359
{
7460
$this->createDemoRecord();
7561

@@ -85,12 +71,31 @@ public function testUriCanBeFoundByShortCode()
8571
);
8672
}
8773

74+
/**
75+
* @throws \Doctrine\ORM\ORMException
76+
* @throws \Doctrine\ORM\OptimisticLockException
77+
*/
78+
private function createDemoRecord(): void
79+
{
80+
/** @var UriRepository $repository */
81+
$repository = $this->entityManager->getRepository(Uri::class);
82+
83+
$putRequest = new PutUriRequest('www.bar.com');
84+
85+
$entity = new Uri();
86+
$entity->setOriginalUrl($putRequest->getUrl());
87+
$entity->setUrlHash($putRequest->getUrlHash());
88+
$entity->setShortCode($putRequest->getShortCode());
89+
90+
$repository->saveUri($entity);
91+
}
92+
8893
/**
8994
* @throws \Doctrine\ORM\NonUniqueResultException
9095
* @throws \Doctrine\ORM\ORMException
9196
* @throws \Doctrine\ORM\OptimisticLockException
9297
*/
93-
public function testUriCanBeFoundByUrlHash()
98+
public function testUriCanBeFoundByUrlHash(): void
9499
{
95100
$this->createDemoRecord();
96101

@@ -109,19 +114,23 @@ public function testUriCanBeFoundByUrlHash()
109114
/**
110115
* {@inheritDoc}
111116
*/
112-
protected function tearDown()
117+
protected function setUp()
113118
{
114-
parent::tearDown();
119+
$kernel = self::bootKernel();
115120

116-
$this->entityManager->close();
117-
$this->entityManager = null; // avoid memory leaks
121+
$this->initDatabase($kernel);
122+
123+
$this->entityManager = $kernel->getContainer()
124+
->get('doctrine')
125+
->getManager();
118126
}
119127

120128
/**
121129
* @param KernelInterface $kernel
130+
*
122131
* @throws \Exception
123132
*/
124-
private function initDatabase(KernelInterface $kernel)
133+
private function initDatabase(KernelInterface $kernel): void
125134
{
126135
$application = new Application($kernel);
127136
$application->setAutoExit(false);
@@ -143,30 +152,21 @@ private function initDatabase(KernelInterface $kernel)
143152

144153
// run migrations
145154
$input = new ArrayInput([
146-
'command' => 'doctrine:migrations:migrate',
155+
'command' => 'doctrine:migrations:migrate',
147156
'--no-interaction' => true
148157

149158
]);
150159
$application->run($input, new NullOutput());
151-
152160
}
153161

154162
/**
155-
* @throws \Doctrine\ORM\ORMException
156-
* @throws \Doctrine\ORM\OptimisticLockException
163+
* {@inheritDoc}
157164
*/
158-
private function createDemoRecord()
165+
protected function tearDown()
159166
{
160-
/** @var UriRepository $repository */
161-
$repository = $this->entityManager->getRepository(Uri::class);
162-
163-
$putRequest = new PutUriRequest('www.bar.com');
164-
165-
$entity = new Uri();
166-
$entity->setOriginalUrl($putRequest->getUrl());
167-
$entity->setUrlHash($putRequest->getUrlHash());
168-
$entity->setShortCode($putRequest->getShortCode());
167+
parent::tearDown();
169168

170-
$repository->saveUri($entity);
169+
$this->entityManager->close();
170+
$this->entityManager = null; // avoid memory leaks
171171
}
172172
}

0 commit comments

Comments
 (0)