Skip to content

Commit 35dfb58

Browse files
authored
Added package Test Cases using sqlite DB connection
Added package Test Cases using sqlite DB connection
2 parents 7074616 + 7e9493d commit 35dfb58

File tree

11 files changed

+371
-2
lines changed

11 files changed

+371
-2
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
vendor/*
22
composer.lock
3-
.idea/*
3+
.idea/*
4+
.phpunit.result.cache
5+
storage/*

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@
3434
"ext-json": "*"
3535
},
3636
"require-dev": {
37-
"laravel/framework": "^5.0|^6.0|^7.0|^8.0|^9.0|^10.0"
37+
"laravel/framework": "^5.0|^6.0|^7.0|^8.0|^9.0|^10.0",
38+
"orchestra/testbench": "^8.5",
39+
"phpunit/phpunit": "^10.1",
40+
"ext-gd": "*",
41+
"ext-sqlite3": "*"
3842
},
3943
"autoload": {
4044
"psr-4": {

examples/database/database.sqlite

Whitespace-only changes.

examples/tests/TestCase.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use Illuminate\Contracts\Config\Repository;
6+
use Illuminate\Foundation\Application;
7+
use Illuminate\Support\Facades\Route;
8+
use Illuminate\Support\ServiceProvider;
9+
use Luezoid\Http\Controllers\MinionController;
10+
use Luezoid\Laravelcore\Http\Controllers\FileController;
11+
12+
require_once __DIR__.'/../Controllers/MinionController.php';
13+
require_once __DIR__.'/../Repositories/MinionRepository.php';
14+
15+
class TestCase extends \Orchestra\Testbench\TestCase
16+
{
17+
/**
18+
* Get package providers.
19+
*
20+
* @param Application $app
21+
*
22+
* @return array<int, class-string<ServiceProvider>>
23+
*/
24+
protected function getPackageProviders($app): array
25+
{
26+
return [
27+
'Luezoid\Laravelcore\CoreServiceProvider',
28+
];
29+
}
30+
31+
public function defineEnvironment($app)
32+
{
33+
tap($app->make('config'), function (Repository $config) {
34+
$config->set('database.default', 'test');
35+
$config->set('database.connections.test', [
36+
'driver' => 'sqlite',
37+
'database' => ':memory:',
38+
'prefix' => '',
39+
]);
40+
});
41+
}
42+
43+
protected function defineRoutes($router)
44+
{
45+
Route::resource('api/minions', MinionController::class, ['parameters' => ['minions' => 'id']]);
46+
Route::post('api/files',[FileController::class, 'store']);
47+
}
48+
49+
protected function defineDatabaseMigrations()
50+
{
51+
$this->loadMigrationsFrom(__DIR__ . '/../migrations');
52+
$this->artisan('migrate', ['--database' => 'test'])->run();
53+
}
54+
55+
56+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Tests\Suite;
4+
5+
use Tests\TestCase;
6+
require_once __DIR__ . '/../TestCase.php';
7+
require_once __DIR__ . '/../../Models/Minion.php';
8+
require_once __DIR__ . '/../../Requests/MinionDeleteRequest.php';
9+
10+
class DeleteAPISuccessTest extends TestCase
11+
{
12+
/**
13+
* A basic feature test example.
14+
*/
15+
public function test_delete_minion()
16+
{
17+
$payload = [
18+
'name' => 'Stuart',
19+
'totalEyes' => 2,
20+
'favouriteSound' => 'Grrrrrrrrrrr',
21+
'hasHairs' => true,
22+
];
23+
// Create a minion.
24+
$this->postJson('/api/minions', $payload);
25+
26+
// Make the request.
27+
$response = $this->deleteJson('/api/minions/1');
28+
// Assert that the response is successful.
29+
$response->assertOk();
30+
// Assert that the minion is deleted.
31+
$response->assertJson([
32+
'message' =>"Resource deleted successfully",
33+
'data' => [
34+
'id' => 1,
35+
'name' => 'Stuart',
36+
'totalEyes' => 2,
37+
'favouriteSound' => 'Grrrrrrrrrrr',
38+
'hasHairs' => true
39+
],
40+
'type' => null,
41+
]);
42+
}
43+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Tests\Suite;
4+
5+
use Illuminate\Http\UploadedFile;
6+
use Illuminate\Support\Facades\Storage;
7+
use Luezoid\Laravelcore\Contracts\IFile;
8+
use Luezoid\Laravelcore\Files\Services\LocalFileUploadService;
9+
use Luezoid\Laravelcore\Files\Services\SaveFileToS3Service;
10+
use Tests\TestCase;
11+
require_once __DIR__.'/../TestCase.php';
12+
require_once __DIR__.'/../../../src/Models/File.php';
13+
require_once __DIR__.'/../../../src/config/file.php';
14+
require_once __DIR__.'/../../../src/Contracts/IFile.php';
15+
require_once __DIR__.'/../../../src/Files/Services/LocalFileUploadService.php';
16+
require_once __DIR__.'/../../../src/Files/Services/SaveFileToS3Service.php';
17+
18+
class FileAPISuccessTest extends TestCase
19+
{
20+
/**
21+
* A basic feature test example.
22+
*/
23+
public function testFileUpload()
24+
{
25+
Storage::fake('local'); // Use a fake disk for testing file uploads
26+
27+
$file = UploadedFile::fake()->image('test-image.jpg'); // Create a fake test file
28+
29+
$this->app->bind(IFile::class, function ($app) {
30+
if (config('file.is_local')) {
31+
return $app->make(LocalFileUploadService::class);
32+
}
33+
return $app->make(SaveFileToS3Service::class);
34+
});
35+
36+
$response = $this->post('/api/files', [
37+
'file' => $file,
38+
'type' => 'EXAMPLE',
39+
]);
40+
41+
$response->assertStatus(200); // Assert that the response has a status code of 200
42+
// Assert the JSON structure of the response
43+
$response->assertJsonStructure([
44+
'message',
45+
'data' => [
46+
'type',
47+
'name',
48+
'localPath',
49+
's3Key',
50+
'updatedAt',
51+
'createdAt',
52+
'id',
53+
'url',
54+
],
55+
'type',
56+
]);
57+
}
58+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Tests\Suite;
4+
5+
use Tests\TestCase;
6+
require_once __DIR__ . '/../TestCase.php';
7+
require_once __DIR__ . '/../../Models/Minion.php';
8+
9+
class GetAPISuccessTest extends TestCase
10+
{
11+
/**
12+
* A basic feature test example.
13+
*/
14+
public function test_get_minion()
15+
{
16+
$payload = [
17+
'name' => 'Stuart',
18+
'totalEyes' => 2,
19+
'favouriteSound' => 'Grrrrrrrrrrr',
20+
'hasHairs' => true,
21+
];
22+
// Create a minion.
23+
$this->postJson('/api/minions', $payload);
24+
25+
// Make the request.
26+
$response = $this->getJson('/api/minions/1');
27+
28+
// Assert that the response is successful.
29+
$response->assertOk();
30+
31+
// Assert that the response data is correct.
32+
$response->assertJson([
33+
'message' => null,
34+
'data' => [
35+
'id' => 1,
36+
'name' => 'Stuart',
37+
'totalEyes' => 2,
38+
'favouriteSound' => 'Grrrrrrrrrrr',
39+
'hasHairs' => true
40+
],
41+
'type' => null,
42+
]);
43+
}
44+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Tests\Suite;
4+
use Tests\TestCase;
5+
6+
require_once __DIR__ . '/../TestCase.php';
7+
require_once __DIR__ . '/../../Models/Minion.php';
8+
9+
class IndexAPISuccessTest extends TestCase
10+
{
11+
/**
12+
* A basic feature test example.
13+
*/
14+
public function test_index_minion(): void
15+
{
16+
$payload = [
17+
'name' => 'Stuart',
18+
'totalEyes' => 2,
19+
'favouriteSound' => 'Grrrrrrrrrrr',
20+
'hasHairs' => true,
21+
];
22+
// Create a minion.
23+
$this->postJson('/api/minions', $payload);
24+
25+
// Make the request.
26+
$response = $this->getJson('/api/minions');
27+
28+
$response->assertOk();
29+
30+
// Assert that the response data is correct.
31+
$response->assertJson([
32+
'message' => null,
33+
'data' => [
34+
'items' => [
35+
[
36+
'id' => 1,
37+
'name' => 'Stuart',
38+
'totalEyes' => 2,
39+
'favouriteSound' => 'Grrrrrrrrrrr',
40+
'hasHairs' => true
41+
]
42+
],
43+
'page' => 1,
44+
'total' => 1,
45+
'pages' => 1,
46+
'perpage' => 15,
47+
],
48+
'type' => null,
49+
]);
50+
}
51+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Tests\Suite;
4+
5+
use Tests\TestCase;
6+
7+
require_once __DIR__ . '/../TestCase.php';
8+
require_once __DIR__ . '/../../Models/Minion.php';
9+
require_once __DIR__ . '/../../Requests/MinionCreateRequest.php';
10+
11+
class PostAPISuccessTest extends TestCase
12+
{
13+
public function test_post_api()
14+
{
15+
$payload = [
16+
'name' => 'Stuart',
17+
'totalEyes' => 2,
18+
'favouriteSound' => 'Grrrrrrrrrrr',
19+
'hasHairs' => true,
20+
];
21+
22+
$response = $this->postJson('/api/minions', $payload);
23+
$response->assertStatus(200);
24+
$response->assertJson([
25+
'message' => 'Resource Created successfully',
26+
'data' => [
27+
'name' => 'Stuart',
28+
'totalEyes' => 2,
29+
'favouriteSound' => 'Grrrrrrrrrrr',
30+
'hasHairs' => true,
31+
'id' => 1,
32+
],
33+
'type' => null,
34+
]);
35+
}
36+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Tests\Suite;
4+
5+
use Tests\TestCase;
6+
require_once __DIR__ . '/../TestCase.php';
7+
require_once __DIR__ . '/../../Models/Minion.php';
8+
require_once __DIR__ . '/../../Requests/MinionUpdateRequest.php';
9+
10+
class PutAPISuccessTest extends TestCase
11+
{
12+
/**
13+
* A basic feature test example.
14+
*/
15+
public function test_update_minion()
16+
{
17+
// Create a minion.
18+
$this->postJson('/api/minions', [
19+
'name' => 'Stuart',
20+
'totalEyes' => 2,
21+
'favouriteSound' => 'Grrrrrrrrrrr',
22+
'hasHairs' => true,
23+
]);
24+
25+
// Prepare the request payload.
26+
$payload = [
27+
'name' => 'Stuart',
28+
'totalEyes' => 2,
29+
'favouriteSound' => 'Hrrrrrrrrrrr',
30+
'hasHairs' => true,
31+
];
32+
33+
// Make the request.
34+
$response = $this->putJson('/api/minions/1', $payload);
35+
36+
// Assert that the response is successful.
37+
$response->assertOk();
38+
39+
// Assert that the response data is correct.
40+
$response->assertJson([
41+
'message' => 'Resource Updated successfully',
42+
'data' => [
43+
'id' => 1,
44+
'name' => 'Stuart',
45+
'totalEyes' => 2,
46+
'favouriteSound' => 'Hrrrrrrrrrrr',
47+
'hasHairs' => true
48+
],
49+
'type' => null,
50+
]);
51+
}
52+
}

0 commit comments

Comments
 (0)