Skip to content
This repository was archived by the owner on Jan 8, 2024. It is now read-only.

Commit 28b9609

Browse files
committed
Added main functional tests. More to come...
1 parent 7d58c5b commit 28b9609

File tree

8 files changed

+103
-82
lines changed

8 files changed

+103
-82
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,8 @@
5555
},
5656
"autoload": {
5757
"psr-4": { "Phpfastcache\\Bundle\\": "src/" }
58+
},
59+
"autoload-dev": {
60+
"psr-4": { "Phpfastcache\\Bundle\\Tests\\": "tests/" }
5861
}
5962
}

phpunit.xml.dist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<testsuites>
1414
<testsuite name="PhpfastcacheBundle Test Suite">
15-
<directory>./tests</directory>
15+
<directory>./Tests</directory>
1616
</testsuite>
1717
</testsuites>
1818

@@ -25,7 +25,7 @@
2525
<whitelist>
2626
<directory>./</directory>
2727
<exclude>
28-
<directory>./tests</directory>
28+
<directory>./Tests</directory>
2929
<directory>./vendor</directory>
3030
</exclude>
3131
</whitelist>

tests/Functional/App/Controller/CacheController.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
namespace Phpfastcache\Bundle\Tests\Functional\App\Controller;
1919

20+
use Phpfastcache\Bundle\Response\CacheableResponse;
2021
use Phpfastcache\Bundle\Service\Phpfastcache;
2122
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
2223
use Symfony\Component\HttpFoundation\JsonResponse;
@@ -45,18 +46,42 @@ public function index(Request $request, Phpfastcache $phpfastcache)
4546
* @param \Phpfastcache\Bundle\Service\Phpfastcache $phpfastcache
4647
* @return Response
4748
*/
48-
public function cacheMiss(Request $request, Phpfastcache $phpfastcache)
49+
public function cacheTest(Request $request, Phpfastcache $phpfastcache)
4950
{
50-
return JsonResponse::create(['result' => 'ok']);
51+
$filecache = $phpfastcache->get('filecache');
52+
$cacheItem = $filecache->getItem(__FUNCTION__);
53+
$cacheHit = $cacheItem->isHit();
54+
55+
if(!$cacheHit){
56+
$cacheItem->set(1337)->expiresAfter(5);
57+
$filecache->save($cacheItem);
58+
}
59+
60+
return JsonResponse::create([
61+
'result' => 'ok',
62+
'cache' => $cacheHit ? 'hit' : 'miss'
63+
]);
5164
}
5265

5366
/**
5467
* @param \Symfony\Component\HttpFoundation\Request $request
5568
* @param \Phpfastcache\Bundle\Service\Phpfastcache $phpfastcache
5669
* @return Response
5770
*/
58-
public function cacheHit(Request $request, Phpfastcache $phpfastcache)
71+
public function cacheHttp(Request $request, Phpfastcache $phpfastcache)
5972
{
60-
return JsonResponse::create(['result' => 'ok']);
73+
return (new CacheableResponse($phpfastcache->get('filecache'), $request))->getResponse(__FUNCTION__, 10, function () {
74+
return JsonResponse::create(['result' => 'ok']);
75+
});
76+
}
77+
78+
/**
79+
* @param \Symfony\Component\HttpFoundation\Request $request
80+
* @param \Phpfastcache\Bundle\Service\Phpfastcache $phpfastcache
81+
* @return Response
82+
*/
83+
public function cacheError(Request $request, Phpfastcache $phpfastcache)
84+
{
85+
$phpfastcache->get('unexisting');
6186
}
6287
}

