Skip to content

Commit

Permalink
Merge pull request #32 from milwad-dev/fix-tests
Browse files Browse the repository at this point in the history
[1.x] Fix tests
  • Loading branch information
milwad-dev authored Jun 2, 2024
2 parents 7eadfd7 + aecaf7e commit 34e9435
Show file tree
Hide file tree
Showing 11 changed files with 450 additions and 514 deletions.
2 changes: 0 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
executionOrder="depends,defects"
requireCoverageMetadata="true"
beStrictAboutCoverageMetadata="true"
beStrictAboutOutputDuringTests="true"
failOnRisky="true"
failOnWarning="true">
Expand Down
4 changes: 2 additions & 2 deletions src/Datas/OptionData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@

class OptionData
{
public const EXIT_OPTION = 'Exit';
public const SEEDER_OPTION = 'Seeder';
public const FACTORY_OPTION = 'Factory';
public const REPOSITORY_OPTION = 'Repository';
public const SERVICE_OPTION = 'Service';
public const TEST_OPTION = 'Tests';
public const EXIT_OPTION = 'Exit';

public static array $options = [
self::EXIT_OPTION,
self::SEEDER_OPTION,
self::FACTORY_OPTION,
self::REPOSITORY_OPTION,
self::SERVICE_OPTION,
self::TEST_OPTION,
self::EXIT_OPTION,
];
}
12 changes: 6 additions & 6 deletions src/Traits/CommonTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trait CommonTrait
*/
private function extraOption(): string|array
{
return $this->choice('Do you want something extra?', OptionData::$options, 5);
return $this->choice('Do you want something extra?', OptionData::$options, 0);
}

/**
Expand All @@ -27,23 +27,23 @@ public function extraOptionOperation(string $name_uc)
{
$selectOption = $this->extraOption();

if ($selectOption === OptionData::SEEDER_OPTION) {
if (OptionData::$options[$selectOption] === OptionData::SEEDER_OPTION) {
$this->makeSeeder($name_uc);
$this->extraOptionOperation($name_uc);
}
if ($selectOption === OptionData::FACTORY_OPTION) {
if (OptionData::$options[$selectOption] === OptionData::FACTORY_OPTION) {
$this->makeFactory($name_uc);
$this->extraOptionOperation($name_uc);
}
if ($selectOption === OptionData::REPOSITORY_OPTION) {
if (OptionData::$options[$selectOption] === OptionData::REPOSITORY_OPTION) {
$this->makeRepository($name_uc);
$this->extraOptionOperation($name_uc);
}
if ($selectOption === OptionData::SERVICE_OPTION) {
if (OptionData::$options[$selectOption] === OptionData::SERVICE_OPTION) {
$this->makeService($name_uc);
$this->extraOptionOperation($name_uc);
}
if ($selectOption === OptionData::TEST_OPTION) {
if (OptionData::$options[$selectOption] === OptionData::TEST_OPTION) {
$this->makeTest($name_uc);
$this->extraOptionOperation($name_uc);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/QueryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private function addDBColumnsToString(array $itemsDB)
$columns = '';
$excepts = config('laravel-crod.queries.except_columns_in_fillable', ['id', 'updated_at', 'created_at']);

if (!is_array($excepts)) {
if (! is_array($excepts)) {
throw new \RuntimeException('Except columns is not an array');
}

Expand Down
30 changes: 0 additions & 30 deletions tests/BaseTest.php

This file was deleted.

171 changes: 171 additions & 0 deletions tests/Commands/MakeCrudCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<?php

namespace Milwad\LaravelCrod\Tests\Commands;

use Milwad\LaravelCrod\Tests\TestCase;
use PHPUnit\Framework\Attributes\Test;

class MakeCrudCommandTest extends TestCase
{
/**
* Test crud files created successfully.
*/
#[Test]
public function crud_files_created_successfully(): void
{
$this->artisan('crud:make', ['name' => 'Product'])
->expectsQuestion('Do you want something extra?', 0)
->assertSuccessful()
->expectsOutput('Crud files successfully generated...');

$this->ensureCrudFileCreated();
}

