55namespace PhpSchool \PhpWorkshopTest ;
66
77use PhpSchool \PhpWorkshop \Application ;
8- use PHPUnit \Framework \TestCase ;
8+ use PhpSchool \PhpWorkshop \CommandDefinition ;
9+ use PhpSchool \PhpWorkshop \CommandRouter ;
10+ use PhpSchool \PhpWorkshop \Event \EventDispatcher ;
911use PhpSchool \PhpWorkshop \Exception \InvalidArgumentException ;
10-
11- class ApplicationTest extends TestCase
12+ use PhpSchool \PhpWorkshop \Exception \RuntimeException ;
13+ use PhpSchool \PhpWorkshop \Output \NullOutput ;
14+ use PhpSchool \PhpWorkshop \Output \OutputInterface ;
15+ use PhpSchool \PhpWorkshopTest \Asset \MockEventDispatcher ;
16+ use Psr \Log \LoggerInterface ;
17+ use Psr \Log \NullLogger ;
18+
19+ class ApplicationTest extends BaseTest
1220{
1321 public function testEventListenersFromLocalAndWorkshopConfigAreMerged (): void
1422 {
23+ $ frameworkFileContent = <<<'FRAME'
24+ <?php return [
25+ 'eventListeners' => [
26+ 'event1' => [
27+ 'entry1',
28+ 'entry2',
29+ ]
30+ ]
31+ ];
32+ FRAME;
33+
34+ $ localFileContent = <<<'LOCAL'
35+ <?php return [
36+ 'eventListeners' => [
37+ 'event1' => [
38+ 'entry3',
39+ ]
40+ ]
41+ ];
42+ LOCAL;
1543
16- $ frameworkFileContent = '<?php return [ ' ;
17- $ frameworkFileContent .= " 'eventListeners' => [ " ;
18- $ frameworkFileContent .= " 'event1' => [ " ;
19- $ frameworkFileContent .= " 'entry1', " ;
20- $ frameworkFileContent .= " 'entry2', " ;
21- $ frameworkFileContent .= ' ] ' ;
22- $ frameworkFileContent .= ' ] ' ;
23- $ frameworkFileContent .= ']; ' ;
24-
25- $ localFileContent = '<?php return [ ' ;
26- $ localFileContent .= " 'eventListeners' => [ " ;
27- $ localFileContent .= " 'event1' => [ " ;
28- $ localFileContent .= " 'entry3', " ;
29- $ localFileContent .= ' ] ' ;
30- $ localFileContent .= ' ] ' ;
31- $ localFileContent .= ']; ' ;
32-
33- $ localFile = sprintf ('%s/%s ' , sys_get_temp_dir (), uniqid ($ this ->getName (), true ));
34- $ frameworkFile = sprintf ('%s/%s ' , sys_get_temp_dir (), uniqid ($ this ->getName (), true ));
35- file_put_contents ($ frameworkFile , $ frameworkFileContent );
36- file_put_contents ($ localFile , $ localFileContent );
44+ $ localFile = $ this ->getTemporaryFile (uniqid ($ this ->getName (), true ), $ localFileContent );
45+ $ frameworkFile = $ this ->getTemporaryFile (uniqid ($ this ->getName (), true ), $ frameworkFileContent );
3746
3847 $ app = new Application ('Test App ' , $ localFile );
3948
@@ -48,7 +57,7 @@ public function testEventListenersFromLocalAndWorkshopConfigAreMerged(): void
4857
4958 $ eventListeners = $ container ->get ('eventListeners ' );
5059
51- $ this -> assertEquals (
60+ self :: assertEquals (
5261 [
5362 'event1 ' => [
5463 'entry1 ' ,
@@ -85,4 +94,76 @@ public function testExceptionIsThrownIfResultRendererClassDoesNotExist(): void
8594 $ app = new Application ('My workshop ' , __DIR__ . '/../app/config.php ' );
8695 $ app ->addResult (\PhpSchool \PhpWorkshop \Result \Success::class, \NotExistingClass::class);
8796 }
97+
98+ public function testTearDownEventIsFiredOnApplicationException (): void
99+ {
100+ $ configFile = $ this ->getTemporaryFile ('config.php ' , '<?php return []; ' );
101+ $ application = new Application ('Testing TearDown ' , $ configFile );
102+
103+ $ container = $ application ->configure ();
104+ $ container ->set ('basePath ' , __DIR__ );
105+ $ container ->set (EventDispatcher::class, new MockEventDispatcher ());
106+ $ container ->set (OutputInterface::class, new NullOutput ());
107+
108+ /** @var MockEventDispatcher $eventDispatcher */
109+ $ eventDispatcher = $ container ->get (EventDispatcher::class);
110+
111+ $ commandRouter = $ container ->get (CommandRouter::class);
112+ $ commandRouter ->addCommand (new CommandDefinition ('Failure ' , [], function () {
113+ throw new RuntimeException ('We failed somewhere... ' );
114+ }));
115+
116+ $ _SERVER ['argv ' ] = [$ this ->getName (), 'Failure ' ];
117+
118+ $ application ->run ();
119+
120+ self ::assertSame (1 , $ eventDispatcher ->getEventDispatchCount ('application.tear-down ' ));
121+ }
122+
123+ public function testLoggingExceptionDuringTearDown (): void
124+ {
125+ $ configFile = $ this ->getTemporaryFile ('config.php ' , '<?php return []; ' );
126+ $ application = new Application ('Testing tear down logging ' , $ configFile );
127+ $ exception = new \Exception ('Unexpected error ' );
128+
129+ $ container = $ application ->configure ();
130+ $ container ->set ('basePath ' , __DIR__ );
131+ $ container ->set (OutputInterface::class, new NullOutput ());
132+ $ container ->set (LoggerInterface::class, new MockLogger ());
133+ $ container ->set ('eventListeners ' , [
134+ 'testing-failure-logging ' => [
135+ 'application.tear-down ' => [
136+ static function () use ($ exception ) {
137+ throw $ exception ;
138+ },
139+ ]
140+ ]
141+ ]);
142+
143+ $ commandRouter = $ container ->get (CommandRouter::class);
144+ $ commandRouter ->addCommand (new CommandDefinition ('Failure ' , [], function () {
145+ throw new RuntimeException ('We failed somewhere... ' );
146+ }));
147+
148+ $ application ->run ();
149+
150+ /** @var MockLogger $logger */
151+ $ logger = $ container ->get (LoggerInterface::class);
152+ self ::assertCount (1 , $ logger ->messages );
153+ self ::assertSame ('Unexpected error ' , $ logger ->messages [0 ]['message ' ]);
154+ self ::assertSame ($ exception , $ logger ->messages [0 ]['context ' ]['exception ' ]);
155+ }
156+
157+ public function testConfigureReturnsSameContainerInstance (): void
158+ {
159+ $ configFile = $ this ->getTemporaryFile ('config.php ' , '<?php return []; ' );
160+ $ application = new Application ('Testing Configure ' , $ configFile );
161+
162+ self ::assertSame ($ application ->configure (), $ application ->configure ());
163+ }
164+
165+ public function tearDown (): void
166+ {
167+ parent ::tearDown ();
168+ }
88169}
0 commit comments