Skip to content

Commit

Permalink
Added package Test Cases using sqlite DB connection
Browse files Browse the repository at this point in the history
Added package Test Cases using sqlite DB connection
  • Loading branch information
choxx authored May 24, 2023
2 parents 7074616 + 7e9493d commit 35dfb58
Show file tree
Hide file tree
Showing 11 changed files with 371 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
vendor/*
composer.lock
.idea/*
.idea/*
.phpunit.result.cache
storage/*
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Empty file.
56 changes: 56 additions & 0 deletions examples/tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

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;
use Luezoid\Laravelcore\Http\Controllers\FileController;

require_once __DIR__.'/../Controllers/MinionController.php';
require_once __DIR__.'/../Repositories/MinionRepository.php';

class TestCase extends \Orchestra\Testbench\TestCase
{
/**
* 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 (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();
}


}
43 changes: 43 additions & 0 deletions examples/tests/suite/DeleteAPISuccessTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Tests\Suite;

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.
$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,
]);
}
}
58 changes: 58 additions & 0 deletions examples/tests/suite/FileAPISuccessTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Tests\Suite;

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';
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(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',
]);
}
}
44 changes: 44 additions & 0 deletions examples/tests/suite/GetAPISuccessTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Tests\Suite;

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.
$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,
]);
}
}
51 changes: 51 additions & 0 deletions examples/tests/suite/IndexAPISuccessTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Tests\Suite;
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.
$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,
]);
}
}
36 changes: 36 additions & 0 deletions examples/tests/suite/PostAPISuccessTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Tests\Suite;

use Tests\TestCase;

require_once __DIR__ . '/../TestCase.php';
require_once __DIR__ . '/../../Models/Minion.php';
require_once __DIR__ . '/../../Requests/MinionCreateRequest.php';

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,
]);
}
}
52 changes: 52 additions & 0 deletions examples/tests/suite/PutAPISuccessTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Tests\Suite;

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.
$this->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,
]);
}
}
23 changes: 23 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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>./examples/tests/suite</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="test"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="DB_CONNECTION" value="test"/>
<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>

0 comments on commit 35dfb58

Please sign in to comment.