Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compiling container using Secrets #606

Closed
wants to merge 14 commits into from
23 changes: 23 additions & 0 deletions src/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use FOS\HttpCache\CacheInvalidator;
use FOS\HttpCache\ProxyClient\ProxyClient;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\VarExporter\LazyObjectInterface;

/**
* The CacheManager is a CacheInvalidator but adds symfony Route support and
Expand All @@ -23,6 +24,11 @@
*/
class CacheManager extends CacheInvalidator
{
/**
* @var ProxyClient
*/
private $cache;

/**
* @var UrlGeneratorInterface
*/
Expand All @@ -44,6 +50,7 @@ class CacheManager extends CacheInvalidator
public function __construct(ProxyClient $cache, UrlGeneratorInterface $urlGenerator)
{
parent::__construct($cache);
$this->cache = $cache;
$this->urlGenerator = $urlGenerator;
}

Expand Down Expand Up @@ -88,4 +95,20 @@ public function refreshRoute($route, array $parameters = [], array $headers = []

return $this;
}

/**
* Send all pending invalidation requests.
*
* @return int the number of cache invalidations performed per caching server
*
* @throws \FOS\HttpCache\Exception\ExceptionCollection
*/
public function flush()
{
if (!$this->cache instanceof LazyObjectInterface || $this->cache->isLazyObjectInitialized()) {
return parent::flush();
}

return 0;
}
}
3 changes: 2 additions & 1 deletion src/Resources/config/cloudflare.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
<services>
<service id="fos_http_cache.proxy_client.cloudflare"
class="FOS\HttpCache\ProxyClient\Cloudflare"
public="true">
public="true"
lazy="true">
<argument type="service" id="fos_http_cache.proxy_client.cloudflare.http_dispatcher"/>
<argument>%fos_http_cache.proxy_client.cloudflare.options%</argument>
</service>
Expand Down
3 changes: 2 additions & 1 deletion src/Resources/config/fastly.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
<services>
<service id="fos_http_cache.proxy_client.fastly"
class="FOS\HttpCache\ProxyClient\Fastly"
public="false">
public="false"
lazy="true">
<argument type="service" id="fos_http_cache.proxy_client.fastly.http_dispatcher"/>
<argument>%fos_http_cache.proxy_client.fastly.options%</argument>
</service>
Expand Down
42 changes: 42 additions & 0 deletions tests/Unit/CacheManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@

namespace FOS\HttpCacheBundle\Tests\Unit;

use FOS\HttpCache\ProxyClient\HttpProxyClient;
use FOS\HttpCache\ProxyClient\Invalidation\PurgeCapable;
use FOS\HttpCache\ProxyClient\Invalidation\RefreshCapable;
use FOS\HttpCache\ProxyClient\ProxyClient;
use FOS\HttpCacheBundle\CacheManager;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\VarExporter\LazyObjectInterface;

class CacheManagerTest extends TestCase
{
Expand Down Expand Up @@ -84,4 +86,44 @@ public function testRefreshRoute()
->refreshRoute('route_with_params', ['id' => 123], ['X-Foo' => 'bar'])
;
}

public function testSkipFlushOnEmptyInvalidationsAndLazyLoaded()
{
$proxyClient = \Mockery::mock(HttpProxyClient::class, LazyObjectInterface::class)
->shouldNotReceive('flush')
->shouldReceive('isLazyObjectInitialized')->andReturn(false)
->getMock();

$router = \Mockery::mock(UrlGeneratorInterface::class);

$cacheInvalidator = new CacheManager($proxyClient, $router);
$this->assertEquals(0, $cacheInvalidator->flush());
}

public function testFlushOnNotLazyLoaded()
{
$proxyClient = \Mockery::mock(HttpProxyClient::class)
->shouldReceive('flush')->andReturn(0)
->shouldNotReceive('isLazyObjectInitialized')
->getMock();

$router = \Mockery::mock(UrlGeneratorInterface::class);

$cacheInvalidator = new CacheManager($proxyClient, $router);
$this->assertEquals(0, $cacheInvalidator->flush());
}

public function testFlushOnLazyLoaded()
{
$proxyClient = \Mockery::mock(HttpProxyClient::class, LazyObjectInterface::class, PurgeCapable::class);
$proxyClient->shouldReceive('flush')->andReturn(1);
$proxyClient->shouldReceive('purge');
$proxyClient->shouldReceive('isLazyObjectInitialized')->andReturn(true);

$router = \Mockery::mock(UrlGeneratorInterface::class);

$cacheInvalidator = new CacheManager($proxyClient, $router);
$cacheInvalidator->invalidatePath('/foo');
$this->assertEquals(1, $cacheInvalidator->flush());
}
}
Loading