/**
* Test crud files created successfully with seeder.
*/
#[Test]
public function crud_files_created_successfully_with_seeder(): void
{
$this->artisan('crud:make', ['name' => 'Product'])
->expectsQuestion('Do you want something extra?', 1)
->expectsQuestion('Do you want something extra?', 0)
->assertSuccessful()
->expectsOutput('Crud files successfully generated...');

$this->ensureCrudFileCreated();

$this->assertFileExists(database_path('seeders/ProductSeeder.php'));
}

/**
* Test crud files created successfully with factory.
*/
#[Test]
public function crud_files_created_successfully_with_factory(): void
{
$this->artisan('crud:make', ['name' => 'Product'])
->expectsQuestion('Do you want something extra?', 2)
->expectsQuestion('Do you want something extra?', 0)
->assertSuccessful()
->expectsOutput('Crud files successfully generated...');

$this->ensureCrudFileCreated();

$this->assertFileExists(database_path('factories/ProductFactory.php'));
}

/**
* Test crud files created successfully with repository.
*/
#[Test]
public function crud_files_created_successfully_with_repository(): void
{
$this->artisan('crud:make', ['name' => 'Product'])
->expectsQuestion('Do you want something extra?', 3)
->expectsQuestion('Do you want something extra?', 0)
->assertSuccessful()
->expectsOutput('Crud files successfully generated...');

$this->ensureCrudFileCreated();

$this->assertFileExists(app_path('Repositories/ProductRepo.php'));
}

/**
* Test crud files created successfully with service.
*/
#[Test]
public function crud_files_created_successfully_with_service(): void
{
$this->artisan('crud:make', ['name' => 'Product'])
->expectsQuestion('Do you want something extra?', 4)
->expectsQuestion('Do you want something extra?', 0)
->assertSuccessful()
->expectsOutput('Crud files successfully generated...');

$this->ensureCrudFileCreated();

$this->assertFileExists(app_path('Services/ProductService.php'));
}

/**
* Test crud files created successfully with tests.
*/
#[Test]
public function crud_files_created_successfully_with_tests(): void
{
$this->artisan('crud:make', ['name' => 'Product'])
->expectsQuestion('Do you want something extra?', 5)
->expectsQuestion('Do you want something extra?', 0)
->assertSuccessful()
->expectsOutput('Crud files successfully generated...');

$this->ensureCrudFileCreated();

$this->assertFileExists(base_path('tests/Feature/ProductTest.php'));
$this->assertFileExists(base_path('tests/Unit/ProductTest.php'));
}

/**
* Test crud files created successfully with tests.
*/
#[Test]
public function crud_files_created_successfully_with_tests_using_pest(): void
{
config()->set(['laravel-crod.are_using_pest', true]);

$this->artisan('crud:make', ['name' => 'Product'])
->expectsQuestion('Do you want something extra?', 5)
->expectsQuestion('Do you want something extra?', 0)
->assertSuccessful()
->expectsOutput('Crud files successfully generated...');

$this->ensureCrudFileCreated();

$this->assertFileExists(base_path('tests/Feature/ProductTest.php'));
$this->assertFileExists(base_path('tests/Unit/ProductTest.php'));
}

/**
* Ensure the crud files successfully created.
*/
protected function ensureCrudFileCreated(): void
{
// Model
$this->assertFileExists(app_path('Models/Product.php'));

// Migration
$this->migrationExists('create_products_table');

// Controller
$this->assertFileExists(app_path('Http/Controllers/ProductController.php'));

// Requests
$this->assertFileExists(app_path('Http/Requests/ProductStoreRequest.php'));
$this->assertFileExists(app_path('Http/Requests/ProductUpdateRequest.php'));

// View
$this->assertFileExists(resource_path('views/products/index.blade.php'));
$this->assertFileExists(resource_path('views/products/create.blade.php'));
$this->assertFileExists(resource_path('views/products/edit.blade.php'));
}

/**
* Check migration file exists.
*/
protected function migrationExists(string $mgr): bool
{
$path = database_path('migrations/');
$files = scandir($path);

foreach ($files as &$value) {
$pos = strpos($value, $mgr);
if ($pos !== false) {
return true;
}
}

return false;
}
}
Loading

0 comments on commit 34e9435

Please sign in to comment.