WebErpMesv2 icon indicating copy to clipboard operation
WebErpMesv2 copied to clipboard

[TEST] Build some unit tests

Open SMEWebify opened this issue 1 year ago • 9 comments
trafficstars

Use existing factorys

First step Acounting model

SMEWebify avatar Apr 29 '24 11:04 SMEWebify

namespace Tests\Unit;

use App\Models\Accounting\AccountingDelivery;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AccountingDeliveryTest extends TestCase
{
    use RefreshDatabase; // Cela s'assure que la base de données est réinitialisée pour chaque test

    public function testCreateAccountingDeliveryWithFactoryDefaults()
    {
        // Créer une instance de AccountingDelivery en utilisant la factory sans arguments supplémentaires
        $delivery = AccountingDelivery::factory()->create();

        // Vérifier que l'instance a été sauvegardée dans la base de données avec les attributs générés par la factory
        $this->assertDatabaseHas('accounting_deliveries', [
            'code' => $delivery->code,
            'label' => $delivery->label,
        ]);

        // Vérifier que les attributs correspondent à ceux générés par la factory
        $this->assertContains($delivery->code, ['FREE_SHIPPING', 'TRANSPORT_COURIER', 'TRANSPORT_CARGO']);
        $this->assertEquals($delivery->code, $delivery->label);
    }
}

SMEWebify avatar Apr 29 '24 11:04 SMEWebify

namespace Tests\Unit;

use App\Models\Accounting\AccountingPaymentConditions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AccountingPaymentConditionsTest extends TestCase
{
    use RefreshDatabase; // Assure que chaque test est exécuté avec une base de données fraîche

    /**
     * Test the creation of AccountingPaymentConditions using the factory.
     */
    public function testCreateAccountingPaymentConditions()
    {
        // Créer une instance en utilisant la factory
        $paymentCondition = AccountingPaymentConditions::factory()->create();

        // Vérifier que l'instance est bien enregistrée dans la base de données
        $this->assertDatabaseHas('accounting_payment_conditions', [
            'code' => $paymentCondition->code,
            'label' => $paymentCondition->label,
            'number_of_month' => $paymentCondition->number_of_month,
            'number_of_day' => $paymentCondition->number_of_day,
            'month_end' => $paymentCondition->month_end
        ]);

        // Vérifier les attributs pour s'assurer qu'ils correspondent aux données générées par la factory
        $this->assertEquals($paymentCondition->code, $paymentCondition->label);
        $this->assertContains($paymentCondition->code, ['NODEF', '30FDM15', '30FDM', '30NET', '45FDM']);
        $this->assertContains($paymentCondition->month_end, [1, 2]);
        $this->assertIsNumeric($paymentCondition->number_of_month);
        $this->assertIsNumeric($paymentCondition->number_of_day);
    }
}

SMEWebify avatar Apr 29 '24 11:04 SMEWebify

namespace Tests\Unit;

