Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] Fix tests #32

Merged
merged 30 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
58a3750
remove old tests
milwad-dev Apr 14, 2024
d6ea2ae
Create MakeCrudCommandTest.php
milwad-dev Apr 15, 2024
5f7ad2e
wip
milwad-dev Apr 15, 2024
8f7dbf9
Update OptionData.php
milwad-dev Apr 15, 2024
6690f22
complete crud_files_created_successfully
milwad-dev Apr 15, 2024
549c3d4
wip
milwad-dev Apr 15, 2024
b6a7db4
wip
milwad-dev May 5, 2024
14aa84a
Update MakeCrudCommandTest.php
milwad-dev May 5, 2024
35fe201
add `crud_files_created_successfully_with_factory` test
milwad-dev May 5, 2024
6969070
add `crud_files_created_successfully_with_repository` test
milwad-dev May 5, 2024
decbd4b
add `crud_files_created_successfully_with_service` test
milwad-dev May 5, 2024
24b0010
add `crud_files_created_successfully_with_tests` test
milwad-dev May 5, 2024
977086e
add `crud_files_created_successfully_with_tests_using_pest` test
milwad-dev May 5, 2024
0dd7bb5
Create MakeQueryCommandTest.php
milwad-dev May 5, 2024
d35a4ff
Update MakeQueryCommandTest.php
milwad-dev May 5, 2024
cccca9e
wip
milwad-dev May 9, 2024
c966635
Create MakeCrudModuleCommandTest.php
milwad-dev May 9, 2024
8c02997
add `crud_files_created_successfully_with_seeder` test
milwad-dev May 9, 2024
2780c3b
add `crud_files_created_successfully_with_factory` test
milwad-dev May 9, 2024
5331615
add `crud_files_created_successfully_with_repository` test
milwad-dev May 9, 2024
c55eaf0
add `crud_files_created_successfully_with_service` test
milwad-dev May 9, 2024
3103188
add `crud_files_created_successfully_with_tests` test
milwad-dev May 9, 2024
89c761a
add `crud_files_created_successfully_with_customize_from_config_file`…
milwad-dev May 9, 2024
daf2692
Delete MakeQueryCommandTest.php
milwad-dev May 9, 2024
2bf840f
Merge branch '1.x' of https://github.com/milwad-dev/laravel-crod into…
milwad-dev May 30, 2024
1d3b2dd
add `all_files_are_exists_with_correct_data` test
milwad-dev Jun 1, 2024
f3a371e
Update MakeQueryCommandTest.php
milwad-dev Jun 1, 2024
9c6acf7
Update MakeQueryCommandTest.php
milwad-dev Jun 1, 2024
8fdaac2
set getEnvironmentSetUp
milwad-dev Jun 1, 2024
aecaf7e
Delete MakeQueryCommandTest.php
milwad-dev Jun 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading