Skip to content

Commit ff54de7

Browse files
committed
Merge pull request #44 from FriendsOfSymfony/abstract-test-case
Move more functionality to the abstract test case
2 parents 8753bc6 + b9444d8 commit ff54de7

File tree

4 files changed

+80
-46
lines changed

4 files changed

+80
-46
lines changed

tests/AbstractCacheProxyTestCase.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ public function getClient()
7878
return self::$client;
7979
}
8080

81+
/**
82+
* Clear the cache between each test
83+
*/
84+
protected function setUp()
85+
{
86+
$this->clearCache();
87+
}
88+
8189
/**
8290
* Get the hostname where your application can be reached
8391
*
@@ -94,10 +102,43 @@ protected function getHostName()
94102
return WEB_SERVER_HOSTNAME;
95103
}
96104

105+
/**
106+
* Wait for caching proxy to be started up and reachable
107+
*
108+
* @param string $ip
109+
* @param int $port
110+
* @param int $timeout Timeout in milliseconds
111+
*
112+
* @throws \RuntimeException If proxy is not reachable within timeout
113+
*/
114+
protected function waitFor($ip, $port, $timeout)
115+
{
116+
for ($i = 0; $i < $timeout; $i++) {
117+
if (@fsockopen($ip, $port)) {
118+
return;
119+
}
120+
121+
usleep(1000);
122+
}
123+
124+
throw new \RuntimeException(
125+
sprintf(
126+
'Caching proxy cannot be reached at %s:%s',
127+
$ip,
128+
$port
129+
)
130+
);
131+
}
132+
97133
/**
98134
* Get port at which the caching proxy is running
99135
*
100136
* @return int
101137
*/
102138
abstract protected function getCachingProxyPort();
139+
140+
/**
141+
* Clear the cache
142+
*/
143+
abstract protected function clearCache();
103144
}

tests/Functional/CacheInvalidatorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class CacheInvalidatorTest extends VarnishTestCase
1212
{
1313
public function testInvalidateTags()
1414
{
15-
$cacheInvalidator = new CacheInvalidator($this->varnish);
15+
$cacheInvalidator = new CacheInvalidator($this->getVarnish());
1616

1717
$this->assertMiss($this->getResponse('/tags.php'));
1818
$this->assertHit($this->getResponse('/tags.php'));

tests/Functional/VarnishTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function testBanAll()
1818
$this->assertMiss($this->getResponse('/json.php'));
1919
$this->assertHit($this->getResponse('/json.php'));
2020

21-
$this->varnish->ban(array(Varnish::HTTP_HEADER_URL => '.*'))->flush();
21+
$this->getVarnish()->ban(array(Varnish::HTTP_HEADER_URL => '.*'))->flush();
2222

2323
$this->assertMiss($this->getResponse('/cache.php'));
2424
$this->assertMiss($this->getResponse('/json.php'));
@@ -29,10 +29,10 @@ public function testBanHost()
2929
$this->assertMiss($this->getResponse('/cache.php'));
3030
$this->assertHit($this->getResponse('/cache.php'));
3131

32-
$this->varnish->ban(array(Varnish::HTTP_HEADER_HOST => 'wrong-host.lo'))->flush();
32+
$this->getVarnish()->ban(array(Varnish::HTTP_HEADER_HOST => 'wrong-host.lo'))->flush();
3333
$this->assertHit($this->getResponse('/cache.php'));
3434

35-
$this->varnish->ban(array(Varnish::HTTP_HEADER_HOST => $this->getHostname()))->flush();
35+
$this->getVarnish()->ban(array(Varnish::HTTP_HEADER_HOST => $this->getHostname()))->flush();
3636
$this->assertMiss($this->getResponse('/cache.php'));
3737
}
3838

@@ -44,7 +44,7 @@ public function testBanPathAll()
4444
$this->assertMiss($this->getResponse('/json.php'));
4545
$this->assertHit($this->getResponse('/json.php'));
4646

47-
$this->varnish->banPath('.*')->flush();
47+
$this->getVarnish()->banPath('.*')->flush();
4848
$this->assertMiss($this->getResponse('/cache.php'));
4949
$this->assertMiss($this->getResponse('/json.php'));
5050
}
@@ -57,7 +57,7 @@ public function testBanPathContentType()
5757
$this->assertMiss($this->getResponse('/json.php'));
5858
$this->assertHit($this->getResponse('/json.php'));
5959

60-
$this->varnish->banPath('.*', 'text/html')->flush();
60+
$this->getVarnish()->banPath('.*', 'text/html')->flush();
6161
$this->assertMiss($this->getResponse('/cache.php'));
6262
$this->assertHit($this->getResponse('/json.php'));
6363
}
@@ -67,7 +67,7 @@ public function testPurge()
6767
$this->assertMiss($this->getResponse('/cache.php'));
6868
$this->assertHit($this->getResponse('/cache.php'));
6969

