| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Test; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use ByJG\DbMigration\Migration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use ByJG\Util\Uri; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class MigrationTest extends \PHPUnit\Framework\TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |      * @var Migration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     protected $object; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     public function setUp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         $this->object = new Migration(new Uri('mysql://localhost'), __DIR__ . '/dirstructure'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     public function tearDown() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $this->object = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public function testGetBaseSql() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $base = $this->object->getBaseSql(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $this->assertEquals(__DIR__ . '/dirstructure/base.sql', $base); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @expectedException \ByJG\DbMigration\Exception\InvalidMigrationFile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function testGetBaseSqlNotFound() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->object = new Migration(new Uri('mysql://localhost'), __DIR__ . '/invalid'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->object->getBaseSql(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     public function testGetBaseSqlNotFoundAndNotRequired() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->object = new Migration(new Uri('mysql://localhost'), __DIR__ . '/invalid', false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->object->getBaseSql(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $this->assertTrue(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     public function testGetMigrationSql1() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $version = $this->object->getMigrationSql(1, 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->assertEquals(__DIR__ . '/dirstructure/migrations/up/00001.sql', $version); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     public function testGetMigrationSql2() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $version = $this->object->getMigrationSql(2, 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $this->assertEquals(__DIR__ . '/dirstructure/migrations/up/00002.sql', $version); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     public function testGetMigrationSql3() | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         $version = $this->object->getMigrationSql(12, 1); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         $this->assertEquals(__DIR__ . '/dirstructure/migrations/up/00012-dev.sql', $version); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @expectedException \ByJG\DbMigration\Exception\InvalidMigrationFile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @expectedExceptionMessage version number '13' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     public function testGetMigrationSql4() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $this->object->getMigrationSql(13, 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     public function testGetMigrationSqlDown1() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $version = $this->object->getMigrationSql(1, -1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $this->assertEquals(__DIR__ . '/dirstructure/migrations/down/00001.sql', $version); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     public function testGetMigrationSqlDown2() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $version = $this->object->getMigrationSql(2, -1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $this->assertEquals(__DIR__ . '/dirstructure/migrations/down/00002.sql', $version); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     public function testGetMigrationSqlDown3() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $version = $this->object->getMigrationSql(12, -1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $this->assertEquals(__DIR__ . '/dirstructure/migrations/down/00012-dev.sql', $version); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @expectedException \ByJG\DbMigration\Exception\InvalidMigrationFile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @expectedExceptionMessage version number '13' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     public function testGetMigrationSqlDown4() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $this->object->getMigrationSql(13, -1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     public function testGetFileContent_NonExists() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 "file" => "non-existent", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 "description" => "no description provided. Pro tip: use `-- @description:` to define one.", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                 "exists" => false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                 "checksum" => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 "content" => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             $this->object->getFileContent("non-existent") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 114 |  | View Code Duplication |     public function testGetFileContent_1() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                 "file" => __DIR__ . '/dirstructure/migrations/up/00001.sql', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 "description" => "this is a test", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                 "exists" => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                 "checksum" => "b937afa57e363c9244fa30844dd11d312694f697", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                 "content" => "-- @description: this is a test\n\nselect * from mysql.users;\n", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             $this->object->getFileContent(__DIR__ . '/dirstructure/migrations/up/00001.sql') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 128 |  | View Code Duplication |     public function testGetFileContent_2() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                 "file" => __DIR__ . '/dirstructure/migrations/up/00002.sql', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                 "description" => "another test", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                 "exists" => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                 "checksum" => "fd8ab8176291c2dcbf0d91564405e0f98f0cd77e", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                 "content" => "--   @description:  another test\n\nselect * from dual;", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             $this->object->getFileContent(__DIR__ . '/dirstructure/migrations/up/00002.sql') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 142 |  | View Code Duplication |     public function testGetFileContent_3() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         $this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |                 "file" => __DIR__ . '/dirstructure/migrations/up/00003.sql', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |                 "description" => "no description provided. Pro tip: use `-- @description:` to define one.", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |                 "exists" => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |                 "checksum" => "73faaa68e2f60c11e75a9ccc18528e0ffa15127a", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |                 "content" => "select something from sometable;", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |             $this->object->getFileContent(__DIR__ . '/dirstructure/migrations/up/00003.sql') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 155 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 156 |  |  |  | 
            
                        
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: