Skip to content

Commit c73e722

Browse files
authored
Merge pull request #320 from FriendsOfSymfony/remove-test-duplication
extract shared assertions into traits
2 parents c27c56f + 15a1fc2 commit c73e722

File tree

7 files changed

+275
-183
lines changed

7 files changed

+275
-183
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSHttpCache package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\HttpCache\Tests\Functional\ProxyClient;
13+
14+
use FOS\HttpCache\ProxyClient\Invalidation\BanInterface;
15+
16+
/**
17+
* Assertions that do the ban operations.
18+
*/
19+
trait BanAssertions
20+
{
21+
/**
22+
* Asserting that banning everything leads to all content getting invalidated.
23+
*
24+
* @param BanInterface $proxyClient The client to send ban instructions to the cache
25+
* @param string $header The header that holds the URLs
26+
* @param array $paths The paths to get, defaults to [/cache.php, json.php]
27+
*/
28+
protected function assertBanAll(BanInterface $proxyClient, $header, array $paths = ['/cache.php', '/json.php'])
29+
{
30+
foreach ($paths as $path) {
31+
$this->assertMiss($this->getResponse($path));
32+
$this->assertHit($this->getResponse($path));
33+
}
34+
35+
$proxyClient->ban([$header => '.*'])->flush();
36+
37+
foreach ($paths as $path) {
38+
$this->assertMiss($this->getResponse($path));
39+
}
40+
}
41+
42+
/**
43+
* Asserting that only banning the right host leads to content getting invalidated.
44+
*
45+
* @param BanInterface $proxyClient The client to send ban instructions to the cache
46+
* @param string $header The header that holds the URLs
47+
* @param string $hostname Name of the host so we can invalidate that host
48+
* @param string $path The path to get, defaults to[/cache.php
49+
*/
50+
protected function assertBanHost(BanInterface $proxyClient, $header, $hostname, $path = '/cache.php')
51+
{
52+
$this->assertMiss($this->getResponse($path));
53+
$this->assertHit($this->getResponse($path));
54+
55+
$proxyClient->ban([$header => 'wrong-host.lo'])->flush();
56+
$this->assertHit($this->getResponse($path));
57+
58+
$proxyClient->ban([$header => $hostname])->flush();
59+
$this->assertMiss($this->getResponse($path));
60+
}
61+
62+
/**
63+
* Asserting that banPath leads to content getting invalidated.
64+
*
65+
* @param BanInterface $proxyClient The client to send ban instructions to the cache
66+
* @param array $paths The paths to get, defaults to [/cache.php, json.php]
67+
*/
68+
protected function assertBanPath(BanInterface $proxyClient, array $paths = ['/cache.php', '/json.php'])
69+
{
70+
foreach ($paths as $path) {
71+
$this->assertMiss($this->getResponse($path));
72+
$this->assertHit($this->getResponse($path));
73+
}
74+
75+
$proxyClient->banPath('.*')->flush();
76+
77+
foreach ($paths as $path) {
78+
$this->assertMiss($this->getResponse($path));
79+
}
80+
}
81+
82+
/**
83+
* Asserting that banPath leads to content getting invalidated.
84+
*
85+
* @param BanInterface $proxyClient The client to send ban instructions to the cache
86+
* @param string $htmlPath Path to a HTML content, defaults to /cache.php
87+
* @param string $otherPath Path to a non-HTML content, defaults to json.php
88+
*/
89+
protected function assertBanPathContentType(BanInterface $proxyClient, $htmlPath = '/cache.php', $otherPath = '/json.php')
90+
{
91+
$this->assertMiss($this->getResponse($htmlPath));
92+
$this->assertHit($this->getResponse($htmlPath));
93+
94+
$this->assertMiss($this->getResponse($otherPath));
95+
$this->assertHit($this->getResponse($otherPath));
96+
97+
$proxyClient->banPath('.*', 'text/html')->flush();
98+
$this->assertMiss($this->getResponse($htmlPath));
99+
$this->assertHit($this->getResponse($otherPath));
100+
}
101+
}

tests/Functional/ProxyClient/NginxProxyClientTest.php

