1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Dolgov_M <[email protected]> |
4
|
|
|
* @date 15.11.2017 at 16:41 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace SilexPinbaProvider\Test; |
8
|
|
|
|
9
|
|
|
use Intaro\PinbaBundle\Stopwatch\Stopwatch; |
10
|
|
|
use PHPUnit\Framework\TestCase; |
11
|
|
|
use Silex\Provider\TwigServiceProvider; |
12
|
|
|
use SilexPinbaProvider\SilexPinbaProvider; |
13
|
|
|
use Silex\Application; |
14
|
|
|
|
15
|
|
|
class PinbaTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
public function testTwigExtension() |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
$storage = new \ArrayObject(); |
23
|
|
|
|
24
|
|
|
$app = new ApplicationEmulator(); |
25
|
|
|
$app |
26
|
|
|
->register(new TwigServiceProvider(),array( |
27
|
|
|
'twig.templates' => array('hello' => 'Hello {{ name }}!'), |
28
|
|
|
)) |
29
|
|
|
->register(new SilexPinbaProvider()); |
30
|
|
|
|
31
|
|
|
$app['intaro_pinba.stopwatch.class'] = 'SilexPinbaProvider\Test\StopwatchEmulate'; |
32
|
|
|
$app->boot(); |
33
|
|
|
$app['intaro_pinba.stopwatch'] -> setStorage($storage); |
34
|
|
|
$app->renderView('hello'); |
35
|
|
|
$this->assertTrue(is_array($storage['tags']), var_export($storage, true)); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
class StopwatchEmulate extends Stopwatch |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var \ArrayObject |
45
|
|
|
*/ |
46
|
|
|
private $storage; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param \ArrayObject $storage |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
|
|
public function setStorage($storage) |
53
|
|
|
{ |
54
|
|
|
$this->storage = $storage; |
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
|
60
|
|
|
public function start(array $tags) |
61
|
|
|
{ |
62
|
|
|
return new StopwatchEventEmulate($tags, $this->storage); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
class StopwatchEventEmulate |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
/** |
70
|
|
|
* @var array |
71
|
|
|
*/ |
72
|
|
|
private $tags; |
73
|
|
|
/** |
74
|
|
|
* @var \ArrayObject |
75
|
|
|
*/ |
76
|
|
|
private $storage; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* StopwatchEventEmulate constructor. |
80
|
|
|
* @param array $tags |
81
|
|
|
* @param \ArrayObject $storage |
82
|
|
|
*/ |
83
|
|
|
public function __construct(array $tags, \ArrayObject $storage) |
84
|
|
|
{ |
85
|
|
|
$this->tags = $tags; |
86
|
|
|
$this->storage = $storage; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
public function stop() |
91
|
|
|
{ |
92
|
|
|
$this->storage['tags'] = $this->tags; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
class ApplicationEmulator extends Application |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
use Application\TwigTrait; |
101
|
|
|
} |
Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.