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

Commit 88c11fe

Browse files
committed
Finished the damn tests
1 parent f72eeb7 commit 88c11fe

File tree

4 files changed

+305
-84
lines changed

4 files changed

+305
-84
lines changed

tests/Command/CommandTestCase.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
/**
4+
*
5+
* This file is part of phpFastCache.
6+
*
7+
* @license MIT License (MIT)
8+
*
9+
* For full copyright and license information, please see the docs/CREDITS.txt file.
10+
*
11+
* @author Georges.L (Geolim4) <[email protected]>
12+
* @author PastisD https://github.com/PastisD
13+
*
14+
*/
15+
16+
namespace Phpfastcache\Bundle\Tests\Command;
17+
18+
use Phpfastcache\Bundle\Service\Phpfastcache;
19+
use PHPUnit\Framework\MockObject\MockObject;
20+
use PHPUnit\Framework\TestCase;
21+
use Symfony\Bundle\FrameworkBundle\Console\Application;
22+
use Symfony\Component\HttpKernel\Kernel;
23+
24+
/**
25+
* Base Class for command tests
26+
*
27+
* @author Sebastian Göttschkes <[email protected]>
28+
*/
29+
abstract class CommandTestCase extends TestCase
30+
{
31+
32+
/**
33+
* @var \Symfony\Bundle\FrameworkBundle\Console\Application
34+
*/
35+
protected $application;
36+
37+
/**
38+
* @var \Symfony\Component\DependencyInjection\ContainerInterface|MockObject
39+
*/
40+
protected $container;
41+
42+
/**
43+
* @var \Phpfastcache\Bundle\Service\Phpfastcache|MockObject
44+
*/
45+
protected $phpfastcache;
46+
47+
48+
/**
49+
* @var array
50+
*/
51+
protected $phpfastcacheParameters = [];
52+
53+
/**
54+
* SetUp called before each tests, setting up the environment (application, globally used mocks)
55+
*/
56+
public function setUp()
57+
{
58+
$this->container = $this->getMockBuilder('\\Symfony\\Component\\DependencyInjection\\ContainerInterface')->getMock();
59+
60+
/** @var Kernel|MockObject $kernel */
61+
$kernel = $this->getMockBuilder('\\Symfony\\Component\\HttpKernel\\Kernel')
62+
->disableOriginalConstructor()
63+
->getMock();
64+
$kernel->expects($this->once())
65+
->method('getBundles')
66+
->will($this->returnValue([]));
67+
$kernel->expects($this->any())
68+
->method('getContainer')
69+
->will($this->returnValue($this->container));
70+
$this->application = new Application($kernel);
71+
72+
$this->phpfastcache = $this->getMockBuilder(Phpfastcache::class)
73+
->setConstructorArgs([$this->phpfastcacheParameters])
74+
->getMock();
75+
76+
$this->phpfastcacheParameters = [
77+
'twig_driver' => 'filecache',
78+
'twig_block_debug' => true,
79+
'drivers' =>
80+
[
81+
'filecache' =>
82+
[
83+
'type' => 'Files',
84+
'parameters' =>
85+
[
86+
'path' => '%kernel.cache_dir%/phpfastcache/',
87+
],
88+
],
89+
'staticcache' =>
90+
[
91+
'type' => 'Memstatic',
92+
'parameters' => [],
93+
],
94+
],
95+
];
96+
97+
$command = $this->getCommand();
98+
$this->application->add($command);
99+
}
100+
101+
/**
102+
* Method used by the implementation of the command test to return the actual command object
103+
*
104+
* @return mixed The command to be tested
105+
*/
106+
abstract protected function getCommand();
107+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
/**
4+
*
5+
* This file is part of phpFastCache.
6+
*
7+
* @license MIT License (MIT)
8+
*
9+
* For full copyright and license information, please see the docs/CREDITS.txt file.
10+
*
11+
* @author Georges.L (Geolim4) <[email protected]>
12+
* @author PastisD https://github.com/PastisD
13+
*
14+
*/
15+
16+
namespace Phpfastcache\Bundle\Tests\Command;
17+
18+
use Phpfastcache\Bundle\Command\PhpfastcacheCommand;
19+
use Symfony\Component\Console\Tester\CommandTester;
20+
21+
/**
22+
* PhpfastcacheCommandTest
23+
*/
24+
class PhpfastcacheCommandTest extends CommandTestCase
25+
{
26+
27+
public function setUp()
28+
{
29+
parent::setUp();
30+
}
31+
32+
public function testCommandOptionWithoutSpecifiedDriver()
33+
{
34+
$command = $this->application->find('phpfastcache:clear');
35+
$commandTester = new CommandTester($command);
36+
$commandTester->execute([
37+
'command' => $command->getName(),
38+
'--no-interaction' => true
39+
]);
40+
41+
$this->assertRegExp('/All caches instances got cleared/', $commandTester->getDisplay());
42+
}
43+
44+
public function testCommandOptionWithExistingDriver()
45+
{
46+
$command = $this->application->find('phpfastcache:clear');
47+
$commandTester = new CommandTester($command);
48+
$commandTester->execute([
49+
'command' => $command->getName(),
50+
'driver' => 'filecache',
51+
'--no-interaction' => true
52+
]);
53+
54+
$this->assertRegExp('/Cache instance filecache cleared/', $commandTester->getDisplay());
55+
56+
$commandTester->execute([
57+
'command' => $command->getName(),
58+
'driver' => 'staticcache',
59+
'--no-interaction' => true
60+
]);
61+
62+
$this->assertRegExp('/Cache instance staticcache cleared/', $commandTester->getDisplay());
63+
}
64+
65+
public function testCommandOptionWithNotExistingDriver()
66+
{
67+
$command = $this->application->find('phpfastcache:clear');
68+
$commandTester = new CommandTester($command);
69+
$commandTester->execute([
70+
'command' => $command->getName(),
71+
'driver' => 'notExisting',
72+
'--no-interaction' => true
73+
]);
74+
75+
$this->assertRegExp('/Cache instance notExisting does not exists/', $commandTester->getDisplay());
76+
}
77+
78+
/**
79+
* @return \Phpfastcache\Bundle\Command\PhpfastcacheCommand
80+
*/
81+
protected function getCommand()
82+
{
83+
return new PhpfastcacheCommand($this->phpfastcache, $this->phpfastcacheParameters);
84+
}
85+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
/**
4+
*
5+
* This file is part of phpFastCache.
6+
*
7+
* @license MIT License (MIT)
8+
*
9+
* For full copyright and license information, please see the docs/CREDITS.txt file.
10+
*
11+
* @author Georges.L (Geolim4) <[email protected]>
12+
* @author PastisD https://github.com/PastisD
13+
*
14+
*/
15+
16+
declare(strict_types=1);
17+
18+
namespace Phpfastcache\Bundle\Tests\Functional;
19+
20+
use Phpfastcache\Bundle\Tests\Functional\App\Kernel;
21+
use Phpfastcache\CacheManager as PhpfastcacheManager;
22+
use Symfony\Bundle\FrameworkBundle\Client;
23+
use Symfony\Bundle\FrameworkBundle\Console\Application;
24+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
25+
use Symfony\Component\Console\Tester\CommandTester;
26+
use Symfony\Component\Filesystem\Filesystem;
27+
use Symfony\Component\HttpFoundation\Response;
28+
29+
/**
30+
* Class IntegrationTest
31+
* @package Phpfastcache\Bundle\Tests\Functional
32+
* @runInSeparateProcesses
33+
*/
34+
class AbstractWebTestCase extends WebTestCase
35+
{
36+
/** @var Client */
37+
protected $client;
38+
39+
protected function setUp()
40+
{
41+
$this->client = static::createClient();
42+
PhpfastcacheManager::clearInstances();
43+
}
44+
45+
/**
46+
* @param string $method
47+
* @param string $uri
48+
* @param array $headers
49+
* @return \Symfony\Component\HttpFoundation\Response
50+
*/
51+
protected function profileRequest(string $method, string $uri, array $headers = []): Response
52+
{
53+
$client = $this->client;
54+
$client->enableProfiler();
55+
$serverParameter = [];
56+
57+
foreach ($headers as $headerKey => $headerVal) {
58+
$serverParameter['HTTP_' . \str_replace('-', '_', \strtoupper($headerKey))] = $headerVal;
59+
}
60+
61+
$client->request($method, $uri, [], [], $serverParameter);
62+
63+
return $client->getResponse();
64+
}
65+
66+
/**
67+
* Manage schema and cleanup chores
68+
*/
69+
public static function setUpBeforeClass()
70+
{
71+
static::deleteTmpDir();
72+
$kernel = static::createClient()->getKernel();
73+
74+
/**
75+
* Cleanup Phpfastcache cache
76+
*/
77+
$application = new Application($kernel);
78+
$command = $application->find('phpfastcache:clear');
79+
$commandTester = new CommandTester($command);
80+
$commandTester->execute([
81+
'command' => $command->getName(), '--no-interaction' => true
82+
]);
83+
}
84+
85+
public static function tearDownAfterClass()
86+
{
87+
static::deleteTmpDir();
88+
}
89+
90+
protected static function deleteTmpDir()
91+
{
92+
if (!file_exists($dir = __DIR__ . '/App/var')) {
93+
return;
94+
}
95+
96+
$fs = new Filesystem();
97+
$fs->remove($dir);
98+
}
99+
100+
/**
101+
* @return string
102+
*/
103+
protected static function getKernelClass()
104+
{
105+
require_once __DIR__ . '/App/Kernel.php';
106+
107+
return Kernel::class;
108+
}
109+
}

0 commit comments

Comments
 (0)