Skip to content

Commit

Permalink
Merge pull request #217 from AyoobMH/refactor-tests-to-pest
Browse files Browse the repository at this point in the history
Refactor tests to pest
  • Loading branch information
freekmurze authored Feb 20, 2023
2 parents 3d98f62 + 3f6db04 commit 0aa130e
Show file tree
Hide file tree
Showing 10 changed files with 485 additions and 595 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ jobs:
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction
- name: Execute tests
run: vendor/bin/phpunit
run: vendor/bin/pest
10 changes: 7 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
},
"require-dev": {
"orchestra/testbench": "^6.23 | ^7.0 | ^8.0",
"pestphp/pest": "^1.22",
"phpunit/phpunit": "^9.4",
"symfony/var-dumper": "^5.3 | ^6.0"
},
Expand All @@ -40,11 +41,14 @@
}
},
"scripts": {
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
"test": "vendor/bin/pest",
"test-coverage": "vendor/bin/pest --coverage"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"extra": {
"laravel": {
Expand Down
76 changes: 28 additions & 48 deletions tests/AttributeStateTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

namespace Spatie\ModelStates\Tests;

use Spatie\ModelStates\Tests\Dummy\AttributeState\AnotherDirectory\AttributeStateC;
use Spatie\ModelStates\Tests\Dummy\AttributeState\AnotherDirectory\AttributeStateD;
use Spatie\ModelStates\Tests\Dummy\AttributeState\AnotherDirectory\AttributeStateE;
Expand All @@ -10,57 +8,39 @@
use Spatie\ModelStates\Tests\Dummy\AttributeState\AttributeStateTransition;
use Spatie\ModelStates\Tests\Dummy\AttributeState\TestModelWithAttributeState;

class AttributeStateTest extends TestCase
{
/** @test */
public function test_default()
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Not PHP 8');
it('test default', function () {
$model = new TestModelWithAttributeState();

expect($model->state->equals(AttributeStateA::class))->toBeTrue();
})->skip(PHP_VERSION_ID < 80000, 'Not PHP 8');

it('test allowed transition', function () {
$model = new TestModelWithAttributeState();

$model->state->transitionTo(AttributeStateB::class);

expect($model->state->equals(AttributeStateB::class))->toBeTrue();
expect(AttributeStateTransition::$transitioned)->toBeTrue();
})->skip(PHP_VERSION_ID < 80000, 'Not PHP 8');

return;
}
it('should allow transition', function () {
$model = new TestModelWithAttributeState();

$model = new TestModelWithAttributeState();
$model->state->transitionTo(AttributeStateB::class);

$this->assertTrue($model->state->equals(AttributeStateA::class));
}
expect($model->state->equals(AttributeStateB::class))->toBeTrue();
expect(AttributeStateTransition::$transitioned)->toBeTrue();
})->skip(PHP_VERSION_ID < 80000, 'Not PHP 8');

/** @test */
public function test_allowed_transition()
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Not PHP 8');
it('should register states', function () {
$model = new TestModelWithAttributeState();

return;
}
expect(AttributeStateA::config()->registeredStates)->toEqual([AttributeStateC::class, AttributeStateD::class, AttributeStateE::class]);
expect(AttributeStateC::config()->registeredStates)->toEqual([AttributeStateC::class, AttributeStateD::class, AttributeStateE::class]);

$model = new TestModelWithAttributeState();
expect($model->state->equals(AttributeStateA::class))->toBeTrue();

$model->state->transitionTo(AttributeStateB::class);
$model->state->transitionTo(AttributeStateC::class);

$this->assertTrue($model->state->equals(AttributeStateB::class));
$this->assertTrue(AttributeStateTransition::$transitioned);
}

/** @test */
public function test_registered_states()
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Not PHP 8');

return;
}

$model = new TestModelWithAttributeState();

$this->assertSame([AttributeStateC::class, AttributeStateD::class, AttributeStateE::class], AttributeStateA::config()->registeredStates);
$this->assertSame([AttributeStateC::class, AttributeStateD::class, AttributeStateE::class], AttributeStateC::config()->registeredStates);

$this->assertTrue($model->state->equals(AttributeStateA::class));

$model->state->transitionTo(AttributeStateC::class);

$this->assertTrue($model->state->equals(AttributeStateC::class));
}
}
expect($model->state->equals(AttributeStateC::class))->toBeTrue();
})->skip(PHP_VERSION_ID < 80000, 'Not PHP 8');
48 changes: 16 additions & 32 deletions tests/Dummy/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,35 @@ public function test_where_state()
'state' => StateC::class,
]);

