-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathApplicationTest.php
More file actions
177 lines (142 loc) 路 6.44 KB
/
Copy pathApplicationTest.php
File metadata and controls
177 lines (142 loc) 路 6.44 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
<?php
/** @noinspection PhpFullyQualifiedNameUsageInspection */
namespace PhpSchool\PhpWorkshopTest;
use PhpSchool\PhpWorkshop\Application;
use PhpSchool\PhpWorkshop\CommandDefinition;
use PhpSchool\PhpWorkshop\CommandRouter;
use PhpSchool\PhpWorkshop\Event\EventDispatcher;
use PhpSchool\PhpWorkshop\Exception\InvalidArgumentException;
use PhpSchool\PhpWorkshop\Exception\RuntimeException;
use PhpSchool\PhpWorkshop\Output\NullOutput;
use PhpSchool\PhpWorkshop\Output\OutputInterface;
use PhpSchool\PhpWorkshopTest\Asset\MockEventDispatcher;
use Psr\Log\LoggerInterface;
use PhpSchool\PhpWorkshop\Logger\ConsoleLogger;
class ApplicationTest extends BaseTest
{
public function testEventListenersFromLocalAndWorkshopConfigAreMerged(): void
{
$frameworkFileContent = <<<'FRAME'
<?php return [
'eventListeners' => [
'event1' => [
'entry1',
'entry2',
]
]
];
FRAME;
$localFileContent = <<<'LOCAL'
<?php return [
'eventListeners' => [
'event1' => [
'entry3',
]
]
];
LOCAL;
$localFile = $this->getTemporaryFile(uniqid($this->getName(), true), $localFileContent);
$frameworkFile = $this->getTemporaryFile(uniqid($this->getName(), true), $frameworkFileContent);
$app = new Application('Test App', $localFile);
$rm = new \ReflectionMethod($app, 'getContainer');
$rm->setAccessible(true);
$rp = new \ReflectionProperty(Application::class, 'frameworkConfigLocation');
$rp->setAccessible(true);
$rp->setValue($app, $frameworkFile);
$container = $rm->invoke($app, false);
$eventListeners = $container->get('eventListeners');
self::assertEquals(
[
'event1' => [
'entry1',
'entry2',
'entry3',
],
],
$eventListeners,
);
}
public function testExceptionIsThrownIfConfigFileDoesNotExist(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('File "not-existing-file.php" was expected to exist.');
new Application('My workshop', 'not-existing-file.php');
}
public function testExceptionIsThrownIfResultClassDoesNotExist(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Class "NotExistingClass" does not exist');
$app = new Application('My workshop', __DIR__ . '/../app/config.php');
$app->addResult(\NotExistingClass::class, \NotExistingClass::class);
}
public function testExceptionIsThrownIfResultRendererClassDoesNotExist(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Class "NotExistingClass" does not exist');
$app = new Application('My workshop', __DIR__ . '/../app/config.php');
$app->addResult(\PhpSchool\PhpWorkshop\Result\Success::class, \NotExistingClass::class);
}
public function testTearDownEventIsFiredOnApplicationException(): void
{
$configFile = $this->getTemporaryFile('config.php', '<?php return ["basePath" => __DIR__ . "../"];');
$application = new Application('Testing TearDown', $configFile);
$container = $application->configure();
$container->set('basePath', __DIR__);
$container->set(EventDispatcher::class, new MockEventDispatcher());
$container->set(OutputInterface::class, new NullOutput());
/** @var MockEventDispatcher $eventDispatcher */
$eventDispatcher = $container->get(EventDispatcher::class);
$commandRouter = $container->get(CommandRouter::class);
$commandRouter->addCommand(new CommandDefinition('Failure', [], function () {
throw new RuntimeException('We failed somewhere...');
}));
$_SERVER['argv'] = [$this->getName(), 'Failure'];
$application->run();
self::assertSame(1, $eventDispatcher->getEventDispatchCount('application.tear-down'));
}
public function testLoggingExceptionDuringTearDown(): void
{
$configFile = $this->getTemporaryFile('config.php', '<?php return ["basePath" => __DIR__ . "../"];');
$application = new Application('Testing tear down logging', $configFile);
$exception = new \Exception('Unexpected error');
$container = $application->configure();
$container->set('basePath', __DIR__);
$container->set(OutputInterface::class, new NullOutput());
$container->set(LoggerInterface::class, new MockLogger());
$container->set('eventListeners', [
'testing-failure-logging' => [
'application.tear-down' => [
static function () use ($exception) {
throw $exception;
},
],
],
]);
$commandRouter = $container->get(CommandRouter::class);
$commandRouter->addCommand(new CommandDefinition('Failure', [], function () {
throw new RuntimeException('We failed somewhere...');
}));
$application->run();
/** @var MockLogger $logger */
$logger = $container->get(LoggerInterface::class);
self::assertCount(1, $logger->messages);
self::assertSame('Unexpected error', $logger->messages[0]['message']);
self::assertSame($exception, $logger->messages[0]['context']['exception']);
}
public function testConfigureReturnsSameContainerInstance(): void
{
$configFile = $this->getTemporaryFile('config.php', '<?php return ["basePath" => __DIR__ . "../"];');
$application = new Application('Testing Configure', $configFile);
self::assertSame($application->configure(), $application->configure());
}
public function testDebugFlagSwitchesLoggerToConsoleLogger(): void
{
$configFile = $this->getTemporaryFile('config.php', '<?php return ["basePath" => __DIR__ . "../"];');
$application = new Application('My workshop', $configFile);
$container = $application->configure(true);
$container->set('phpschoolGlobalDir', $this->getTemporaryDirectory());
$container->set('appName', 'my-workshop');
$logger = $container->get(LoggerInterface::class);
self::assertInstanceOf(ConsoleLogger::class, $logger);
}
}