Skip to content

Commit

Permalink
test: add separate test classes for mixins
Browse files Browse the repository at this point in the history
  • Loading branch information
bensherred committed Nov 28, 2023
1 parent 613f3fd commit 5c0209b
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 65 deletions.
29 changes: 29 additions & 0 deletions tests/Mixins/FindBySqidMixinTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

use Workbench\App\Models\Customer;
use Workbench\Database\Factories\CustomerFactory;

it(description: 'can find a model by its sqid', closure: function (): void {
$customer = CustomerFactory::new()->create();
$sqid = $customer->sqid;

expect(Customer::findBySqid(sqid: $sqid))
->toBeInstanceOf(Customer::class)
->is($customer)->toBeTrue();
});

it(description: 'can find a model by its sqid from a specific column', closure: function (): void {
$customer = CustomerFactory::new()->create();
$sqid = $customer->sqid;

expect(Customer::findBySqid(sqid: $sqid, columns: ['id']))
->toBeInstanceOf(Customer::class)
->is($customer)->toBeTrue();
});

it(description: 'returns null if it cannot find a model with the given sqid', closure: function (): void {
expect(Customer::findBySqid(sqid: 'missing-sqid'))
->toBeNull();
});
33 changes: 33 additions & 0 deletions tests/Mixins/FindBySqidOrFailMixinTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Eloquent\ModelNotFoundException;
use Workbench\App\Models\Customer;
use Workbench\Database\Factories\CustomerFactory;

it(description: 'can find or fail a model by its sqid', closure: function (): void {
$customer = CustomerFactory::new()->create();
$sqid = $customer->sqid;

expect(Customer::findBySqidOrFail(sqid: $sqid))
->toBeInstanceOf(Customer::class)
->is($customer)->toBeTrue();

$this->expectException(ModelNotFoundException::class);

Customer::findBySqidOrFail(sqid: 'missing-sqid');
});

it(description: 'can find or fail a model by its sqid from a specific column', closure: function (): void {
$customer = CustomerFactory::new()->create();
$sqid = $customer->sqid;

expect(Customer::findBySqidOrFail(sqid: $sqid, columns: ['id']))
->toBeInstanceOf(Customer::class)
->is($customer)->toBeTrue();

$this->expectException(ModelNotFoundException::class);

Customer::findBySqidOrFail(sqid: 'missing-sqid', columns: ['id']);
});
23 changes: 23 additions & 0 deletions tests/Mixins/WhereSqidInMixinTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Eloquent\Collection;
use Workbench\App\Models\Customer;
use Workbench\Database\Factories\CustomerFactory;

it(description: 'can query all models with the given sqids', closure: function (): void {
$customer = CustomerFactory::new()->create();

CustomerFactory::new()->count(count: 10)->create();

$customers = Customer::query()
->whereSqidIn(column: 'id', sqids: [$customer->sqid])
->get();

expect($customers)
->toBeInstanceOf(class: Collection::class)
->toHaveCount(count: 1)
->first()->toBeInstanceOf(class: Customer::class)
->first()->is($customer);
});
23 changes: 23 additions & 0 deletions tests/Mixins/WhereSqidMixinTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Eloquent\Collection;
use Workbench\App\Models\Customer;
use Workbench\Database\Factories\CustomerFactory;

it(description: 'can query a model by its sqid', closure: function (): void {
$customer = CustomerFactory::new()->create();

CustomerFactory::new()->count(count: 10)->create();

$customers = Customer::query()
->whereSqid(sqid: $customer->sqid)
->get();

expect($customers)
->toBeInstanceOf(class: Collection::class)
->toHaveCount(count: 1)
->first()->toBeInstanceOf(class: Customer::class)
->first()->is($customer);
});
22 changes: 22 additions & 0 deletions tests/Mixins/WhereSqidNotInMixinTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Eloquent\Collection;
use Workbench\App\Models\Customer;
use Workbench\Database\Factories\CustomerFactory;

it(description: 'can query all models except the given sqids', closure: function (): void {
$customer = CustomerFactory::new()->create();

CustomerFactory::new()->count(count: 10)->create();

$customers = Customer::query()
->whereSqidNotIn(column: 'id', sqids: [$customer->sqid])
->get();

expect($customers)
->toBeInstanceOf(class: Collection::class)
->toHaveCount(count: 10)
->pluck('id')->not->toContain($customer->id);
});
65 changes: 0 additions & 65 deletions tests/MixinsTest.php

This file was deleted.

0 comments on commit 5c0209b

Please sign in to comment.