1+ <?php
2+
3+ use Mockery as m ;
4+ use Feather \Models \Extension ;
5+
6+ class ExtensionTest extends PHPUnit_Framework_TestCase {
7+
8+
9+ public function testExtensionsAreBootstrapped ()
10+ {
11+ list ($ app , $ dispatcher ) = $ this ->getApplicationAndDispatcher ();
12+ $ extension = new Extension (array (
13+ 'location ' => 'extension/location ' ,
14+ 'identifier ' => 'foo ' ,
15+ 'auto ' => true
16+ ));
17+ $ dispatcher ->register ($ extension );
18+
19+ $ this ->assertEquals ('success ' , $ app ['events ' ]->first ('start_test ' ));
20+ }
21+
22+
23+ public function testExtensionsCanListenForEvents ()
24+ {
25+ list ($ app , $ dispatcher ) = $ this ->getApplicationAndDispatcher ();
26+ $ extension = new Extension (array (
27+ 'location ' => 'extension/location ' ,
28+ 'identifier ' => 'foo ' ,
29+ 'auto ' => true
30+ ));
31+ $ dispatcher ->register ($ extension );
32+ $ extension ->loaded ['Feather\Extensions\extension\location\FooExtension ' ]->listen ('foobar ' , function ()
33+ {
34+ return 'barfoo ' ;
35+ });
36+ $ extension ->loaded ['Feather\Extensions\extension\location\FooExtension ' ]->listen ('barfoo ' , function ()
37+ {
38+ return 'foobar ' ;
39+ });
40+ $ this ->assertEquals ('barfoo ' , $ app ['events ' ]->first ('foobar ' ));
41+ $ this ->assertEquals ('foobar ' , $ app ['events ' ]->first ('barfoo ' ));
42+ }
43+
44+
45+ public function testExtensionsCanOverrideEvents ()
46+ {
47+ list ($ app , $ dispatcher ) = $ this ->getApplicationAndDispatcher ();
48+ $ extension = new Extension (array (
49+ 'location ' => 'extension/location ' ,
50+ 'identifier ' => 'foo ' ,
51+ 'auto ' => true
52+ ));
53+ $ dispatcher ->register ($ extension );
54+ $ extension ->loaded ['Feather\Extensions\extension\location\FooExtension ' ]->listen ('foobar ' , function ()
55+ {
56+ return 'barfoo ' ;
57+ });
58+ $ extension ->loaded ['Feather\Extensions\extension\location\FooExtension ' ]->override ('foobar ' , function ()
59+ {
60+ return 'barbar ' ;
61+ });
62+ $ this ->assertEquals ('barbar ' , $ app ['events ' ]->first ('foobar ' ));
63+ }
64+
65+
66+ public function testExtensionsCanUseMethods ()
67+ {
68+ list ($ app , $ dispatcher ) = $ this ->getApplicationAndDispatcher ();
69+ $ extension = new Extension (array (
70+ 'location ' => 'extension/location ' ,
71+ 'identifier ' => 'foo ' ,
72+ 'auto ' => true
73+ ));
74+ $ dispatcher ->register ($ extension );
75+ $ extension ->loaded ['Feather\Extensions\extension\location\FooExtension ' ]->listen ('foobar ' , 'foo ' );
76+ $ this ->assertEquals ('foomethod ' , $ app ['events ' ]->first ('foobar ' ));
77+ }
78+
79+
80+ protected function getApplicationAndDispatcher ()
81+ {
82+ $ app = new Illuminate \Container ;
83+ $ app ['events ' ] = new Illuminate \Events \Dispatcher ;
84+ $ app ['files ' ] = m::mock ('Illuminate\Filesystem ' );
85+ $ app ['files ' ]->shouldReceive ('exists ' )->once ()->andReturn (true );
86+ $ dispatcher = m::mock ('Feather\Extensions\Dispatcher[findExtensions,loadExtension] ' );
87+ $ dispatcher ->__construct ($ app ['files ' ], 'path/to ' );
88+ $ dispatcher ->setApplication ($ app );
89+ $ file = m::mock ('stdClass ' );
90+ $ file ->shouldReceive ('getBasename ' )->once ()->andReturn ('FooExtension ' );
91+ $ file ->shouldReceive ('getExtension ' )->once ()->andReturn ('php ' );
92+ $ instantiatedExtension = m::mock ('Feather\Extensions\Extension[start] ' );
93+ $ instantiatedExtension ->__construct ($ app );
94+ $ instantiatedExtension ->shouldReceive ('foo ' )->andReturn ('foomethod ' );
95+ $ instantiatedExtension ->shouldReceive ('start ' )->once ()->andReturnUsing (function () use ($ instantiatedExtension )
96+ {
97+ $ instantiatedExtension ->listen ('start_test ' , function ()
98+ {
99+ return 'success ' ;
100+ });
101+ });
102+ $ dispatcher ->shouldReceive ('findExtensions ' )->once ()->with ('path/to/extension/location ' )->andReturn (array ($ file ));
103+ $ dispatcher ->shouldReceive ('loadExtension ' )->with ('Feather\Extensions\extension\location\FooExtension ' )->andReturn ($ instantiatedExtension );
104+ return array ($ app , $ dispatcher );
105+ }
106+
107+
108+ }
0 commit comments