Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

146 řádky
3.7KB

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

Powered by TurnKey Linux.