From 9fdae62355ae3cc87b319fee7bd4fd5510c4eef0 Mon Sep 17 00:00:00 2001 From: Aayush Vijay <vijay.aayush26@gmail.com> Date: Tue, 23 May 2023 16:40:34 +0530 Subject: [PATCH 1/6] Testing Cases Initialisation using TestBench --- .phpunit.result.cache | 1 + composer.json | 4 ++- examples/database/database.sqlite | 0 examples/tests/TestCase.php | 32 ++++++++++++++++++++ examples/tests/suite/PostAPISuccessTest.php | 33 +++++++++++++++++++++ phpunit.xml | 31 +++++++++++++++++++ 6 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 .phpunit.result.cache create mode 100644 examples/database/database.sqlite create mode 100644 examples/tests/TestCase.php create mode 100644 examples/tests/suite/PostAPISuccessTest.php create mode 100644 phpunit.xml diff --git a/.phpunit.result.cache b/.phpunit.result.cache new file mode 100644 index 0000000..6401a57 --- /dev/null +++ b/.phpunit.result.cache @@ -0,0 +1 @@ +{"version":1,"defects":{"Tests\\Suite\\PostAPISuccessTest::test_post_api":7},"times":{"Tests\\Suite\\PostAPISuccessTest::test_post_api":0.05}} \ No newline at end of file diff --git a/composer.json b/composer.json index fcc0594..48e7a13 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,9 @@ "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" }, "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..9bad633 --- /dev/null +++ b/examples/tests/TestCase.php @@ -0,0 +1,32 @@ +<?php + +namespace Tests; + +class TestCase extends \Orchestra\Testbench\TestCase +{ + use \Illuminate\Foundation\Testing\RefreshDatabase; + public function defineEnvironment($app) + { + tap($app->make('config'), function (\Illuminate\Contracts\Config\Repository $config) { + $config->set('database.default', 'testbench'); + $config->set('database.connections.testbench', [ + 'driver' => 'sqlite', + 'database' => ':memory:', + 'prefix' => '', + ]); + }); + } + + protected function defineRoutes($router) + { + \Illuminate\Support\Facades\Route::resource('api/minions', 'MinionController', ['parameters' => ['minions' => 'id']]); + } + + protected function defineDatabaseMigrations() + { + $this->loadMigrationsFrom(__DIR__ . '/../migrations'); + $this->artisan('migrate', ['--database' => 'testbench'])->run(); + } + + +} diff --git a/examples/tests/suite/PostAPISuccessTest.php b/examples/tests/suite/PostAPISuccessTest.php new file mode 100644 index 0000000..782f531 --- /dev/null +++ b/examples/tests/suite/PostAPISuccessTest.php @@ -0,0 +1,33 @@ +<?php + +namespace Tests\Suite; + +use \Tests\TestCase; + +class PostAPISuccessTest extends TestCase +{ + public function test_post_api() + { + $payload = [ + 'name' => '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/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..a98ff26 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,31 @@ +<?xml version="1.0" encoding="UTF-8"?> +<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd" + bootstrap="vendor/autoload.php" + colors="true" +> + <testsuites> + <testsuite name="Unit"> + <directory suffix="Test.php">.examples/tests/suite</directory> + </testsuite> + <testsuite name="Feature"> + <directory suffix="Test.php">.examples/tests/suite</directory> + </testsuite> + </testsuites> + <source> + <include> + <directory suffix=".php">./app</directory> + </include> + </source> + <php> + <env name="APP_ENV" value="testbench"/> + <env name="BCRYPT_ROUNDS" value="4"/> + <env name="CACHE_DRIVER" value="array"/> + <env name="DB_CONNECTION" value="testbench"/> + <env name="DB_DATABASE" value=":memory:"/> + <env name="MAIL_MAILER" value="array"/> + <env name="QUEUE_CONNECTION" value="sync"/> + <env name="SESSION_DRIVER" value="array"/> + <env name="TELESCOPE_ENABLED" value="false"/> + </php> +</phpunit> From aba82e1990c63af2f134f5f6a8722128e0a189e7 Mon Sep 17 00:00:00 2001 From: Abhishek Mishra <abhishek@luezoid.com> Date: Tue, 23 May 2023 17:25:35 +0530 Subject: [PATCH 2/6] Fixed TestCase class namespace issue --- examples/tests/suite/PostAPISuccessTest.php | 4 +++- phpunit.xml | 5 +---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/examples/tests/suite/PostAPISuccessTest.php b/examples/tests/suite/PostAPISuccessTest.php index 782f531..817c298 100644 --- a/examples/tests/suite/PostAPISuccessTest.php +++ b/examples/tests/suite/PostAPISuccessTest.php @@ -2,7 +2,9 @@ namespace Tests\Suite; -use \Tests\TestCase; +use Tests\TestCase; + +require_once __DIR__.'/../TestCase.php'; class PostAPISuccessTest extends TestCase { diff --git a/phpunit.xml b/phpunit.xml index a98ff26..04475d5 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -6,10 +6,7 @@ > <testsuites> <testsuite name="Unit"> - <directory suffix="Test.php">.examples/tests/suite</directory> - </testsuite> - <testsuite name="Feature"> - <directory suffix="Test.php">.examples/tests/suite</directory> + <directory suffix="Test.php">./examples/tests/suite</directory> </testsuite> </testsuites> <source> From 376e99a6da95550f9c7a82642c09336f27da8870 Mon Sep 17 00:00:00 2001 From: Abhishek Mishra <abhishek@luezoid.com> Date: Tue, 23 May 2023 23:43:47 +0530 Subject: [PATCH 3/6] Deleted .phpunit.result.cache file --- .phpunit.result.cache | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .phpunit.result.cache diff --git a/.phpunit.result.cache b/.phpunit.result.cache deleted file mode 100644 index 6401a57..0000000 --- a/.phpunit.result.cache +++ /dev/null @@ -1 +0,0 @@ -{"version":1,"defects":{"Tests\\Suite\\PostAPISuccessTest::test_post_api":7},"times":{"Tests\\Suite\\PostAPISuccessTest::test_post_api":0.05}} \ No newline at end of file From f1e0b7126b8b04a4b2235f7885286fa179e7ad54 Mon Sep 17 00:00:00 2001 From: Abhishek Mishra <abhishek@luezoid.com> Date: Tue, 23 May 2023 23:47:01 +0530 Subject: [PATCH 4/6] - Resolved dependencies issues in base TestCase class - Added .gitignore --- .gitignore | 3 +- examples/tests/TestCase.php | 28 ++++++++++-- examples/tests/suite/PostAPISuccessTest.php | 48 ++++++++++++++++++++- 3 files changed, 74 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index a1149f2..eb97e3d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ vendor/* composer.lock -.idea/* \ No newline at end of file +.idea/* +.phpunit.result.cache diff --git a/examples/tests/TestCase.php b/examples/tests/TestCase.php index 9bad633..6ef07e4 100644 --- a/examples/tests/TestCase.php +++ b/examples/tests/TestCase.php @@ -2,12 +2,34 @@ namespace Tests; +use Illuminate\Contracts\Config\Repository; +use Illuminate\Foundation\Application; +use Illuminate\Support\Facades\Route; +use Illuminate\Support\ServiceProvider; +use Luezoid\Http\Controllers\MinionController; +require_once __DIR__.'/../Controllers/MinionController.php'; +require_once __DIR__.'/../Repositories/MinionRepository.php'; +require_once __DIR__.'/../Requests/MinionCreateRequest.php'; + class TestCase extends \Orchestra\Testbench\TestCase { - use \Illuminate\Foundation\Testing\RefreshDatabase; + /** + * Get package providers. + * + * @param Application $app + * + * @return array<int, class-string<ServiceProvider>> + */ + protected function getPackageProviders($app): array + { + return [ + 'Luezoid\Laravelcore\CoreServiceProvider', + ]; + } + public function defineEnvironment($app) { - tap($app->make('config'), function (\Illuminate\Contracts\Config\Repository $config) { + tap($app->make('config'), function (Repository $config) { $config->set('database.default', 'testbench'); $config->set('database.connections.testbench', [ 'driver' => 'sqlite', @@ -19,7 +41,7 @@ public function defineEnvironment($app) protected function defineRoutes($router) { - \Illuminate\Support\Facades\Route::resource('api/minions', 'MinionController', ['parameters' => ['minions' => 'id']]); + Route::resource('api/minions', MinionController::class, ['parameters' => ['minions' => 'id']]); } protected function defineDatabaseMigrations() diff --git a/examples/tests/suite/PostAPISuccessTest.php b/examples/tests/suite/PostAPISuccessTest.php index 817c298..c53ebe3 100644 --- a/examples/tests/suite/PostAPISuccessTest.php +++ b/examples/tests/suite/PostAPISuccessTest.php @@ -4,7 +4,8 @@ use Tests\TestCase; -require_once __DIR__.'/../TestCase.php'; +require_once __DIR__ . '/../TestCase.php'; +require_once __DIR__ . '/../../Models/Minion.php'; class PostAPISuccessTest extends TestCase { @@ -32,4 +33,49 @@ public function test_post_api() ]); } + /*public function test_list_api() + { + $response = $this->get('/api/minions'); + $response->assertStatus(200); + $response->assertJson([ + "message" => null, + "data" => [ + "items" => [ + [ + "id" => 1, + "name" => "Lucifer", + "totalEyes" => 0, + "favouriteSound" => "Luuuuuuu", + "hasHairs" => true, + "missions" => [], + "leadingMission" => null + ] + ], + "page" => 1, + "total" => 1, + "pages" => 1, + "perpage" => 15 + ], + "type" => null + ]); + }*/ + + /*public function test_show_api() + { + $response = $this->get('/api/minions/1'); + $response->assertStatus(200); + $response->assertJson([ + "message" => null, + "data" => [ + "id" => 1, + "name" => "Lucifer", + "totalEyes" => 0, + "favouriteSound" => "Luuuuuuu", + "hasHairs" => true, + "missions" => [], + "leadingMission" => null + ], + "type" => null + ]); + }*/ } From fb487c1c3fdc70b00dd8ac531cdf7867fb5aa09d Mon Sep 17 00:00:00 2001 From: Aayush Vijay <vijay.aayush26@gmail.com> Date: Wed, 24 May 2023 12:36:14 +0530 Subject: [PATCH 5/6] Test Cases for Laravel Core Package --- examples/.DS_Store | Bin 0 -> 8196 bytes examples/tests/TestCase.php | 3 + examples/tests/suite/DeleteAPISuccessTest.php | 44 ++++++++++++++ examples/tests/suite/FileAPISuccessTest.php | 55 ++++++++++++++++++ examples/tests/suite/GetAPISuccessTest.php | 45 ++++++++++++++ examples/tests/suite/IndexAPISuccessTest.php | 53 +++++++++++++++++ examples/tests/suite/PostAPISuccessTest.php | 47 +-------------- examples/tests/suite/PutAPISuccessTest.php | 54 +++++++++++++++++ .../5e1904a8-2cf2-497b-9c3e-a27dd1ffd51e.jpg | Bin 0 -> 695 bytes 9 files changed, 255 insertions(+), 46 deletions(-) create mode 100644 examples/.DS_Store create mode 100644 examples/tests/suite/DeleteAPISuccessTest.php create mode 100644 examples/tests/suite/FileAPISuccessTest.php create mode 100644 examples/tests/suite/GetAPISuccessTest.php create mode 100644 examples/tests/suite/IndexAPISuccessTest.php create mode 100644 examples/tests/suite/PutAPISuccessTest.php create mode 100644 storage/examples/5e1904a8-2cf2-497b-9c3e-a27dd1ffd51e.jpg diff --git a/examples/.DS_Store b/examples/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..b36b43e981e0137476213122f108018072aaeb1a GIT binary patch literal 8196 zcmeI1F>ezw7=}MLA%rAC9V@YzjS0vO($S_15)vX43(z!GiIi|DX%MP}bU&hhfSHY% zfdvT!3j-SqTPMUHK;r#u<FoI4S6C2A?dtA3onPBe-t*b|u0$j@m*YOs1`)M!a9r== z6ewKJN2#otJJ%5f+7mscl&1LG!&NNNf#^VVAUY5ohz|UZ4q(sb(yrL|b!|LH2ciRK z(g9u{Bo2;=iJ6XS>A*>)0EkW8Rt49{15_JnVq#*ZqiTvib@m`kQ(;mJp_`*#vK%on zG1E~uC!w2@Fe3|-p$IiP+DRoRk?0uD(Shhd*#XYoyR?P8{Rtg<`TJtJ-yiO#v$<~6 zdRVQTrGvp}nvS4o_dZ>H^`P_aR-JnM7wY+AmqMajtQ2F~0qqWra4cxgCuK=l<1)S( z9)Ek?Rg9&+W@%1Wr}Megm@V3-XZZHPP8Qs2^q}zSU7oGa*0X{pXQ|To9BTFJfa5vs zf@BVoiF&pLp5`HVf;yM$?H9{cq0RYD(*`)3bQ^4=qE})FHe2WNT>ff#ocf$0R38|( zLo>LtkJ-+%DCo~RzKy}3fX?Olc<I{jxn7~o*-q2?oOdxi=F1)UGsbhA7Cq|TDAv1N z2Pf7iuMc-_S803>ULL4N_tacU@vcqaSPJJRxKo%@4PGHCwJzV$56f4h&%OH-z0WO~ zyREnnXvwVQce4E=q;WY<ep=2_U#^#RI-e_Ht_zB5fwz-aU94Y4C*_B*%4NIKv2!ob zpG#M-4DhwoKCpyiC9<{qAoJ>7ruWh^6*T#?tkU=#Y8^YL3Cd6IJiF-Bx(qMB-|Qb< zRBOAZ$O*y<e|*U!ta@qaAGpE!|918F|5djk{=VqInRY-l2gAW8EHJrs$qdf5C5}fp zxUeqMQ8mFyrQ?Vy9Y?hPFvPWll-ndGW;)^y%0K@QU^AMy|MULt?|TV95^wMJbE~-j Jvq7s+egPu76CVHo literal 0 HcmV?d00001 diff --git a/examples/tests/TestCase.php b/examples/tests/TestCase.php index 6ef07e4..5120bc0 100644 --- a/examples/tests/TestCase.php +++ b/examples/tests/TestCase.php @@ -7,6 +7,8 @@ use Illuminate\Support\Facades\Route; use Illuminate\Support\ServiceProvider; use Luezoid\Http\Controllers\MinionController; +use Luezoid\Laravelcore\Http\Controllers\FileController; + require_once __DIR__.'/../Controllers/MinionController.php'; require_once __DIR__.'/../Repositories/MinionRepository.php'; require_once __DIR__.'/../Requests/MinionCreateRequest.php'; @@ -42,6 +44,7 @@ public function defineEnvironment($app) protected function defineRoutes($router) { Route::resource('api/minions', MinionController::class, ['parameters' => ['minions' => 'id']]); + Route::post('api/files',FileController::class.'@store'); } protected function defineDatabaseMigrations() diff --git a/examples/tests/suite/DeleteAPISuccessTest.php b/examples/tests/suite/DeleteAPISuccessTest.php new file mode 100644 index 0000000..4698e5f --- /dev/null +++ b/examples/tests/suite/DeleteAPISuccessTest.php @@ -0,0 +1,44 @@ +<?php + +namespace Tests\Suite; + +use Luezoid\Models\Minion; +use Tests\TestCase; +require_once __DIR__ . '/../TestCase.php'; +require_once __DIR__ . '/../../Models/Minion.php'; +require_once __DIR__ . '/../../Requests/MinionDeleteRequest.php'; + +class DeleteAPISuccessTest extends TestCase +{ + /** + * A basic feature test example. + */ + public function test_delete_minion() + { + $payload = [ + 'name' => 'Stuart', + 'totalEyes' => 2, + 'favouriteSound' => 'Grrrrrrrrrrr', + 'hasHairs' => true, + ]; + // Create a minion. + $minion = $this->postJson('/api/minions', $payload); + $minion = Minion::where('id', 1)->first(); + // 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' => $minion->id, + 'name' => $minion->name, + 'totalEyes' => $minion->total_eyes, + 'favouriteSound' => $minion->favourite_sound, + 'hasHairs' => $minion->has_hairs + ], + 'type' => null, + ]); + } +} diff --git a/examples/tests/suite/FileAPISuccessTest.php b/examples/tests/suite/FileAPISuccessTest.php new file mode 100644 index 0000000..ae51dc8 --- /dev/null +++ b/examples/tests/suite/FileAPISuccessTest.php @@ -0,0 +1,55 @@ +<?php + +namespace Tests\Suite; + +use Illuminate\Http\UploadedFile; +use Illuminate\Support\Facades\Storage; +use Tests\TestCase; +require_once __DIR__.'/../TestCase.php'; +require_once __DIR__.'/../../../src/Models/File.php'; +require_once __DIR__.'/../../../src/config/file.php'; +require_once __DIR__.'/../../../src/Contracts/IFile.php'; +require_once __DIR__.'/../../../src/Files/Services/LocalFileUploadService.php'; +require_once __DIR__.'/../../../src/Files/Services/SaveFileToS3Service.php'; + +class FileAPISuccessTest extends TestCase +{ + /** + * A basic feature test example. + */ + public function testFileUpload() + { + Storage::fake('local'); // Use a fake disk for testing file uploads + + $file = UploadedFile::fake()->image('test-image.jpg'); // Create a fake test file + + $this->app->bind(\Luezoid\Laravelcore\Contracts\IFile::class, function ($app) { + if (config('file.is_local')) { + return $app->make(\Luezoid\Laravelcore\Files\Services\LocalFileUploadService::class); + } + return $app->make(\Luezoid\Laravelcore\Files\Services\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..8cdf8b6 --- /dev/null +++ b/examples/tests/suite/GetAPISuccessTest.php @@ -0,0 +1,45 @@ +<?php + +namespace Tests\Suite; + +use Luezoid\Models\Minion; +use Tests\TestCase; +require_once __DIR__ . '/../TestCase.php'; +require_once __DIR__ . '/../../Models/Minion.php'; + +class GetAPISuccessTest extends TestCase +{ + /** + * A basic feature test example. + */ + public function test_get_minion() + { + $payload = [ + 'name' => 'Stuart', + 'totalEyes' => 2, + 'favouriteSound' => 'Grrrrrrrrrrr', + 'hasHairs' => true, + ]; + // Create a minion. + $minion = $this->postJson('/api/minions', $payload); + $minion = Minion::where('id', 1)->first(); + // 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' => $minion->id, + 'name' => $minion->name, + 'totalEyes' => $minion->total_eyes, + 'favouriteSound' => $minion->favourite_sound, + 'hasHairs' => $minion->has_hairs + ], + 'type' => null, + ]); + } +} diff --git a/examples/tests/suite/IndexAPISuccessTest.php b/examples/tests/suite/IndexAPISuccessTest.php new file mode 100644 index 0000000..70430d1 --- /dev/null +++ b/examples/tests/suite/IndexAPISuccessTest.php @@ -0,0 +1,53 @@ +<?php + +namespace Tests\Suite; +use Luezoid\Models\Minion; +use Tests\TestCase; + +require_once __DIR__ . '/../TestCase.php'; +require_once __DIR__ . '/../../Models/Minion.php'; + +class IndexAPISuccessTest extends TestCase +{ + /** + * A basic feature test example. + */ + public function test_index_minion(): void + { + $payload = [ + 'name' => 'Stuart', + 'totalEyes' => 2, + 'favouriteSound' => 'Grrrrrrrrrrr', + 'hasHairs' => true, + ]; + // Create a minion. + $minion = $this->postJson('/api/minions', $payload); + $minion = Minion::where('id', 1)->first(); + + // Make the request. + $response = $this->getJson('/api/minions'); + + $response->assertOk(); + + // Assert that the response data is correct. + $response->assertJson([ + 'message' => null, + 'data' => [ + 'items' => [ + [ + 'id' => $minion->id, + 'name' => $minion->name, + 'totalEyes' => $minion->total_eyes, + 'favouriteSound' => $minion->favourite_sound, + 'hasHairs' => $minion->has_hairs + ] + ], + 'page' => 1, + 'total' => 1, + 'pages' => 1, + 'perpage' => 15, + ], + 'type' => null, + ]); + } +} diff --git a/examples/tests/suite/PostAPISuccessTest.php b/examples/tests/suite/PostAPISuccessTest.php index c53ebe3..9273305 100644 --- a/examples/tests/suite/PostAPISuccessTest.php +++ b/examples/tests/suite/PostAPISuccessTest.php @@ -6,6 +6,7 @@ require_once __DIR__ . '/../TestCase.php'; require_once __DIR__ . '/../../Models/Minion.php'; +require_once __DIR__ . '/../../Requests/MinionCreateRequest.php'; class PostAPISuccessTest extends TestCase { @@ -32,50 +33,4 @@ public function test_post_api() 'type' => null, ]); } - - /*public function test_list_api() - { - $response = $this->get('/api/minions'); - $response->assertStatus(200); - $response->assertJson([ - "message" => null, - "data" => [ - "items" => [ - [ - "id" => 1, - "name" => "Lucifer", - "totalEyes" => 0, - "favouriteSound" => "Luuuuuuu", - "hasHairs" => true, - "missions" => [], - "leadingMission" => null - ] - ], - "page" => 1, - "total" => 1, - "pages" => 1, - "perpage" => 15 - ], - "type" => null - ]); - }*/ - - /*public function test_show_api() - { - $response = $this->get('/api/minions/1'); - $response->assertStatus(200); - $response->assertJson([ - "message" => null, - "data" => [ - "id" => 1, - "name" => "Lucifer", - "totalEyes" => 0, - "favouriteSound" => "Luuuuuuu", - "hasHairs" => true, - "missions" => [], - "leadingMission" => null - ], - "type" => null - ]); - }*/ } diff --git a/examples/tests/suite/PutAPISuccessTest.php b/examples/tests/suite/PutAPISuccessTest.php new file mode 100644 index 0000000..9ea62aa --- /dev/null +++ b/examples/tests/suite/PutAPISuccessTest.php @@ -0,0 +1,54 @@ +<?php + +namespace Tests\Suite; + +use Luezoid\Models\Minion; +use Tests\TestCase; +require_once __DIR__ . '/../TestCase.php'; +require_once __DIR__ . '/../../Models/Minion.php'; +require_once __DIR__ . '/../../Requests/MinionUpdateRequest.php'; + +class PutAPISuccessTest extends TestCase +{ + /** + * A basic feature test example. + */ + public function test_update_minion() + { + // Create a minion. + $minion = $this->postJson('/api/minions', [ + 'name' => 'Stuart', + 'totalEyes' => 2, + 'favouriteSound' => 'Grrrrrrrrrrr', + 'hasHairs' => true, + ]); + // Get the minion. + $minion = Minion::where('id', 1)->first(); + // 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' => $minion->id, + 'name' => $payload['name'], + 'totalEyes' => $payload['totalEyes'], + 'favouriteSound' => $payload['favouriteSound'], + 'hasHairs' => $payload['hasHairs'], + ], + 'type' => null, + ]); + } +} diff --git a/storage/examples/5e1904a8-2cf2-497b-9c3e-a27dd1ffd51e.jpg b/storage/examples/5e1904a8-2cf2-497b-9c3e-a27dd1ffd51e.jpg new file mode 100644 index 0000000000000000000000000000000000000000..b00797624aa746a4a3578303e4fa34d50be62291 GIT binary patch literal 695 zcmex=<NpH&0WUXCHwH#VMur3+WcdG&!Ol6z)iK0B$VwqSMK`M;HC>_1P|rX?qqI0P zFI~aY%U!`Mz|~!$%)&rZM<FFOEwMDGM4_-WF(<R6lI#C%24@BiHa2!PRt|P{c1}(X zE*=qH9&T<PNg-i=5m_mDIaw(g83h$Rbp<6IWf>U_b4?usLlYAdd38%$3nLpnV-q8g zA&i`yoIKn-61=<;Mv5|uMkIs(2N(o77`Pa?m>HEAm;@P_1sVSzVUTBFU}OdQ7UW?l zU}R!uVP#|I;N;>4D%dK(z{JSR%*4XX%F4n5R9y>{XJ8Rz6;d>GWD^cdWLGK_F>0K+ zkVDyN<3Z7&iyu^slZu)+xx~aJB&Af<)HO7<OiazpEiA2^U0mJVJv_aFLqfyCBO;@c zfq|8tk(pIoQd(ACQCZd8(%RPE(b+X=@|3C5rq7r;YtiB*OP4KQv2xX>&0Dr^+rDGx zu0w~996fgY#K}{aE?>EN?fQ+Iw;n!v{N(Ag=PzEq`uOSdm#^Qx|M>X}<S#}BW{9`o zK11`DAkbe-EG*0{>>z(JGL-`{vmgtrq9L1*V<3BCp|FxsBZr97#DyCVaw;1KeGpA5 xy2vG_V)9V+BgkuDpAqM=CbE16_ZY%ow-|Vs8G(_<EXZKb@bh1n#sdETHvuSa<I?~D literal 0 HcmV?d00001 From 7e9493d3e557bc36557957a2439e12f7e7a7108d Mon Sep 17 00:00:00 2001 From: Abhishek Mishra <abhishek@luezoid.com> Date: Wed, 24 May 2023 13:36:54 +0530 Subject: [PATCH 6/6] Code clean up --- .gitignore | 1 + composer.json | 4 +++- examples/.DS_Store | Bin 8196 -> 0 bytes examples/tests/TestCase.php | 9 ++++----- examples/tests/suite/DeleteAPISuccessTest.php | 15 +++++++-------- examples/tests/suite/FileAPISuccessTest.php | 9 ++++++--- examples/tests/suite/GetAPISuccessTest.php | 15 +++++++-------- examples/tests/suite/IndexAPISuccessTest.php | 14 ++++++-------- examples/tests/suite/PutAPISuccessTest.php | 16 +++++++--------- phpunit.xml | 11 +++-------- .../5e1904a8-2cf2-497b-9c3e-a27dd1ffd51e.jpg | Bin 695 -> 0 bytes 11 files changed, 44 insertions(+), 50 deletions(-) delete mode 100644 examples/.DS_Store delete mode 100644 storage/examples/5e1904a8-2cf2-497b-9c3e-a27dd1ffd51e.jpg diff --git a/.gitignore b/.gitignore index eb97e3d..4900578 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ vendor/* composer.lock .idea/* .phpunit.result.cache +storage/* diff --git a/composer.json b/composer.json index 48e7a13..9aa17b0 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,9 @@ "require-dev": { "laravel/framework": "^5.0|^6.0|^7.0|^8.0|^9.0|^10.0", "orchestra/testbench": "^8.5", - "phpunit/phpunit": "^10.1" + "phpunit/phpunit": "^10.1", + "ext-gd": "*", + "ext-sqlite3": "*" }, "autoload": { "psr-4": { diff --git a/examples/.DS_Store b/examples/.DS_Store deleted file mode 100644 index b36b43e981e0137476213122f108018072aaeb1a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 8196 zcmeI1F>ezw7=}MLA%rAC9V@YzjS0vO($S_15)vX43(z!GiIi|DX%MP}bU&hhfSHY% zfdvT!3j-SqTPMUHK;r#u<FoI4S6C2A?dtA3onPBe-t*b|u0$j@m*YOs1`)M!a9r== z6ewKJN2#otJJ%5f+7mscl&1LG!&NNNf#^VVAUY5ohz|UZ4q(sb(yrL|b!|LH2ciRK z(g9u{Bo2;=iJ6XS>A*>)0EkW8Rt49{15_JnVq#*ZqiTvib@m`kQ(;mJp_`*#vK%on zG1E~uC!w2@Fe3|-p$IiP+DRoRk?0uD(Shhd*#XYoyR?P8{Rtg<`TJtJ-yiO#v$<~6 zdRVQTrGvp}nvS4o_dZ>H^`P_aR-JnM7wY+AmqMajtQ2F~0qqWra4cxgCuK=l<1)S( z9)Ek?Rg9&+W@%1Wr}Megm@V3-XZZHPP8Qs2^q}zSU7oGa*0X{pXQ|To9BTFJfa5vs zf@BVoiF&pLp5`HVf;yM$?H9{cq0RYD(*`)3bQ^4=qE})FHe2WNT>ff#ocf$0R38|( zLo>LtkJ-+%DCo~RzKy}3fX?Olc<I{jxn7~o*-q2?oOdxi=F1)UGsbhA7Cq|TDAv1N z2Pf7iuMc-_S803>ULL4N_tacU@vcqaSPJJRxKo%@4PGHCwJzV$56f4h&%OH-z0WO~ zyREnnXvwVQce4E=q;WY<ep=2_U#^#RI-e_Ht_zB5fwz-aU94Y4C*_B*%4NIKv2!ob zpG#M-4DhwoKCpyiC9<{qAoJ>7ruWh^6*T#?tkU=#Y8^YL3Cd6IJiF-Bx(qMB-|Qb< zRBOAZ$O*y<e|*U!ta@qaAGpE!|918F|5djk{=VqInRY-l2gAW8EHJrs$qdf5C5}fp zxUeqMQ8mFyrQ?Vy9Y?hPFvPWll-ndGW;)^y%0K@QU^AMy|MULt?|TV95^wMJbE~-j Jvq7s+egPu76CVHo diff --git a/examples/tests/TestCase.php b/examples/tests/TestCase.php index 5120bc0..c9b57e6 100644 --- a/examples/tests/TestCase.php +++ b/examples/tests/TestCase.php @@ -11,7 +11,6 @@ require_once __DIR__.'/../Controllers/MinionController.php'; require_once __DIR__.'/../Repositories/MinionRepository.php'; -require_once __DIR__.'/../Requests/MinionCreateRequest.php'; class TestCase extends \Orchestra\Testbench\TestCase { @@ -32,8 +31,8 @@ protected function getPackageProviders($app): array public function defineEnvironment($app) { tap($app->make('config'), function (Repository $config) { - $config->set('database.default', 'testbench'); - $config->set('database.connections.testbench', [ + $config->set('database.default', 'test'); + $config->set('database.connections.test', [ 'driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '', @@ -44,13 +43,13 @@ public function defineEnvironment($app) protected function defineRoutes($router) { Route::resource('api/minions', MinionController::class, ['parameters' => ['minions' => 'id']]); - Route::post('api/files',FileController::class.'@store'); + Route::post('api/files',[FileController::class, 'store']); } protected function defineDatabaseMigrations() { $this->loadMigrationsFrom(__DIR__ . '/../migrations'); - $this->artisan('migrate', ['--database' => 'testbench'])->run(); + $this->artisan('migrate', ['--database' => 'test'])->run(); } diff --git a/examples/tests/suite/DeleteAPISuccessTest.php b/examples/tests/suite/DeleteAPISuccessTest.php index 4698e5f..d50ef97 100644 --- a/examples/tests/suite/DeleteAPISuccessTest.php +++ b/examples/tests/suite/DeleteAPISuccessTest.php @@ -2,7 +2,6 @@ namespace Tests\Suite; -use Luezoid\Models\Minion; use Tests\TestCase; require_once __DIR__ . '/../TestCase.php'; require_once __DIR__ . '/../../Models/Minion.php'; @@ -22,8 +21,8 @@ public function test_delete_minion() 'hasHairs' => true, ]; // Create a minion. - $minion = $this->postJson('/api/minions', $payload); - $minion = Minion::where('id', 1)->first(); + $this->postJson('/api/minions', $payload); + // Make the request. $response = $this->deleteJson('/api/minions/1'); // Assert that the response is successful. @@ -32,11 +31,11 @@ public function test_delete_minion() $response->assertJson([ 'message' =>"Resource deleted successfully", 'data' => [ - 'id' => $minion->id, - 'name' => $minion->name, - 'totalEyes' => $minion->total_eyes, - 'favouriteSound' => $minion->favourite_sound, - 'hasHairs' => $minion->has_hairs + '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 index ae51dc8..f382d4a 100644 --- a/examples/tests/suite/FileAPISuccessTest.php +++ b/examples/tests/suite/FileAPISuccessTest.php @@ -4,6 +4,9 @@ use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Storage; +use Luezoid\Laravelcore\Contracts\IFile; +use Luezoid\Laravelcore\Files\Services\LocalFileUploadService; +use Luezoid\Laravelcore\Files\Services\SaveFileToS3Service; use Tests\TestCase; require_once __DIR__.'/../TestCase.php'; require_once __DIR__.'/../../../src/Models/File.php'; @@ -23,11 +26,11 @@ public function testFileUpload() $file = UploadedFile::fake()->image('test-image.jpg'); // Create a fake test file - $this->app->bind(\Luezoid\Laravelcore\Contracts\IFile::class, function ($app) { + $this->app->bind(IFile::class, function ($app) { if (config('file.is_local')) { - return $app->make(\Luezoid\Laravelcore\Files\Services\LocalFileUploadService::class); + return $app->make(LocalFileUploadService::class); } - return $app->make(\Luezoid\Laravelcore\Files\Services\SaveFileToS3Service::class); + return $app->make(SaveFileToS3Service::class); }); $response = $this->post('/api/files', [ diff --git a/examples/tests/suite/GetAPISuccessTest.php b/examples/tests/suite/GetAPISuccessTest.php index 8cdf8b6..61395b8 100644 --- a/examples/tests/suite/GetAPISuccessTest.php +++ b/examples/tests/suite/GetAPISuccessTest.php @@ -2,7 +2,6 @@ namespace Tests\Suite; -use Luezoid\Models\Minion; use Tests\TestCase; require_once __DIR__ . '/../TestCase.php'; require_once __DIR__ . '/../../Models/Minion.php'; @@ -21,8 +20,8 @@ public function test_get_minion() 'hasHairs' => true, ]; // Create a minion. - $minion = $this->postJson('/api/minions', $payload); - $minion = Minion::where('id', 1)->first(); + $this->postJson('/api/minions', $payload); + // Make the request. $response = $this->getJson('/api/minions/1'); @@ -33,11 +32,11 @@ public function test_get_minion() $response->assertJson([ 'message' => null, 'data' => [ - 'id' => $minion->id, - 'name' => $minion->name, - 'totalEyes' => $minion->total_eyes, - 'favouriteSound' => $minion->favourite_sound, - 'hasHairs' => $minion->has_hairs + '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 index 70430d1..75720be 100644 --- a/examples/tests/suite/IndexAPISuccessTest.php +++ b/examples/tests/suite/IndexAPISuccessTest.php @@ -1,7 +1,6 @@ <?php namespace Tests\Suite; -use Luezoid\Models\Minion; use Tests\TestCase; require_once __DIR__ . '/../TestCase.php'; @@ -21,8 +20,7 @@ public function test_index_minion(): void 'hasHairs' => true, ]; // Create a minion. - $minion = $this->postJson('/api/minions', $payload); - $minion = Minion::where('id', 1)->first(); + $this->postJson('/api/minions', $payload); // Make the request. $response = $this->getJson('/api/minions'); @@ -35,11 +33,11 @@ public function test_index_minion(): void 'data' => [ 'items' => [ [ - 'id' => $minion->id, - 'name' => $minion->name, - 'totalEyes' => $minion->total_eyes, - 'favouriteSound' => $minion->favourite_sound, - 'hasHairs' => $minion->has_hairs + 'id' => 1, + 'name' => 'Stuart', + 'totalEyes' => 2, + 'favouriteSound' => 'Grrrrrrrrrrr', + 'hasHairs' => true ] ], 'page' => 1, diff --git a/examples/tests/suite/PutAPISuccessTest.php b/examples/tests/suite/PutAPISuccessTest.php index 9ea62aa..5274cda 100644 --- a/examples/tests/suite/PutAPISuccessTest.php +++ b/examples/tests/suite/PutAPISuccessTest.php @@ -2,7 +2,6 @@ namespace Tests\Suite; -use Luezoid\Models\Minion; use Tests\TestCase; require_once __DIR__ . '/../TestCase.php'; require_once __DIR__ . '/../../Models/Minion.php'; @@ -16,14 +15,13 @@ class PutAPISuccessTest extends TestCase public function test_update_minion() { // Create a minion. - $minion = $this->postJson('/api/minions', [ + $this->postJson('/api/minions', [ 'name' => 'Stuart', 'totalEyes' => 2, 'favouriteSound' => 'Grrrrrrrrrrr', 'hasHairs' => true, ]); - // Get the minion. - $minion = Minion::where('id', 1)->first(); + // Prepare the request payload. $payload = [ 'name' => 'Stuart', @@ -42,11 +40,11 @@ public function test_update_minion() $response->assertJson([ 'message' => 'Resource Updated successfully', 'data' => [ - 'id' => $minion->id, - 'name' => $payload['name'], - 'totalEyes' => $payload['totalEyes'], - 'favouriteSound' => $payload['favouriteSound'], - 'hasHairs' => $payload['hasHairs'], + 'id' => 1, + 'name' => 'Stuart', + 'totalEyes' => 2, + 'favouriteSound' => 'Hrrrrrrrrrrr', + 'hasHairs' => true ], 'type' => null, ]); diff --git a/phpunit.xml b/phpunit.xml index 04475d5..2ebd8fb 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -6,19 +6,14 @@ > <testsuites> <testsuite name="Unit"> - <directory suffix="Test.php">./examples/tests/suite</directory> + <directory>./examples/tests/suite</directory> </testsuite> </testsuites> - <source> - <include> - <directory suffix=".php">./app</directory> - </include> - </source> <php> - <env name="APP_ENV" value="testbench"/> + <env name="APP_ENV" value="test"/> <env name="BCRYPT_ROUNDS" value="4"/> <env name="CACHE_DRIVER" value="array"/> - <env name="DB_CONNECTION" value="testbench"/> + <env name="DB_CONNECTION" value="test"/> <env name="DB_DATABASE" value=":memory:"/> <env name="MAIL_MAILER" value="array"/> <env name="QUEUE_CONNECTION" value="sync"/> diff --git a/storage/examples/5e1904a8-2cf2-497b-9c3e-a27dd1ffd51e.jpg b/storage/examples/5e1904a8-2cf2-497b-9c3e-a27dd1ffd51e.jpg deleted file mode 100644 index b00797624aa746a4a3578303e4fa34d50be62291..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 695 zcmex=<NpH&0WUXCHwH#VMur3+WcdG&!Ol6z)iK0B$VwqSMK`M;HC>_1P|rX?qqI0P zFI~aY%U!`Mz|~!$%)&rZM<FFOEwMDGM4_-WF(<R6lI#C%24@BiHa2!PRt|P{c1}(X zE*=qH9&T<PNg-i=5m_mDIaw(g83h$Rbp<6IWf>U_b4?usLlYAdd38%$3nLpnV-q8g zA&i`yoIKn-61=<;Mv5|uMkIs(2N(o77`Pa?m>HEAm;@P_1sVSzVUTBFU}OdQ7UW?l zU}R!uVP#|I;N;>4D%dK(z{JSR%*4XX%F4n5R9y>{XJ8Rz6;d>GWD^cdWLGK_F>0K+ zkVDyN<3Z7&iyu^slZu)+xx~aJB&Af<)HO7<OiazpEiA2^U0mJVJv_aFLqfyCBO;@c zfq|8tk(pIoQd(ACQCZd8(%RPE(b+X=@|3C5rq7r;YtiB*OP4KQv2xX>&0Dr^+rDGx zu0w~996fgY#K}{aE?>EN?fQ+Iw;n!v{N(Ag=PzEq`uOSdm#^Qx|M>X}<S#}BW{9`o zK11`DAkbe-EG*0{>>z(JGL-`{vmgtrq9L1*V<3BCp|FxsBZr97#DyCVaw;1KeGpA5 xy2vG_V)9V+BgkuDpAqM=CbE16_ZY%ow-|Vs8G(_<EXZKb@bh1n#sdETHvuSa<I?~D