| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * DronePHP (http://www.dronephp.com) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @link      http://github.com/Pleets/DronePHP | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @license   http://www.dronephp.com/license | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @author    Darío Rivera <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace DroneTest\Util; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Drone\Mvc\Router; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Drone\Mvc\View; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Drone\Mvc\ModuleFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  | class SimpleMvcApplicationTest extends TestCase | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      * Tests if we can create a simple Mvc application | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      * @return null | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public function testMakingMvcApplication() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $router = new Router(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $router->addRoute([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             'AppRoute' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |                 'module'     => 'Master', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                 'controller' => 'Admin', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                 'view'       => 'index' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         // you should code the request to match /Master/Admin/index | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $router->setIdentifiers('Master', 'Admin', 'index'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $router->setClassNameBuilder(function($module, $class) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             return "\\$module\Controller\\$class"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         \Drone\Loader\ClassMap::$path = 'test-skeleton/module/Master/source'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         spl_autoload_register("Drone\Loader\ClassMap::autoload"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $router->match(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $ctrl = $router->getController(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         # inject the module dependency to the controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $router->getController()->setModule(ModuleFactory::create("Master", [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             "config"  => 'test-skeleton/module/Master/config/config.php' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         ])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $result = $router->run(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->assertSame(["message" => "Hello world!"], $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $router->addRoute([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             'AppRouteView' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 'module'     => 'Master', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 'controller' => 'Admin', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 'view'       => 'withView' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $router->setIdentifiers('Master', 'Admin', 'withView'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $router->match(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $result = $router->run(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $this->assertTrue($result instanceof View); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $result->setPath("test-skeleton/module/Master/source/view/Admin"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 75 |  |  |         $this->assertSame("<h1>Hello world!</h1>", $result->getContents()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         /*$result->setPath('test-skeleton/module/source/view'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $result->setView($ctrl->getMethod()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $result->render();*/ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 81 |  |  | } |