選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

114 行
2.9KB

  1. <?php
  2. declare(strict_types=1);
  3. require_once __DIR__ . '/../vendor/autoload.php';
  4. use Core\App;
  5. use Core\Database;
  6. use Core\Dispatcher;
  7. use Core\MigrationManager;
  8. use Core\Request;
  9. use Core\Router;
  10. $tempMigrationPath = sys_get_temp_dir() . '/mvc_migrations_' . uniqid('', true);
  11. mkdir($tempMigrationPath, 0777, true);
  12. $migrationFile = $tempMigrationPath . '/20260509_120000_create_projects_table.php';
  13. file_put_contents($migrationFile, <<<'PHP'
  14. <?php
  15. declare(strict_types=1);
  16. use Core\Database;
  17. use Core\Migration;
  18. return new class extends Migration
  19. {
  20. public function up(Database $database): void
  21. {
  22. $database->execute('CREATE TABLE projects (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(100) NOT NULL)');
  23. }
  24. public function down(Database $database): void
  25. {
  26. $database->execute('DROP TABLE IF EXISTS projects');
  27. }
  28. };
  29. PHP
  30. );
  31. $memoryDatabase = new Database([
  32. 'dsn' => 'sqlite::memory:',
  33. 'options' => [
  34. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  35. PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
  36. ],
  37. ]);
  38. $migrationManager = new MigrationManager($memoryDatabase, $tempMigrationPath);
  39. $ran = $migrationManager->runPending();
  40. if ($ran !== ['20260509_120000_create_projects_table.php']) {
  41. echo "FAIL: migration manager did not apply the expected migration\n";
  42. exit(1);
  43. }
  44. $projectTable = $memoryDatabase->first("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'projects'");
  45. if ($projectTable === null) {
  46. echo "FAIL: migration up() did not create the projects table\n";
  47. exit(1);
  48. }
  49. $rolledBack = $migrationManager->rollback();
  50. if ($rolledBack !== ['20260509_120000_create_projects_table.php']) {
  51. echo "FAIL: migration manager did not roll back the expected migration\n";
  52. exit(1);
  53. }
  54. $projectTableAfterRollback = $memoryDatabase->first("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'projects'");
  55. if ($projectTableAfterRollback !== null) {
  56. echo "FAIL: migration down() did not remove the projects table\n";
  57. exit(1);
  58. }
  59. $createdMigrationPath = $migrationManager->make('create_tasks_table');
  60. if (!file_exists($createdMigrationPath)) {
  61. echo "FAIL: migration manager did not create a migration file\n";
  62. exit(1);
  63. }
  64. $router = new Router();
  65. $app = new App();
  66. (new MigrationManager(database(), __DIR__ . '/../database/migrations'))->runPending();
  67. require_once __DIR__ . '/../routes/web.php';
  68. $router->get('/hello/{name}', function (string $name) {
  69. return 'Hello, ' . $name;
  70. });
  71. $request = new Request([], [], [
  72. 'REQUEST_METHOD' => 'GET',
  73. 'REQUEST_URI' => '/hello/Daniel',
  74. ]);
  75. $response = (new Dispatcher($router, $app))->dispatch($request);
  76. if ($response->status() !== 200) {
  77. echo "FAIL: expected status 200\n";
  78. exit(1);
  79. }
  80. if ($response->content() !== 'Hello, Daniel') {
  81. echo "FAIL: unexpected response content\n";
  82. exit(1);
  83. }
  84. echo "PASS: migration manager and route dispatch work\n";

Powered by TurnKey Linux.