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 Psr\Log\AbstractLogger; |
12
|
|
|
use Silex\Provider\TwigServiceProvider; |
13
|
|
|
use SilexPinbaProvider\SilexPinbaProvider; |
14
|
|
|
use Silex\Application; |
15
|
|
|
|
16
|
|
|
class PinbaTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
public function testTwigExtension() |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
global $app; |
|
|
|
|
23
|
|
|
$storage = new \ArrayObject(); |
24
|
|
|
$app = new ApplicationEmulator(); |
25
|
|
|
$emulate = false; |
26
|
|
|
if(!function_exists('pinba_script_name_set')) { |
27
|
|
|
$emulate = true; |
28
|
|
|
$app['pinba_logger'] = function () { |
29
|
|
|
return new PinbaLogger(); |
30
|
|
|
}; |
31
|
|
|
require __DIR__.'/../pinba_emulator.php'; |
32
|
|
|
} |
33
|
|
|
$app |
34
|
|
|
->register(new TwigServiceProvider(),array( |
35
|
|
|
'twig.templates' => array('hello' => 'Hello {{ name }}!'), |
36
|
|
|
)) |
37
|
|
|
->register(new SilexPinbaProvider()); |
38
|
|
|
|
39
|
|
|
$app['intaro_pinba.stopwatch.class'] = 'SilexPinbaProvider\Test\StopwatchEmulate'; |
40
|
|
|
$app->boot(); |
41
|
|
|
/** |
42
|
|
|
* @var $stopwatch StopwatchEmulate |
43
|
|
|
*/ |
44
|
|
|
$stopwatch = $app['intaro_pinba.stopwatch']; |
45
|
|
|
$this->assertTrue($stopwatch instanceof Stopwatch); |
46
|
|
|
$stopwatch-> setStorage($storage); |
47
|
|
|
$app->renderView('hello'); |
48
|
|
|
$this->assertTrue(is_array($storage['tags']), var_export($storage, true)); |
49
|
|
|
if($emulate) { |
50
|
|
|
/** |
51
|
|
|
* @var $logger PinbaLogger |
52
|
|
|
*/ |
53
|
|
|
$logger = $app['pinba_logger']; |
54
|
|
|
$this->assertTrue($logger instanceof PinbaLogger); |
55
|
|
|
$stack = $logger->getLogStack(); |
56
|
|
|
$this->assertTrue(is_array($stack)); |
57
|
|
|
$this->assertNotEmpty($stack); |
58
|
|
|
$expected = [ |
59
|
|
|
['debug', 'pinba_get_info', []], |
60
|
|
|
]; |
61
|
|
|
$this->assertEquals($expected, $stack); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
class StopwatchEmulate extends Stopwatch |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var \ArrayObject |
72
|
|
|
*/ |
73
|
|
|
private $storage; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param \ArrayObject $storage |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
|
|
public function setStorage($storage) |
80
|
|
|
{ |
81
|
|
|
$this->storage = $storage; |
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
|
87
|
|
|
public function start(array $tags) |
88
|
|
|
{ |
89
|
|
|
return new StopwatchEventEmulate($tags, $this->storage); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
class StopwatchEventEmulate |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
/** |
97
|
|
|
* @var array |
98
|
|
|
*/ |
99
|
|
|
private $tags; |
100
|
|
|
/** |
101
|
|
|
* @var \ArrayObject |
102
|
|
|
*/ |
103
|
|
|
private $storage; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* StopwatchEventEmulate constructor. |
107
|
|
|
* @param array $tags |
108
|
|
|
* @param \ArrayObject $storage |
109
|
|
|
*/ |
110
|
|
|
public function __construct(array $tags, \ArrayObject $storage) |
111
|
|
|
{ |
112
|
|
|
$this->tags = $tags; |
113
|
|
|
$this->storage = $storage; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
public function stop() |
118
|
|
|
{ |
119
|
|
|
$this->storage['tags'] = $this->tags; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
class ApplicationEmulator extends Application |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
use Application\TwigTrait; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
class PinbaLogger extends AbstractLogger { |
|
|
|
|
131
|
|
|
|
132
|
|
|
private $logStack = []; |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Logs with an arbitrary level. |
136
|
|
|
* |
137
|
|
|
* @param mixed $level |
138
|
|
|
* @param string $message |
139
|
|
|
* @param array $context |
140
|
|
|
* |
141
|
|
|
* @return void |
142
|
|
|
*/ |
143
|
|
|
public function log($level, $message, array $context = array()) |
144
|
|
|
{ |
145
|
|
|
$this->logStack[] = [$level, $message, $context]; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
|
|
public function getLogStack() |
152
|
|
|
{ |
153
|
|
|
return $this->logStack; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
} |
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state