Lines changed: 19 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -19,81 +19,45 @@
1919
*/
2020
class NginxProxyClientTest extends NginxTestCase
2121
{
22-
public function testPurgeSeparateLocationPath()
23-
{
24-
$this->assertMiss($this->getResponse('/cache.php'));
25-
$this->assertHit($this->getResponse('/cache.php'));
26-
27-
$nginx = $this->getProxyClient('/purge');
28-
$nginx->purge('/cache.php')->flush();
29-
30-
$this->assertMiss($this->getResponse('/cache.php'));
31-
}
22+
use RefreshAssertions;
23+
use PurgeAssertions;
3224

3325
public function testPurgeSeparateLocation()
3426
{
35-
$this->assertMiss($this->getResponse('/cache.php'));
36-
$this->assertHit($this->getResponse('/cache.php'));
37-
38-
$nginx = $this->getProxyClient('/purge');
39-
40-
$nginx->purge(sprintf('http://%s/cache.php', $this->getHostName()))->flush();
41-
42-
$this->assertMiss($this->getResponse('/cache.php'));
27+
$this->assertPurge($this->getProxyClient('/purge'));
4328
}
4429

4530
public function testPurgeSameLocation()
4631
{
47-
$this->assertMiss($this->getResponse('/cache.php'));
48-
$this->assertHit($this->getResponse('/cache.php'));
32+
$this->assertPurge($this->getProxyClient());
33+
}
4934

50-
$nginx = $this->getProxyClient();
51-
$nginx->purge(sprintf('http://%s/cache.php', $this->getHostName()))->flush();
35+
public function testPurgeContentType()
36+
{
37+
$this->markTestSkipped('Not working with nginx, it can only purge one type');
5238

53-
$this->assertMiss($this->getResponse('/cache.php'));
39+
$this->assertPurgeContentType($this->getProxyClient());
5440
}
5541

56-
public function testPurgeSameLocationPath()
42+
public function testPurgeSeparateLocationHost()
5743
{
58-
$this->assertMiss($this->getResponse('/cache.php'));
59-
$this->assertHit($this->getResponse('/cache.php'));
60-
61-
$nginx = $this->getProxyClient();
62-
$nginx->purge('/cache.php')->flush();
44+
$this->assertPurgeHost($this->getProxyClient('/purge'), sprintf('http://%s', $this->getHostName()));
45+
}
6346

64-
$this->assertMiss($this->getResponse('/cache.php'));
47+
public function testPurgeSameLocationHost()
48+
{
49+
$this->assertPurgeHost($this->getProxyClient(), sprintf('http://%s', $this->getHostName()));
6550
}
6651

6752
public function testRefresh()
6853
{
69-
$this->assertMiss($this->getResponse('/cache.php'));
70-
$response = $this->getResponse('/cache.php');
71-
$this->assertHit($response);
72-
73-
$nginx = $this->getProxyClient();
74-
$nginx->refresh('/cache.php')->flush();
75-
usleep(1000);
76-
$refreshed = $this->getResponse('/cache.php');
77-
$this->assertGreaterThan(
78-
(float) (string) $response->getBody(),
79-
(float) (string) $refreshed->getBody()
80-
);
54+
$this->assertRefresh($this->getProxyClient());
8155
}
8256

83-
public function testRefreshPath()
57+
public function testRefreshContentType()
8458
{
85-
$this->assertMiss($this->getResponse('/cache.php'));
86-
$response = $this->getResponse('/cache.php');
87-
$this->assertHit($response);
88-
89-
$nginx = $this->getProxyClient();
90-
$nginx->refresh('/cache.php')->flush();
91-
usleep(1000);
92-
$refreshed = $this->getResponse('/cache.php');
59+
$this->markTestSkipped('TODO: is nginx mixing up variants?');
9360

94-
$this->assertGreaterThan(
95-
(float) (string) $response->getBody(),
96-
(float) (string) $refreshed->getBody()
97-
);
61+
$this->assertRefreshContentType($this->getProxyClient());
9862
}
9963
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSHttpCache package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\HttpCache\Tests\Functional\ProxyClient;
13+
14+
use FOS\HttpCache\ProxyClient\Invalidation\PurgeInterface;
15+
16+
/**
17+
* Assertions that do the purge operations.
18+
*/
19+
trait PurgeAssertions
20+
{
21+
/**
22+
* Asserting that purging leads to invalidated content.
23+
*
24+
* @param PurgeInterface $proxyClient The client to send purge instructions to the cache
25+
* @param string $path The path to get and purge, defaults to /cache.php
26+
*/
27+
protected function assertPurge(PurgeInterface $proxyClient, $path = '/cache.php')
28+
{
29+
$this->assertMiss($this->getResponse($path));
30+
$this->assertHit($this->getResponse($path));
31+
32+
$proxyClient->purge($path)->flush();
33+
$this->assertMiss($this->getResponse($path));
34+
}
35+
36+
/**
37+
* Asserting that purging including the domain leads to invalidated content.
38+
*
39+
* @param PurgeInterface $proxyClient The client to send purge instructions to the cache
40+
* @param string $host The host name to use in the purge request
41+
* @param string $path The path to get and purge, defaults to /cache.php
42+
*/
43+
protected function assertPurgeHost(PurgeInterface $proxyClient, $host, $path = '/cache.php')
44+
{
45+
$this->assertMiss($this->getResponse($path));
46+
$this->assertHit($this->getResponse($path));
47+
48+
$proxyClient->purge($host.$path)->flush();
49+
$this->assertMiss($this->getResponse($path));
50+
}
51+
52+
protected function assertPurgeContentType(PurgeInterface $proxyClient, $path = '/negotiation.php')
53+
{
54+
$json = ['Accept' => 'application/json'];
55+
$html = ['Accept' => 'text/html'];
56+
57+
$response = $this->getResponse($path, $json);
58+
$this->assertMiss($response);
59+
$this->assertEquals('application/json', $response->getHeaderLine('Content-Type'));
60+
$this->assertHit($this->getResponse($path, $json));
61+
62+
$response = $this->getResponse($path, $html);
63+
$this->assertContains('text/html', $response->getHeaderLine('Content-Type'));
64+
$this->assertMiss($response);
65+
$this->assertHit($this->getResponse($path, $html));
66+
67+
$this->getResponse($path);
68+
$proxyClient->purge($path)->flush();
69+
$this->assertMiss($this->getResponse($path, $json));
70+
$this->assertMiss($this->getResponse($path, $html));
71+
}
72+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSHttpCache package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\HttpCache\Tests\Functional\ProxyClient;
13+
14+
use FOS\HttpCache\ProxyClient\Invalidation\RefreshInterface;
15+
16+
/**
17+
* Assertions that do the refresh operations.
18+
*/
19+
trait RefreshAssertions
20+
{
21+
/**
22+
* Asserting that refreshing leads to updated content that is already cached.
23+
*
24+
* @param RefreshInterface $proxyClient The client to send refresh instructions to the cache
25+
* @param string $path The path to get and refresh, defaults to /cache.php
26+
*/
27+
protected function assertRefresh(RefreshInterface $proxyClient, $path = '/cache.php')
28+
{
29+
$this->assertMiss($this->getResponse($path));
30+
$response = $this->getResponse($path);
31+
$this->assertHit($response);
32+
33+
$proxyClient->refresh($path)->flush();
34+
usleep(1000);
35+
$refreshed = $this->getResponse($path);
36+
37+
$originalTimestamp = (float) (string) $response->getBody();
38+
$refreshedTimestamp = (float) (string) $refreshed->getBody();
39+
40+
\PHPUnit_Framework_Assert::assertThat(
41+
$refreshedTimestamp,
42+
\PHPUnit_Framework_Assert::greaterThan($originalTimestamp)
43+
);
44+
}
45+
46+
/**
47+
* Asserting that refreshing one variant does not touch the other variants.
48+
*
49+
* @param RefreshInterface $proxyClient The client to send refresh instructions to the cache
50+
* @param string $path The path to get and refresh, defaults to /negotiation.php
51+
*/
52+
protected function assertRefreshContentType(RefreshInterface $proxyClient, $path = '/negotiation.php')
53+
{
54+
$json = ['Accept' => 'application/json'];
55+
$html = ['Accept' => 'text/html'];
56+
57+
$proxyClient->refresh($path, $json)->flush();
58+
59+
$this->assertHit($this->getResponse($path, $json));
60+
$this->assertMiss($this->getResponse($path, $html));
61+
}
62+
}

0 commit comments

Comments
 (0)