-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCacheWrapperTest.php
More file actions
178 lines (145 loc) · 5.87 KB
/
CacheWrapperTest.php
File metadata and controls
178 lines (145 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
namespace CacheBundle\Tests;
use CacheBundle\Annotation\Cache;
use CacheBundle\Tests\Helpers\CacheableClass;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Monolog\Handler\TestHandler;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symfony\Component\HttpKernel\Kernel;
class CacheWrapperTest extends KernelTestCase
{
/**
* @var ContainerInterface
*/
protected $container;
public function setUp()
{
parent::setUp();
self::bootKernel();
$this->container = self::$kernel->getContainer();
}
protected static function getKernelClass()
{
return get_class(new class('test', []) extends Kernel
{
/**
* Returns an array of bundles to register.
*
* @return BundleInterface[] An array of bundle instances
*/
public function registerBundles()
{
return [
new \Symfony\Bundle\MonologBundle\MonologBundle(),
new \CacheBundle\CacheBundle()
];
}
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(__DIR__ . '/config.yml');
}
public function __construct($environment, $debug)
{
parent::__construct($environment, $debug);
$loader = require __DIR__ . '/../vendor/autoload.php';
AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
$this->rootDir = __DIR__ . '/app/';
}
});
}
public function testWithParams()
{
$object = $this->container->get('cache.testservice');
$data = $object->getCachedTime();
$dataWithParam = $object->getCachedTime(300);
sleep(1);
$this->assertEquals($data, $object->getCachedTime());
$this->assertEquals($dataWithParam, $object->getCachedTime(300));
}
public function testWithSamePrefix()
{
/** @var CacheableClass $object */
$object = $this->container->get('cache.testservice');
$objectReflectionClass = new \ReflectionClass($object);
$annotationReader = new AnnotationReader();
/** @var Cache $methodOne */
$methodOne = $annotationReader->getMethodAnnotation(new \ReflectionMethod($objectReflectionClass->getParentClass()->getName(), 'getComputationOneWithoutParametersSamePrefix'), Cache::class);
/** @var Cache $methodTwo */
$methodTwo = $annotationReader->getMethodAnnotation(new \ReflectionMethod($objectReflectionClass->getParentClass()->getName(), 'getComputationTwoWithoutParametersSamePrefix'), Cache::class);
$resultOne = $object->getComputationOneWithoutParametersSamePrefix();
$resultTwo = $object->getComputationTwoWithoutParametersSamePrefix();
$this->assertEquals($methodOne->getCache(), $methodTwo->getCache());
$this->assertNotEquals($resultOne, $resultTwo);
sleep(1);
$this->assertNotEquals($object->getComputationOneWithoutParametersSamePrefix(), $object->getComputationTwoWithoutParametersSamePrefix());
}
public function testWithParamsExtendedClass()
{
$object = $this->container->get('cache.testservice.extended');
$data = $object->getCachedTime();
$dataWithParam = $object->getCachedTime(300);
sleep(1);
$this->assertEquals($data, $object->getCachedTime());
$this->assertEquals($dataWithParam, $object->getCachedTime(300));
}
public function testReset()
{
$object = $this->container->get('cache.testservice');
$data = $object->getCachedTime();
$this->assertEquals($data, $object->getCachedTime());
$this->assertNotEquals($data, $object->getCachedTimeWithReset());
}
public function testWithMultiParams()
{
$object = $this->container->get('cache.testservice');
$result = $object->testWithMultipleParams(200, 300);
$this->assertEquals($result, $object->testWithMultipleParams(200, 300));
$this->assertEquals($result, $object->testWithMultipleParams(200, 150));
$this->assertEquals($result, $object->testWithMultipleParams(200, 150, 100));
$this->assertNotEquals($result, $object->testWithMultipleParams(200, 150, 200));
}
/**
* @expectedExceptionMessage Missing param3
* @expectedException \CacheBundle\Exception\CacheException
*/
public function testWithWrongParamNames()
{
$object = $this->container->get('cache.testservice');
$result = $object->testWithWrongParams(200, 300);
}
public function testMethodWithoutParams()
{
$object = $this->container->get('cache.testservice');
$result = $object->testWithoutParams();
$this->assertEquals($result, $object->testWithoutParams());
}
public function testAccessToProtectedMethod()
{
$object = $this->container->get('cache.testservice');
$result = $object->publicMethodThatCallsProtected();
$this->assertEquals($result, $object->publicMethodThatCallsProtected());
}
/**
* Get TestHandler object
*
* @return TestHandler
*/
protected function getLogHandler()
{
$testHandler = false;
foreach ($this->container->get('logger')->getHandlers() as $handler) {
if ($handler instanceof TestHandler) {
$testHandler = $handler;
break;
}
}
if (!$testHandler instanceof TestHandler) {
throw new \RuntimeException('TestHandler does not exists in monolog.');
}
return $testHandler;
}
}