You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
3.8KB

  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. /* Employee routes belong to the framework example and are not part of KCI Kanban.
  85. $employeePage = (new Dispatcher($router, $app))->dispatch(new Request([], [], [
  86. 'REQUEST_METHOD' => 'GET',
  87. 'REQUEST_URI' => '/employees',
  88. ]));
  89. if ($employeePage->status() !== 200) {
  90. echo "FAIL: expected employee page status 200\n";
  91. exit(1);
  92. }
  93. if (strpos($employeePage->content(), 'Add Employee') === false) {
  94. echo "FAIL: employee page did not render form content\n";
  95. exit(1);
  96. }
  97. $employeeData = (new Dispatcher($router, $app))->dispatch(new Request([
  98. 'search' => '',
  99. ], [], [
  100. 'REQUEST_METHOD' => 'GET',
  101. 'REQUEST_URI' => '/employees/data',
  102. ]));
  103. if ($employeeData->status() !== 200) {
  104. echo "FAIL: expected employee data status 200\n";
  105. exit(1);
  106. }
  107. if (strpos($employeeData->content(), '[') === false) {
  108. echo "FAIL: employee data endpoint did not return JSON array content\n";
  109. exit(1);
  110. }
  111. */
  112. echo "PASS: migration manager and route dispatch work\n";

Powered by TurnKey Linux.