diff --git a/.gitignore b/.gitignore index a1149f2..4900578 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ vendor/* composer.lock -.idea/* \ No newline at end of file +.idea/* +.phpunit.result.cache +storage/* diff --git a/composer.json b/composer.json index fcc0594..9aa17b0 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,11 @@ "ext-json": "*" }, "require-dev": { - "laravel/framework": "^5.0|^6.0|^7.0|^8.0|^9.0|^10.0" + "laravel/framework": "^5.0|^6.0|^7.0|^8.0|^9.0|^10.0", + "orchestra/testbench": "^8.5", + "phpunit/phpunit": "^10.1", + "ext-gd": "*", + "ext-sqlite3": "*" }, "autoload": { "psr-4": { diff --git a/examples/database/database.sqlite b/examples/database/database.sqlite new file mode 100644 index 0000000..e69de29 diff --git a/examples/tests/TestCase.php b/examples/tests/TestCase.php new file mode 100644 index 0000000..c9b57e6 --- /dev/null +++ b/examples/tests/TestCase.php @@ -0,0 +1,56 @@ +> + */ + protected function getPackageProviders($app): array + { + return [ + 'Luezoid\Laravelcore\CoreServiceProvider', + ]; + } + + public function defineEnvironment($app) + { + tap($app->make('config'), function (Repository $config) { + $config->set('database.default', 'test'); + $config->set('database.connections.test', [ + 'driver' => 'sqlite', + 'database' => ':memory:', + 'prefix' => '', + ]); + }); + } + + protected function defineRoutes($router) + { + Route::resource('api/minions', MinionController::class, ['parameters' => ['minions' => 'id']]); + Route::post('api/files',[FileController::class, 'store']); + } + + protected function defineDatabaseMigrations() + { + $this->loadMigrationsFrom(__DIR__ . '/../migrations'); + $this->artisan('migrate', ['--database' => 'test'])->run(); + } + + +} diff --git a/examples/tests/suite/DeleteAPISuccessTest.php b/examples/tests/suite/DeleteAPISuccessTest.php new file mode 100644 index 0000000..d50ef97 --- /dev/null +++ b/examples/tests/suite/DeleteAPISuccessTest.php @@ -0,0 +1,43 @@ + 'Stuart', + 'totalEyes' => 2, + 'favouriteSound' => 'Grrrrrrrrrrr', + 'hasHairs' => true, + ]; + // Create a minion. + $this->postJson('/api/minions', $payload); + + // Make the request. + $response = $this->deleteJson('/api/minions/1'); + // Assert that the response is successful. + $response->assertOk(); + // Assert that the minion is deleted. + $response->assertJson([ + 'message' =>"Resource deleted successfully", + 'data' => [ + 'id' => 1, + 'name' => 'Stuart', + 'totalEyes' => 2, + 'favouriteSound' => 'Grrrrrrrrrrr', + 'hasHairs' => true + ], + 'type' => null, + ]); + } +} diff --git a/examples/tests/suite/FileAPISuccessTest.php b/examples/tests/suite/FileAPISuccessTest.php new file mode 100644 index 0000000..f382d4a --- /dev/null +++ b/examples/tests/suite/FileAPISuccessTest.php @@ -0,0 +1,58 @@ +image('test-image.jpg'); // Create a fake test file + + $this->app->bind(IFile::class, function ($app) { + if (config('file.is_local')) { + return $app->make(LocalFileUploadService::class); + } + return $app->make(SaveFileToS3Service::class); + }); + + $response = $this->post('/api/files', [ + 'file' => $file, + 'type' => 'EXAMPLE', + ]); + + $response->assertStatus(200); // Assert that the response has a status code of 200 + // Assert the JSON structure of the response + $response->assertJsonStructure([ + 'message', + 'data' => [ + 'type', + 'name', + 'localPath', + 's3Key', + 'updatedAt', + 'createdAt', + 'id', + 'url', + ], + 'type', + ]); + } +} diff --git a/examples/tests/suite/GetAPISuccessTest.php b/examples/tests/suite/GetAPISuccessTest.php new file mode 100644 index 0000000..61395b8 --- /dev/null +++ b/examples/tests/suite/GetAPISuccessTest.php @@ -0,0 +1,44 @@ + 'Stuart', + 'totalEyes' => 2, + 'favouriteSound' => 'Grrrrrrrrrrr', + 'hasHairs' => true, + ]; + // Create a minion. + $this->postJson('/api/minions', $payload); + + // Make the request. + $response = $this->getJson('/api/minions/1'); + + // Assert that the response is successful. + $response->assertOk(); + + // Assert that the response data is correct. + $response->assertJson([ + 'message' => null, + 'data' => [ + 'id' => 1, + 'name' => 'Stuart', + 'totalEyes' => 2, + 'favouriteSound' => 'Grrrrrrrrrrr', + 'hasHairs' => true + ], + 'type' => null, + ]); + } +} diff --git a/examples/tests/suite/IndexAPISuccessTest.php b/examples/tests/suite/IndexAPISuccessTest.php new file mode 100644 index 0000000..75720be --- /dev/null +++ b/examples/tests/suite/IndexAPISuccessTest.php @@ -0,0 +1,51 @@ + 'Stuart', + 'totalEyes' => 2, + 'favouriteSound' => 'Grrrrrrrrrrr', + 'hasHairs' => true, + ]; + // Create a minion. + $this->postJson('/api/minions', $payload); + + // Make the request. + $response = $this->getJson('/api/minions'); + + $response->assertOk(); + + // Assert that the response data is correct. + $response->assertJson([ + 'message' => null, + 'data' => [ + 'items' => [ + [ + 'id' => 1, + 'name' => 'Stuart', + 'totalEyes' => 2, + 'favouriteSound' => 'Grrrrrrrrrrr', + 'hasHairs' => true + ] + ], + 'page' => 1, + 'total' => 1, + 'pages' => 1, + 'perpage' => 15, + ], + 'type' => null, + ]); + } +} diff --git a/examples/tests/suite/PostAPISuccessTest.php b/examples/tests/suite/PostAPISuccessTest.php new file mode 100644 index 0000000..9273305 --- /dev/null +++ b/examples/tests/suite/PostAPISuccessTest.php @@ -0,0 +1,36 @@ + 'Stuart', + 'totalEyes' => 2, + 'favouriteSound' => 'Grrrrrrrrrrr', + 'hasHairs' => true, + ]; + + $response = $this->postJson('/api/minions', $payload); + $response->assertStatus(200); + $response->assertJson([ + 'message' => 'Resource Created successfully', + 'data' => [ + 'name' => 'Stuart', + 'totalEyes' => 2, + 'favouriteSound' => 'Grrrrrrrrrrr', + 'hasHairs' => true, + 'id' => 1, + ], + 'type' => null, + ]); + } +} diff --git a/examples/tests/suite/PutAPISuccessTest.php b/examples/tests/suite/PutAPISuccessTest.php new file mode 100644 index 0000000..5274cda --- /dev/null +++ b/examples/tests/suite/PutAPISuccessTest.php @@ -0,0 +1,52 @@ +postJson('/api/minions', [ + 'name' => 'Stuart', + 'totalEyes' => 2, + 'favouriteSound' => 'Grrrrrrrrrrr', + 'hasHairs' => true, + ]); + + // Prepare the request payload. + $payload = [ + 'name' => 'Stuart', + 'totalEyes' => 2, + 'favouriteSound' => 'Hrrrrrrrrrrr', + 'hasHairs' => true, + ]; + + // Make the request. + $response = $this->putJson('/api/minions/1', $payload); + + // Assert that the response is successful. + $response->assertOk(); + + // Assert that the response data is correct. + $response->assertJson([ + 'message' => 'Resource Updated successfully', + 'data' => [ + 'id' => 1, + 'name' => 'Stuart', + 'totalEyes' => 2, + 'favouriteSound' => 'Hrrrrrrrrrrr', + 'hasHairs' => true + ], + 'type' => null, + ]); + } +} diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..2ebd8fb --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,23 @@ + + + + + ./examples/tests/suite + + + + + + + + + + + + + +