tests/Functional/App/Kernel.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ protected function configureContainer(ContainerBuilder $container, LoaderInterfa
6363
protected function configureRoutes(RouteCollectionBuilder $routes)
6464
{
6565
$routes->add('/', CacheController::class . ':index');
66-
$routes->add('/cache/miss', CacheController::class . ':cacheMiss');
67-
$routes->add('/cache/hit', CacheController::class . ':cacheHit');
66+
$routes->add('/cache-test', CacheController::class . ':cacheTest');
67+
$routes->add('/cache-error', CacheController::class . ':cacheError');
68+
$routes->add('/cache-http', CacheController::class . ':cacheHttp');
6869
}
6970

7071
/**

tests/Functional/App/config.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,13 @@ framework:
1212
web_profiler:
1313
toolbar: false
1414
intercept_redirects: false
15+
16+
# PhpFastCache configuration
17+
phpfastcache:
18+
twig_driver: "filecache" # This option must be a valid declared driver, in our example: "filecache"
19+
twig_block_debug: true # This option will wrap CACHE/ENDCACHE blocks with block debug as HTML comment
20+
drivers:
21+
filecache:
22+
type: Files
23+
parameters:
24+
path: "%kernel.cache_dir%/phpfastcache/"

tests/Functional/IntegrationTest.php renamed to tests/Functional/MainFunctionalTest.php

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace Phpfastcache\Bundle\Tests\Functional;
1919

2020
use Phpfastcache\Bundle\Tests\Functional\App\Kernel;
21+
use Phpfastcache\CacheManager as PhpfastcacheManager;
2122
use Symfony\Bundle\FrameworkBundle\Client;
2223
use Symfony\Bundle\FrameworkBundle\Console\Application;
2324
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
@@ -28,34 +29,84 @@
2829
/**
2930
* Class IntegrationTest
3031
* @package Phpfastcache\Bundle\Tests\Functional
32+
* @runInSeparateProcesses
3133
*/
32-
class IntegrationTest extends WebTestCase
34+
class MainFunctionalTest extends WebTestCase
3335
{
3436
/** @var Client */
3537
private $client;
3638

3739
protected function setUp()
3840
{
3941
$this->client = static::createClient();
42+
PhpfastcacheManager::clearInstances();
4043
}
4144

42-
public function testIntegration()
45+
public function testCacheMiss()
4346
{
44-
$response = $this->profileRequest('GET', '/');
47+
$response = $this->profileRequest('GET', '/cache-test');
4548

4649
$this->assertSame(Response::HTTP_OK, $response->getStatusCode());
50+
$this->assertSame('miss', \json_decode($response->getContent(), true)['cache']);
51+
}
52+
53+
public function testCacheHit()
54+
{
55+
$response = $this->profileRequest('GET', '/cache-test');
56+
57+
$this->assertSame(Response::HTTP_OK, $response->getStatusCode());
58+
$this->assertSame('hit', \json_decode($response->getContent(), true)['cache']);
59+
}
60+
61+
public function testCacheError()
62+
{
63+
$response = $this->profileRequest('GET', '/cache-error');
64+
65+
$this->assertSame(Response::HTTP_INTERNAL_SERVER_ERROR, $response->getStatusCode());
66+
}
67+
68+
public function testCacheHttp()
69+
{
70+
/**
71+
* The first request should be a MISS one
72+
*/
73+
$response = $this->profileRequest('GET', '/cache-http');
74+
$this->assertSame(Response::HTTP_OK, $response->getStatusCode());
75+
$etag = $response->getEtag();
76+
77+
/**
78+
* Clear instance to avoid Phpfastcache alerts
79+
* as we are in a shared process context
80+
*/
81+
PhpfastcacheManager::clearInstances();
82+
83+
/**
84+
* The second request should be a HIT one with a 304 response
85+
*/
86+
$response = $this->profileRequest('GET', '/cache-http', [
87+
'If-None-Match' => $etag
88+
]);
89+
$this->assertSame(Response::HTTP_NOT_MODIFIED, $response->getStatusCode());
90+
$this->assertSame('', trim($response->getContent()));
4791
}
4892

4993
/**
5094
* @param string $method
5195
* @param string $uri
96+
* @param array $headers
5297
* @return \Symfony\Component\HttpFoundation\Response
5398
*/
54-
private function profileRequest(string $method, string $uri): Response
99+
private function profileRequest(string $method, string $uri, array $headers = []): Response
55100
{
56101
$client = $this->client;
57102
$client->enableProfiler();
58-
$client->request($method, $uri);
103+
$serverParameter = [];
104+
105+
foreach ($headers as $headerKey => $headerVal) {
106+
$serverParameter['HTTP_' . \str_replace('-', '_', \strtoupper($headerKey))] = $headerVal;
107+
}
108+
109+
$client->request($method, $uri, [], [], $serverParameter);
59110

60111
return $client->getResponse();
61112
}

tests/Response/CacheableResponseTest.php

Lines changed: 0 additions & 46 deletions
This file was deleted.

tests/Service/PhpfastcacheTest.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)