|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spiral\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Monolog\Handler\NullHandler; |
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
|
7
|
|
|
use Psr\Log\LoggerInterface; |
|
8
|
|
|
use Psr\Log\LoggerTrait; |
|
9
|
|
|
use Psr\Log\LogLevel; |
|
10
|
|
|
use Spiral\Core\Traits\SharedTrait; |
|
11
|
|
|
use Spiral\Database\Builders\SelectQuery; |
|
12
|
|
|
use Spiral\Debug\Snapshot; |
|
13
|
|
|
use Spiral\Snapshotter\Bootloaders\FileSnapshotterBootloader; |
|
14
|
|
|
use Spiral\Snapshotter\DelegateSnapshot; |
|
15
|
|
|
use Spiral\Snapshotter\AggregationHandler\Database\SnapshotRecord; |
|
16
|
|
|
use Spiral\Snapshotter\AggregationHandler; |
|
17
|
|
|
use Spiral\Snapshotter\FileHandler; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @property \Spiral\Core\MemoryInterface $memory |
|
21
|
|
|
* @property \Spiral\Core\ContainerInterface $container |
|
22
|
|
|
* @property \Spiral\Debug\LogsInterface $logs |
|
23
|
|
|
* @property \Spiral\Http\HttpDispatcher $http |
|
24
|
|
|
* @property \Spiral\Console\ConsoleDispatcher $console |
|
25
|
|
|
* @property \Spiral\Console\ConsoleDispatcher $commands |
|
26
|
|
|
* @property \Spiral\Files\FilesInterface $files |
|
27
|
|
|
* @property \Spiral\Tokenizer\TokenizerInterface $tokenizer |
|
28
|
|
|
* @property \Spiral\Tokenizer\ClassesInterface $locator |
|
29
|
|
|
* @property \Spiral\Tokenizer\InvocationsInterface $invocationLocator |
|
30
|
|
|
* @property \Spiral\Views\ViewManager $views |
|
31
|
|
|
* @property \Spiral\Translator\Translator $translator |
|
32
|
|
|
* @property \Spiral\Database\DatabaseManager $dbal |
|
33
|
|
|
* @property \Spiral\ORM\ORM $orm |
|
34
|
|
|
* @property \Spiral\Encrypter\EncrypterInterface $encrypter |
|
35
|
|
|
* @property \Spiral\Database\Entities\Database $db |
|
36
|
|
|
* @property \Spiral\Http\Cookies\CookieQueue $cookies |
|
37
|
|
|
* @property \Spiral\Http\Routing\RouterInterface $router |
|
38
|
|
|
* @property \Spiral\Pagination\PaginatorsInterface $paginators |
|
39
|
|
|
* @property \Psr\Http\Message\ServerRequestInterface $request |
|
40
|
|
|
* @property \Spiral\Http\Request\InputManager $input |
|
41
|
|
|
* @property \Spiral\Http\Response\ResponseWrapper $response |
|
42
|
|
|
* @property \Spiral\Http\Routing\RouteInterface $route |
|
43
|
|
|
* @property \Spiral\Security\PermissionsInterface $permissions |
|
44
|
|
|
* @property \Spiral\Security\RulesInterface $rules |
|
45
|
|
|
* @property \Spiral\Security\ActorInterface $actor |
|
46
|
|
|
* @property \Spiral\Session\SessionInterface $session |
|
47
|
|
|
*/ |
|
48
|
|
|
abstract class BaseTest extends TestCase |
|
49
|
|
|
{ |
|
50
|
|
|
use SharedTrait; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var TestApplication |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $app; |
|
56
|
|
|
|
|
57
|
|
|
public function setUp() |
|
58
|
|
|
{ |
|
59
|
|
|
$root = __DIR__ . '/-app-/'; |
|
60
|
|
|
$this->app = TestApplication::init( |
|
|
|
|
|
|
61
|
|
|
[ |
|
62
|
|
|
'root' => $root, |
|
63
|
|
|
'libraries' => dirname(__DIR__) . '/vendor/', |
|
64
|
|
|
'application' => $root, |
|
65
|
|
|
'framework' => dirname(__DIR__) . '/source/', |
|
66
|
|
|
'runtime' => $root . 'runtime/', |
|
67
|
|
|
'cache' => $root . 'runtime/cache/', |
|
68
|
|
|
], |
|
69
|
|
|
null, |
|
70
|
|
|
null, |
|
71
|
|
|
false |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
|
|
//Monolog love to write to CLI when no handler set |
|
75
|
|
|
$this->app->logs->debugHandler(new NullHandler()); |
|
76
|
|
|
$this->app->container->bind('factory', $this->app->container); |
|
77
|
|
|
|
|
78
|
|
|
$files = $this->app->files; |
|
79
|
|
|
|
|
80
|
|
|
//Ensure runtime is clean |
|
81
|
|
|
foreach ($files->getFiles($this->app->directory('runtime')) as $filename) { |
|
82
|
|
|
//If exception is thrown here this will mean that application wasn't correctly |
|
83
|
|
|
//destructed and there is open resources kept |
|
84
|
|
|
$files->delete($filename); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$builder = $this->orm->schemaBuilder(true); |
|
88
|
|
|
$builder->renderSchema(); |
|
89
|
|
|
$builder->pushSchema(); |
|
90
|
|
|
$this->orm->setSchema($builder); |
|
91
|
|
|
|
|
92
|
|
|
if ($this->app->getEnvironment()->get('DEBUG')) { |
|
93
|
|
|
$this->app->db->getDriver()->setLogger(new class implements LoggerInterface |
|
94
|
|
|
{ |
|
95
|
|
|
use LoggerTrait; |
|
96
|
|
|
|
|
97
|
|
|
public function log($level, $message, array $context = []) |
|
98
|
|
|
{ |
|
99
|
|
|
if ($level == LogLevel::ERROR) { |
|
100
|
|
|
echo " \n! \033[31m" . $message . "\033[0m"; |
|
101
|
|
|
} elseif ($level == LogLevel::ALERT) { |
|
102
|
|
|
echo " \n! \033[35m" . $message . "\033[0m"; |
|
103
|
|
|
} elseif (strpos($message, 'PRAGMA') === 0) { |
|
104
|
|
|
echo " \n> \033[34m" . $message . "\033[0m"; |
|
105
|
|
|
} else { |
|
106
|
|
|
if (strpos($message, 'SELECT') === 0) { |
|
107
|
|
|
echo " \n> \033[32m" . $message . "\033[0m"; |
|
108
|
|
|
} else { |
|
109
|
|
|
echo " \n> \033[33m" . $message . "\033[0m"; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
}); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
clearstatcache(); |
|
117
|
|
|
|
|
118
|
|
|
//Open application scope |
|
119
|
|
|
TestApplication::shareContainer($this->app->container); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* This method performs full destroy of spiral environment. |
|
124
|
|
|
*/ |
|
125
|
|
|
public function tearDown() |
|
126
|
|
|
{ |
|
127
|
|
|
\Mockery::close(); |
|
128
|
|
|
|
|
129
|
|
|
TestApplication::shareContainer(null); |
|
130
|
|
|
|
|
131
|
|
|
//Forcing destruction |
|
132
|
|
|
$this->app = null; |
|
133
|
|
|
|
|
134
|
|
|
gc_collect_cycles(); |
|
135
|
|
|
clearstatcache(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @return \Spiral\Core\ContainerInterface |
|
140
|
|
|
*/ |
|
141
|
|
|
protected function iocContainer() |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->app->container; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param string $message |
|
148
|
|
|
* @param int $code |
|
149
|
|
|
* @return Snapshot |
|
150
|
|
|
*/ |
|
151
|
|
|
protected function makeSnapshot(string $message, int $code): Snapshot |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->factory->make(Snapshot::class, [ |
|
|
|
|
|
|
154
|
|
|
'exception' => new \Error($message, $code) |
|
155
|
|
|
]); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param Snapshot $snapshot |
|
160
|
|
|
* @param bool $report |
|
161
|
|
|
* @return SnapshotRecord |
|
162
|
|
|
*/ |
|
163
|
|
|
protected function handleSnapshot(Snapshot $snapshot, bool $report = true): SnapshotRecord |
|
164
|
|
|
{ |
|
165
|
|
|
/** @var DelegateSnapshot $delegate */ |
|
166
|
|
|
$delegate = $this->factory->make(DelegateSnapshot::class, [ |
|
|
|
|
|
|
167
|
|
|
'exception' => $snapshot->getException(), |
|
168
|
|
|
'handler' => bind(AggregationHandler::class) |
|
169
|
|
|
]); |
|
170
|
|
|
|
|
171
|
|
|
if ($report) { |
|
172
|
|
|
$delegate->report(); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
return $this->orm->source(SnapshotRecord::class)->findOne( |
|
176
|
|
|
[], |
|
177
|
|
|
['id' => SelectQuery::SORT_DESC] |
|
178
|
|
|
); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @param Snapshot $snapshot |
|
183
|
|
|
* @param bool $report |
|
184
|
|
|
*/ |
|
185
|
|
|
protected function handleFileSnapshot(Snapshot $snapshot, bool $report = true) |
|
186
|
|
|
{ |
|
187
|
|
|
$this->app->getBootloader()->bootload([FileSnapshotterBootloader::class]); |
|
188
|
|
|
|
|
189
|
|
|
/** @var DelegateSnapshot $delegate */ |
|
190
|
|
|
$delegate = $this->factory->make(DelegateSnapshot::class, [ |
|
|
|
|
|
|
191
|
|
|
'exception' => $snapshot->getException() |
|
192
|
|
|
]); |
|
193
|
|
|
|
|
194
|
|
|
if ($report) { |
|
195
|
|
|
$delegate->report(); |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..