use App\Models\Accounting\AccountingPaymentMethod;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AccountingPaymentMethodTest extends TestCase
{
    use RefreshDatabase; // Assure que chaque test est exécuté avec une base de données fraîche

    /**
     * Test the creation of AccountingPaymentMethod using the factory.
     */
    public function testCreateAccountingPaymentMethod()
    {
        // Créer une instance en utilisant la factory
        $paymentMethod = AccountingPaymentMethod::factory()->create();

        // Vérifier que l'instance est bien enregistrée dans la base de données
        $this->assertDatabaseHas('accounting_payment_methods', [
            'code' => $paymentMethod->code,
            'label' => $paymentMethod->label,
            'code_account' => $paymentMethod->code_account,
        ]);

        // Vérifier les attributs pour s'assurer qu'ils correspondent aux données générées par la factory
        $this->assertEquals($paymentMethod->code, $paymentMethod->label);
        $this->assertContains($paymentMethod->code, ['CACHE', 'BANK_CARD', 'BANCK_TRANSFER']);
        $this->assertIsNumeric($paymentMethod->code_account);
    }

SMEWebify avatar Apr 29 '24 11:04 SMEWebify

namespace Tests\Unit;

use App\Models\Accounting\AccountingVat;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AccountingVatTest extends TestCase
{
    use RefreshDatabase; // Assure que chaque test est exécuté avec une base de données fraîche

    /**
     * Test the creation of AccountingVat using the factory.
     */
    public function testCreateAccountingVat()
    {
        // Créer une instance en utilisant la factory
        $vat = AccountingVat::factory()->create();

        // Vérifier que l'instance est bien enregistrée dans la base de données
        $this->assertDatabaseHas('accounting_vats', [
            'code' => $vat->code,
            'label' => $vat->label,
            'rate' => $vat->rate,
        ]);

        // Vérifier les attributs pour s'assurer qu'ils correspondent aux données générées par la factory
        $this->assertEquals($vat->code, $vat->label);
        $this->assertContains($vat->code, ['TVA0', 'TVA5', 'TVA10', 'TVA20']);
        $this->assertContains($vat->rate, [0, 5, 10, 20]);
    }
}

SMEWebify avatar Apr 29 '24 12:04 SMEWebify

Some error with php unit

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - phpunit/php-code-coverage 10.0 requires **phpunit/php-file-iterator ^3.0.3** -> found phpunit/php-file-iterator[3.0.3, ..., 3.0.x-dev] but the package 
is fixed to 4.0 (lock file version) by a partial update and that version does not match. Make sure you list it as an argument for the update command.    
    - phpunit/phpunit 10.0.0 requires phpunit/php-code-coverage ^10.0 -> satisfiable by phpunit/php-code-coverage[10.0].
    - Root composer.json requires phpunit/phpunit 10.0 -> satisfiable by phpunit/phpunit[10.0.0].

Use the option --with-all-dependencies (-W) to allow upgrades, downgrades and removals for packages currently locked to specific versions.

Installation failed, reverting ./composer.json and ./composer.lock to their original content.
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - Root composer.json requires phpunit/phpunit 10.0 -> satisfiable by phpunit/phpunit[10.0.0].
    - phpunit/phpunit 10.0.0 requires **phpunit/php-file-iterator ^4.0** -> found phpunit/php-file-iterator[4.0.0, ..., 4.1.x-dev] but the package is fixed to 3.0.3 (lock file version) by a partial update and that version does not match. Make sure you list it as an argument for the update command.

Use the option --with-all-dependencies (-W) to allow upgrades, downgrades and removals for packages currently locked to specific versions.

Installation failed, reverting ./composer.json and ./composer.lock to their original content.

phpunit/php-code-coverage need php-file-iterator 3.0.3 phpunit/phpunit 10.0.0 requires need phpunit/php-file-iterator 4.0

How do I get out of there?

SMEWebify avatar May 02 '24 20:05 SMEWebify

composer update --with-all-dependencies Loading composer repositories with package information Updating dependencies Lock file operations: 0 installs, 16 updates, 0 removals

  • Downgrading phpunit/php-code-coverage (10.0 => 9.2.31)
  • Upgrading phpunit/php-file-iterator (3.0.3 => 3.0.6)
  • Downgrading phpunit/php-invoker (4.0 => 3.1.1)
  • Downgrading phpunit/php-text-template (3.0 => 2.0.4)
  • Downgrading phpunit/php-timer (6.0 => 5.0.3)
  • Downgrading sebastian/cli-parser (2.0 => 1.0.2)
  • Downgrading sebastian/code-unit (2.0 => 1.0.8)
  • Downgrading sebastian/comparator (5.0 => 4.0.8)
  • Downgrading sebastian/diff (5.0 => 4.0.6)
  • Downgrading sebastian/environment (6.0 => 5.1.5)
  • Downgrading sebastian/exporter (5.0 => 4.0.6)
  • Downgrading sebastian/global-state (6.0 => 5.0.7)
  • Downgrading sebastian/object-enumerator (5.0 => 4.0.4)
  • Downgrading sebastian/recursion-context (5.0 => 4.0.5)
  • Downgrading sebastian/type (4.0 => 3.2.1)
  • Downgrading sebastian/version (4.0 => 3.0.2)

SMEWebify avatar May 02 '24 20:05 SMEWebify

composer update phpunit/phpunit phpunit/php-code-coverage --with-all-dependencies Loading composer repositories with package information Updating dependencies Nothing to modify in lock file Installing dependencies from lock file (including require-dev) Nothing to install, update or remove

SMEWebify avatar May 02 '24 20:05 SMEWebify

./vendor/bin/phpunit --version PHPUnit 9.6.19 by Sebastian Bergmann and contributors.

SMEWebify avatar May 02 '24 20:05 SMEWebify

php artisan test --without-tty

NunoMaduro\Collision\Adapters\Laravel\Exceptions\RequirementsException

Running Collision 7.x artisan test command requires at least PHPUnit 10.x.

SMEWebify avatar May 02 '24 20:05 SMEWebify

PASS Tests\Feature\AccountingDeliveryTest ✓ create accounting delivery with factory defaults 8.98s

PASS Tests\Feature\AccountingPaymentConditionsTest ✓ create accounting payment conditions 0.07s

PASS Tests\Feature\AccountingPaymentMethodTest ✓ create accounting payment method 0.06s

PASS Tests\Feature\AccountingVatTest ✓ create accounting vat 0.05s

SMEWebify avatar Sep 25 '24 21:09 SMEWebify