Skip to content

Commit a3f47ad

Browse files
committed
Improve base_url explanation
Rename to getHttpClient() * Rename getClient() to getHttpClient() * Make convenience methods protected * Remove getProxyClient() method from ProxyTestCase * Prefix base_url with "http://" so we can use it safely for the HTTP client * Update docs Improve type hints Remove leading whitespace
1 parent cb142b3 commit a3f47ad

File tree

6 files changed

+58
-39
lines changed

6 files changed

+58
-39
lines changed

DependencyInjection/FOSHttpCacheExtension.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -217,13 +217,16 @@ private function loadVarnish(ContainerBuilder $container, XmlFileLoader $loader,
217217
{
218218
$loader->load('varnish.xml');
219219
foreach ($config['servers'] as $url) {
220-
$this->validateUrl($url, 'Not a valid varnish server address: "%s"');
220+
$this->validateUrl($url, 'Not a valid Varnish server address: "%s"');
221221
}
222222
if (!empty($config['base_url'])) {
223-
$this->validateUrl($config['base_url'], 'Not a valid base path: "%s"');
223+
$baseUrl = $this->prefixSchema($config['base_url'], 'Not a valid base path: "%s"');
224+
$this->validateUrl($baseUrl, 'Not a valid base path: "%s"');
225+
} else {
226+
$baseUrl = null;
224227
}
225228
$container->setParameter($this->getAlias() . '.proxy_client.varnish.servers', $config['servers']);
226-
$container->setParameter($this->getAlias() . '.proxy_client.varnish.base_url', $config['base_url']);
229+
$container->setParameter($this->getAlias() . '.proxy_client.varnish.base_url', $baseUrl);
227230
if ($config['guzzle_client']) {
228231
$container->getDefinition($this->getAlias() . '.proxy_client.varnish')
229232
->addArgument(
@@ -237,13 +240,15 @@ private function loadNginx(ContainerBuilder $container, XmlFileLoader $loader, a
237240
{
238241
$loader->load('nginx.xml');
239242
foreach ($config['servers'] as $url) {
240-
$this->validateUrl($url, 'Not a valid nginx server address: "%s"');
243+
$this->validateUrl($url, 'Not a valid Nginx server address: "%s"');
241244
}
242245
if (!empty($config['base_url'])) {
243-
$this->validateUrl($config['base_url'], 'Not a valid base path: "%s"');
246+
$baseUrl = $this->prefixSchema($config['base_url'], 'Not a valid base path: "%s"');
247+
} else {
248+
$baseUrl = null;
244249
}
245250
$container->setParameter($this->getAlias() . '.proxy_client.nginx.servers', $config['servers']);
246-
$container->setParameter($this->getAlias() . '.proxy_client.nginx.base_url', $config['base_url']);
251+
$container->setParameter($this->getAlias() . '.proxy_client.nginx.base_url', $baseUrl);
247252
$container->setParameter($this->getAlias() . '.proxy_client.nginx.purge_location', $config['purge_location']);
248253
}
249254

@@ -341,14 +346,22 @@ private function loadInvalidatorRules(ContainerBuilder $container, array $config
341346
}
342347

343348
private function validateUrl($url, $msg)
349+
{
350+
$prefixed = $this->prefixSchema($url);
351+
352+
if (!$parts = parse_url($prefixed)) {
353+
throw new InvalidConfigurationException(sprintf($msg, $url));
354+
}
355+
}
356+
357+
358+
private function prefixSchema($url)
344359
{
345360
if (false === strpos($url, '://')) {
346361
$url = sprintf('%s://%s', 'http', $url);
347362
}
348363

349-
if (!$parts = parse_url($url)) {
350-
throw new InvalidConfigurationException(sprintf($msg, $url));
351-
}
364+
return $url;
352365
}
353366

354367
private function getDefault(array $config)

Resources/doc/features/testing.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ methods for cache testing::
4141
Test Client
4242
^^^^^^^^^^^
4343

44-
The ``getResponse()`` method calls ``getClient()`` to retrieve a test client. You
44+
The ``getResponse()`` method calls ``getHttpClient()`` to retrieve a test client. You
4545
can use this client yourself to customise the requests. Note that the test
4646
client must be :doc:`enabled in your configuration </reference/configuration/test>`.
4747
By default, it is enabled when you access your application in debug mode and

Resources/doc/reference/configuration/proxy-client.rst

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,17 @@ base_url
3333

3434
**type**: ``string``
3535

36-
This must match the web host name clients are using when connecting
37-
to the caching proxy. Optionally can contain a base path to your
38-
application. Used for invalidation with paths.
36+
The hostname (or base URL) where users access your web application. The base
37+
URL may contain a path. If you access your web application on a port other than
38+
80, include that port:
39+
40+
.. code-block:: yaml
41+
42+
# app/config/config.yml
43+
fos_http_cache:
44+
proxy_client:
45+
varnish:
46+
base_url: yourwebsite.com:8000
3947
4048
.. warning::
4149

Test/ProxyTestCase.php

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
use FOS\HttpCache\Test\PHPUnit\IsCacheHitConstraint;
66
use FOS\HttpCache\Test\PHPUnit\IsCacheMissConstraint;
7-
use FOS\HttpCache\Test\ProxyTestCaseInterface;
7+
use FOS\HttpCache\Test\Proxy\ProxyInterface;
8+
use Guzzle\Http\ClientInterface;
89
use Guzzle\Http\Message\Response;
910
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
11+
use Symfony\Component\DependencyInjection\ContainerInterface;
1012

1113
/**
1214
* Base class that you can extend to run integration tests against a live
@@ -57,21 +59,28 @@ public static function isCacheMiss()
5759
}
5860

5961
/**
60-
* Get test client
62+
* Get HTTP test client for making requests to your application through a
63+
* live caching proxy
6164
*
62-
* @return \Guzzle\Http\Client
65+
* @return ClientInterface
6366
*/
64-
public function getClient()
67+
protected function getHttpClient()
6568
{
6669
return static::getContainer()->get('fos_http_cache.test.default_client');
6770
}
6871

6972
/**
70-
* {@inheritdoc}
73+
* Get a response from your application through a live caching proxy
74+
*
75+
* @param string $url Request URL (absolute or relative)
76+
* @param array $headers Request HTTP headers
77+
* @param array $options Request options
78+
*
79+
* @return Response
7180
*/
72-
public function getResponse($url, array $headers = array(), $options = array())
81+
protected function getResponse($url, array $headers = array(), $options = array())
7382
{
74-
return $this->getClient()->get($url, $headers, $options)->send();
83+
return $this->getHttpClient()->get($url, $headers, $options)->send();
7584
}
7685

7786
/**
@@ -94,7 +103,7 @@ protected function setUp()
94103
/**
95104
* Get proxy server
96105
*
97-
* @return \FOS\HttpCache\Test\Proxy\ProxyInterface
106+
* @return ProxyInterface
98107
*
99108
* @throws \RuntimeException If proxy server is not configured
100109
*/
@@ -109,16 +118,6 @@ protected function getProxy()
109118
return static::getContainer()->get('fos_http_cache.test.default_proxy_server');
110119
}
111120

112-
/**
113-
* Get default caching proxy client
114-
*
115-
* @return \FOS\HttpCache\ProxyClient\ProxyClientInterface
116-
*/
117-
protected function getProxyClient()
118-
{
119-
return static::getContainer()->get('fos_http_cache.default_proxy_client');
120-
}
121-
122121
/**
123122
* Get HTTP header that shows whether the response was a cache hit or miss
124123
*
@@ -132,7 +131,7 @@ protected static function getCacheDebugHeader()
132131
/**
133132
* Get container
134133
*
135-
* @return \Symfony\Component\DependencyInjection\ContainerInterface
134+
* @return ContainerInterface
136135
*/
137136
protected static function getContainer()
138137
{

Tests/Functional/Fixtures/app/config/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fos_http_cache:
1717
proxy_client:
1818
varnish:
1919
servers: 127.0.0.1
20-
base_url: http://localhost:8080
20+
base_url: localhost:8080
2121
tags:
2222
rules:
2323
-

Tests/Functional/Test/ProxyTestCaseTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace FOS\HttpCacheBundle\Tests\Functional\Test;
1313

1414
use FOS\HttpCacheBundle\Test\ProxyTestCase;
15-
use Symfony\Bundle\FrameworkBundle\Tests\Functional\WebTestCase;
1615

1716
class ProxyTestCaseTest extends ProxyTestCase
1817
{
@@ -26,12 +25,12 @@ protected function setUp()
2625
parent::setUp();
2726
}
2827

29-
public function testGetProxyClient()
28+
public function testGetHttpClient()
3029
{
31-
$this->assertInstanceOf(
32-
'\FOS\HttpCache\ProxyClient\ProxyClientInterface',
33-
$this->getProxyClient()
34-
);
30+
$client = $this->getHttpClient();
31+
32+
$this->assertInstanceOf('\Guzzle\Http\Client', $client);
33+
$this->assertEquals('http://localhost:8080', $client->getBaseUrl());
3534
}
3635

3736
public function testAssertHit()

0 commit comments

Comments
 (0)