$this->assertEquals(
1,
expect(1)->toEqual(
TestModel::query()
->whereState('state', StateC::class)
->where('id', $model->id)
->count()
);

$this->assertEquals(
1,
expect(1)->toEqual(
TestModel::query()
->whereState('state', StateC::getMorphClass())
->where('id', $model->id)
->count()
);

$this->assertEquals(
1,
expect(1)->toEqual(
TestModel::query()
->whereState('state', [StateA::class, StateC::class])
->where('id', $model->id)
->count()
);

$this->assertEquals(
0,
expect(0)->toEqual(
TestModel::query()
->whereState('state', StateA::class)
->where('id', $model->id)
->count()
);

$this->assertEquals(
0,
expect(0)->toEqual(
TestModel::query()
->whereState('state', [StateA::class, StateB::class])
->where('id', $model->id)
Expand All @@ -65,40 +60,35 @@ public function test_where_not_state()
'state' => StateC::class,
]);

$this->assertEquals(
0,
expect(0)->toEqual(
TestModel::query()
->whereNotState('state', StateC::class)
->where('id', $model->id)
->count()
);

$this->assertEquals(
0,
expect(0)->toEqual(
TestModel::query()
->whereNotState('state', StateC::getMorphClass())
->where('id', $model->id)
->count()
);

$this->assertEquals(
0,
expect(0)->toEqual(
TestModel::query()
->whereNotState('state', [StateA::class, StateC::class])
->where('id', $model->id)
->count()
);

$this->assertEquals(
1,
expect(1)->toEqual(
TestModel::query()
->whereNotState('state', StateA::class)
->where('id', $model->id)
->count()
);

$this->assertEquals(
1,
expect(1)->toEqual(
TestModel::query()
->whereNotState('state', [StateA::class, StateB::class])
->where('id', $model->id)
Expand All @@ -112,26 +102,23 @@ public function test_or_where_state()
$modelOne = TestModel::create([ 'state' => StateB::class ]);
$modelTwo = TestModel::create([ 'state' => StateC::class ]);

$this->assertEquals(
0,
expect(0)->toEqual(
TestModel::query()
->whereState('state', StateA::class)
->orWhereState('state', StateC::class)
->where('id', $modelOne->id)
->count()
);

$this->assertEquals(
1,
expect(1)->toEqual(
TestModel::query()
->whereState('state', StateA::class)
->orWhereState('state', StateC::class)
->where('id', $modelTwo->id)
->count()
);

$this->assertEquals(
2,
expect(2)->toEqual(
TestModel::query()
->whereState('state', StateB::class)
->orWhereState('state', StateC::class)
Expand All @@ -145,26 +132,23 @@ public function test_or_where_not_state()
$modelOne = TestModel::create([ 'state' => StateB::class ]);
$modelTwo = TestModel::create([ 'state' => StateC::class ]);

$this->assertEquals(
1,
expect(1)->toEqual(
TestModel::query()
->whereState('state', StateA::class)
->orWhereNotState('state', StateC::class)
->where('id', $modelOne->id)
->count()
);

$this->assertEquals(
0,
expect(0)->toEqual(
TestModel::query()
->whereState('state', StateA::class)
->orWhereNotState('state', StateC::class)
->where('id', $modelTwo->id)
->count()
);

$this->assertEquals(
2,
expect(2)->toEqual(
TestModel::query()
->whereNotState('state', StateD::class)
->orWhereNotState('state', StateA::class)
Expand Down
23 changes: 23 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Spatie\ModelStates\Tests\TestCase;

/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
*/

uses(TestCase::class)->in('.');

/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
*/

/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
*/
Loading

0 comments on commit 0aa130e

Please sign in to comment.