@@ -55,4 +55,49 @@ public function down(): void {}
5555 unlink ($ file );
5656 rmdir ($ dir );
5757 }
58+
59+ public function test_migrator_skips_already_ran_migrations ()
60+ {
61+ $ dir = sys_get_temp_dir () . '/cm_db_migrations_ ' . uniqid ('' , true );
62+
63+ mkdir ($ dir );
64+
65+ $ first = '2025_01_01_000000_first_migration ' ;
66+ $ second = '2025_01_02_000000_second_migration ' ;
67+
68+ foreach ([$ first , $ second ] as $ name ) {
69+ file_put_contents ($ dir . DIRECTORY_SEPARATOR . $ name . '.php ' , <<<PHP
70+ <?php
71+
72+ use Codemonster\\Database\\Migrations\\Migration;
73+
74+ return new class extends Migration {
75+ public function up(): void {}
76+ public function down(): void {}
77+ };
78+ PHP );
79+ }
80+
81+ $ conn = new FakeConnection ();
82+ $ conn ->migrations = [$ first ];
83+
84+ $ repo = new MigrationRepository ($ conn );
85+
86+ /** @var MigrationPathResolver $paths */
87+ $ paths = $ this ->createStub (MigrationPathResolver::class);
88+ $ paths ->method ('getPaths ' )->willReturn ([$ dir ]);
89+
90+ $ migrator = new Migrator ($ repo , $ conn , $ paths );
91+
92+ $ ran = $ migrator ->migrate ();
93+
94+ $ this ->assertSame ([$ second ], $ ran );
95+ $ this ->assertSame ([$ first , $ second ], $ conn ->migrations );
96+
97+ foreach ([$ first , $ second ] as $ name ) {
98+ unlink ($ dir . DIRECTORY_SEPARATOR . $ name . '.php ' );
99+ }
100+
101+ rmdir ($ dir );
102+ }
58103}
0 commit comments