70-
$this->varnish->purge('/cache.php')->flush();
70+
$this->getVarnish()->purge('/cache.php')->flush();
7171
$this->assertMiss($this->getResponse('/cache.php'));
7272
}
7373

@@ -87,7 +87,7 @@ public function testPurgeContentType()
8787
$this->assertHit($this->getResponse('/negotation.php', $html));
8888

8989
self::getResponse('/negotation.php');
90-
$this->varnish->purge('/negotation.php')->flush();
90+
$this->getVarnish()->purge('/negotation.php')->flush();
9191
$this->assertMiss($this->getResponse('/negotation.php', $json));
9292
$this->assertMiss($this->getResponse('/negotation.php', $html));
9393
}
@@ -108,7 +108,7 @@ public function testRefresh()
108108
$response = $this->getResponse('/cache.php');
109109
$this->assertHit($response);
110110

111-
$this->varnish->refresh('/cache.php')->flush();
111+
$this->getVarnish()->refresh('/cache.php')->flush();
112112
usleep(1000);
113113
$refreshed = $this->getResponse('/cache.php');
114114
$this->assertGreaterThan((float) $response->getBody(true), (float) $refreshed->getBody(true));
@@ -119,7 +119,7 @@ public function testRefreshContentType()
119119
$json = array('Accept' => 'application/json');
120120
$html = array('Accept' => 'text/html');
121121

122-
$this->varnish->refresh('/negotation.php', $json)->flush();
122+
$this->getVarnish()->refresh('/negotation.php', $json)->flush();
123123

124124
$this->assertHit($this->getResponse('/negotation.php', $json));
125125
$this->assertMiss($this->getResponse('/negotation.php', $html));

tests/VarnishTestCase.php

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,27 @@ protected function getCacheDir()
113113
/**
114114
* {@inheritdoc}
115115
*/
116-
protected function setUp()
116+
protected function tearDown()
117117
{
118-
$this->varnish = new Varnish(
119-
array('http://127.0.0.1:' . $this->getCachingProxyPort()),
120-
$this->getHostName() . ':' . $this->getCachingProxyPort()
121-
);
122-
123118
$this->stopVarnish();
119+
}
120+
121+
/**
122+
* Stop Varnish process if it's running
123+
*/
124+
protected function stopVarnish()
125+
{
126+
if (file_exists(self::PID)) {
127+
exec('kill ' . file_get_contents(self::PID));
128+
unlink(self::PID);
129+
}
130+
}
124131

132+
/**
133+
* Start Varnish process if it's not yet running
134+
*/
135+
protected function startVarnish()
136+
{
125137
exec($this->getBinary() .
126138
' -a localhost:' . $this->getCachingProxyPort() .
127139
' -T localhost:' . $this->getVarnishMgmtPort() .
@@ -131,47 +143,28 @@ protected function setUp()
131143
' -P ' . self::PID
132144
);
133145

134-
$this->waitForVarnish('127.0.0.1', $this->getCachingProxyPort(), 2000);
146+
$this->waitFor('127.0.0.1', $this->getCachingProxyPort(), 2000);
135147
}
136148

137149
/**
138150
* {@inheritdoc}
139151
*/
140-
protected function tearDown()
152+
protected function clearCache()
141153
{
154+
// Clear Varnish cache by restarting
142155
$this->stopVarnish();
156+
$this->startVarnish();
143157
}
144158

145-
/**
146-
* Wait for Varnish proxy to be started up and reachable
147-
*
148-
* @param string $ip
149-
* @param int $port
150-
* @param int $timeout Timeout in milliseconds
151-
*
152-
* @throws \RuntimeException If Varnish is not reachable within timeout
153-
*/
154-
protected function waitForVarnish($ip, $port, $timeout)
159+
protected function getVarnish()
155160
{
156-
for ($i = 0; $i < $timeout; $i++) {
157-
if (@fsockopen($ip, $port)) {
158-
return;
159-
}
160-
161-
usleep(1000);
161+
if (null === $this->varnish) {
162+
$this->varnish = new Varnish(
163+
array('http://127.0.0.1:' . $this->getCachingProxyPort()),
164+
$this->getHostName() . ':' . $this->getCachingProxyPort()
165+
);
162166
}
163167

164-
throw new \RuntimeException(sprintf('Varnish proxy cannot be reached at %s:%s', '127.0.0.1', $this->getCachingProxyPort()));
165-
}
166-
167-
/**
168-
* Stop Varnish process if it's running
169-
*/
170-
protected function stopVarnish()
171-
{
172-
if (file_exists(self::PID)) {
173-
exec('kill ' . file_get_contents(self::PID));
174-
unlink(self::PID);
175-
}
168+
return $this->varnish;
176169
}
177170
}

0 commit comments

Comments
 (0)