| 1 |  |  | <?php | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace ReddogsTest\Doctrine\Migrations; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Interop\Container\ContainerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Doctrine\DBAL\Migrations\Tools\Console\Command\GenerateCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Doctrine\ORM\EntityManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Doctrine\DBAL\Connection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Doctrine\DBAL\Migrations\Tools\Console\Command\MigrateCommand as MigrateMigrationsCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Doctrine\DBAL\Migrations\Tools\Console\Command\StatusCommand as StatusMigrationsCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Reddogs\Doctrine\Migrations\StatusCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Reddogs\Doctrine\Migrations\MigrateCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Reddogs\Doctrine\Migrations\CommandFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | require_once __DIR__ . '/_files/TestCommandImplementation.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class CommandFactoryTest extends \PHPUnit_Framework_TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     protected function setUp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $this->factory = new CommandFactory(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     public function testCreateService() | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         $factory = $this->getMockBuilder(CommandFactory::class) | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |                         ->setMethods(['getMigrationsCommand']) | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |                         ->getMock(); | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         $generateCommand = new GenerateCommand(); | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         $factory->expects($this->once()) | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |                 ->method('getMigrationsCommand') | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |                 ->with($this->equalTo(TestCommandImplementation::class)) | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |                 ->will($this->returnValue($generateCommand)); | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         $container = $this->getMockBuilder(ContainerInterface::class) | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |                           ->setMethods(['get', 'has']) | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |                           ->getMock(); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         $entityManager = $this->createMock(EntityManager::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         $connection = $this->createMock(Connection::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         $entityManager->expects($this->once()) | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |                       ->method('getConnection') | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |                       ->will($this->returnValue($connection)); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $config = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |             'doctrine' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |                 'reddogs_doctrine_migrations' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |                     'testmodule' => ['test'] | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |                 ] | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |             ] | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         $container->expects($this->at(0)) | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |                   ->method('get') | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |                   ->with($this->equalTo('config')) | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |                   ->will($this->returnValue($config)); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         $container->expects($this->at(1)) | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |                   ->method('get') | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |                   ->with($this->equalTo(EntityManager::class)) | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |                   ->will($this->returnValue($entityManager)); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         $service = $factory->__invoke($container, TestCommandImplementation::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         $this->assertInstanceOf(TestCommandImplementation::class, $service); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public function testGetMigrationsCommand() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $this->assertInstanceOf(StatusMigrationsCommand::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                                 $this->factory->getMigrationsCommand(StatusCommand::class)); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 70 |  |  | } | 
            
                        
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.