From efb44f9e09ee271b15d30513c8f7939bd12b24fa Mon Sep 17 00:00:00 2001 From: Ayoob Mohammed Date: Thu, 1 Dec 2022 16:25:56 +0300 Subject: [PATCH 1/2] refactor tests --- .github/workflows/run-tests.yml | 2 +- .gitignore | 1 + composer.json | 10 +- tests/AttributeStateTest.php | 76 +++---- tests/Dummy/QueryBuilderTest.php | 48 ++--- tests/Pest.php | 23 +++ tests/StateCastingTest.php | 281 ++++++++++++-------------- tests/StateTest.php | 330 ++++++++++++++----------------- tests/TestCase.php | 2 +- tests/TransitionTest.php | 241 ++++++++++------------ tests/ValidationRuleTest.php | 67 +++---- 11 files changed, 486 insertions(+), 595 deletions(-) create mode 100644 tests/Pest.php diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index cd50a62e..54bbf8df 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -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 diff --git a/.gitignore b/.gitignore index 543318a3..7a3e63f6 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ vendor coverage .phpunit.result.cache .php-cs-fixer.cache +.idea diff --git a/composer.json b/composer.json index 2121a7f1..695b4275 100644 --- a/composer.json +++ b/composer.json @@ -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" }, @@ -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": { diff --git a/tests/AttributeStateTest.php b/tests/AttributeStateTest.php index cb98911f..01c83b75 100644 --- a/tests/AttributeStateTest.php +++ b/tests/AttributeStateTest.php @@ -1,7 +1,5 @@ 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'); diff --git a/tests/Dummy/QueryBuilderTest.php b/tests/Dummy/QueryBuilderTest.php index eeb91374..bb6a671b 100644 --- a/tests/Dummy/QueryBuilderTest.php +++ b/tests/Dummy/QueryBuilderTest.php @@ -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) @@ -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) @@ -112,8 +102,7 @@ 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) @@ -121,8 +110,7 @@ public function test_or_where_state() ->count() ); - $this->assertEquals( - 1, + expect(1)->toEqual( TestModel::query() ->whereState('state', StateA::class) ->orWhereState('state', StateC::class) @@ -130,8 +118,7 @@ public function test_or_where_state() ->count() ); - $this->assertEquals( - 2, + expect(2)->toEqual( TestModel::query() ->whereState('state', StateB::class) ->orWhereState('state', StateC::class) @@ -145,8 +132,7 @@ 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) @@ -154,8 +140,7 @@ public function test_or_where_not_state() ->count() ); - $this->assertEquals( - 0, + expect(0)->toEqual( TestModel::query() ->whereState('state', StateA::class) ->orWhereNotState('state', StateC::class) @@ -163,8 +148,7 @@ public function test_or_where_not_state() ->count() ); - $this->assertEquals( - 2, + expect(2)->toEqual( TestModel::query() ->whereNotState('state', StateD::class) ->orWhereNotState('state', StateA::class) diff --git a/tests/Pest.php b/tests/Pest.php new file mode 100644 index 00000000..0a69b25e --- /dev/null +++ b/tests/Pest.php @@ -0,0 +1,23 @@ +in('.'); + +/* +|-------------------------------------------------------------------------- +| Expectations +|-------------------------------------------------------------------------- +*/ + +/* +|-------------------------------------------------------------------------- +| Functions +|-------------------------------------------------------------------------- +*/ diff --git a/tests/StateCastingTest.php b/tests/StateCastingTest.php index e6bb173e..4437bffe 100644 --- a/tests/StateCastingTest.php +++ b/tests/StateCastingTest.php @@ -1,7 +1,5 @@ StateA::class, - ]); - - $this->assertInstanceOf(StateA::class, $model->state); - - $this->assertDatabaseHas($model->getTable(), [ - 'state' => StateA::getMorphClass(), - ]); - } - - /** @test */ - public function custom_registered_state_without_alias_is_serialized_on_create() - { - $model = TestModel::create([ - 'state' => StateF::class, - ]); - - $this->assertInstanceOf(StateF::class, $model->state); - - $this->assertDatabaseHas($model->getTable(), [ - 'state' => StateF::getMorphClass(), - ]); - } - - /** @test */ - public function state_with_alias_is_serialized_on_create_when_using_class_name() - { - $model = TestModel::create([ - 'state' => StateC::class, - ]); - - $this->assertInstanceOf(StateC::class, $model->state); - - $this->assertDatabaseHas($model->getTable(), [ - 'state' => StateC::getMorphClass(), - ]); - } - - /** @test */ - public function custom_registered_state_with_alias_is_serialized_on_create_when_using_class_name() - { - $model = TestModel::create([ - 'state' => StateG::class, - ]); - - $this->assertInstanceOf(StateG::class, $model->state); - - $this->assertDatabaseHas($model->getTable(), [ - 'state' => StateG::getMorphClass(), - ]); - } - - /** @test */ - public function state_with_alias_is_serialized_on_create_when_using_alias() - { - $model = TestModel::create([ - 'state' => StateC::getMorphClass(), - ]); - - $this->assertInstanceOf(StateC::class, $model->state); - - $this->assertDatabaseHas($model->getTable(), [ - 'state' => StateC::getMorphClass(), - ]); - } - - /** @test */ - public function custom_registered_state_with_alias_is_serialized_on_create_when_using_alias() - { - $model = TestModel::create([ - 'state' => StateG::getMorphClass(), - ]); - - $this->assertInstanceOf(StateG::class, $model->state); - - $this->assertDatabaseHas($model->getTable(), [ - 'state' => StateG::getMorphClass(), - ]); - } - - /** @test */ - public function state_is_immediately_unserialized_on_property_set() - { - $model = new TestModel(); - - $model->state = StateA::class; - - $this->assertInstanceOf(StateA::class, $model->state); - } - - /** @test */ - public function state_is_immediately_unserialized_on_model_construction() - { - $model = new TestModel([ - 'state' => StateA::class, - ]); - - $this->assertInstanceOf(StateA::class, $model->state); - } - - /** @test */ - public function state_is_unserialized_on_fetch() - { - DB::table((new TestModel())->getTable())->insert([ - 'id' => 1, - 'state' => StateA::getMorphClass(), - ]); - - $model = TestModel::find(1); - - $this->assertInstanceOf(StateA::class, $model->state); - } - - /** @test */ - public function default_state_is_set_when_none_provided() - { - $model = (new class() extends TestModel { - public function registerStates(): void - { - $this - ->addState('state', ModelState::class) - ->default(StateA::class); - } - })->create(); - - $this->assertInstanceOf(StateA::class, $model->state); - - $this->assertDatabaseHas($model->getTable(), [ - 'state' => StateA::getMorphClass(), - ]); - } - - /** @test */ - public function field_is_always_populated_when_set() - { - $model = new TestModel(); - - $this->assertInstanceOf(StateA::class, $model->state); - - $model->state = new StateB($model); - - $this->assertInstanceOf(StateB::class, $model->state); - - $this->assertEquals('state', $model->state->getField()); - } -} +it('state without alias is serialized on create', function () { + $model = TestModel::create([ + 'state' => StateA::class, + ]); + + expect($model->state)->toBeInstanceOf(StateA::class); + + $this->assertDatabaseHas($model->getTable(), [ + 'state' => StateA::getMorphClass(), + ]); +}); + +it('custom registered state without alias is serialized on create', function () { + $model = TestModel::create([ + 'state' => StateF::class, + ]); + + expect($model->state)->toBeInstanceOf(StateF::class); + + $this->assertDatabaseHas($model->getTable(), [ + 'state' => StateF::getMorphClass(), + ]); +}); + +it('state with alias is serialized on create when using class name', function () { + $model = TestModel::create([ + 'state' => StateC::class, + ]); + + expect($model->state)->toBeInstanceOf(StateC::class); + + $this->assertDatabaseHas($model->getTable(), [ + 'state' => StateC::getMorphClass(), + ]); +}); + +it('custom registered state with alias is serialized on create when using class name', function () { + $model = TestModel::create([ + 'state' => StateG::class, + ]); + + expect($model->state)->toBeInstanceOf(StateG::class); + + $this->assertDatabaseHas($model->getTable(), [ + 'state' => StateG::getMorphClass(), + ]); +}); + +it('state with alias is serialized on create when using alias', function () { + $model = TestModel::create([ + 'state' => StateC::getMorphClass(), + ]); + + expect($model->state)->toBeInstanceOf(StateC::class); + + $this->assertDatabaseHas($model->getTable(), [ + 'state' => StateC::getMorphClass(), + ]); +}); + +it('custom registered state with alias is serialized on create when using alias', function () { + $model = TestModel::create([ + 'state' => StateG::getMorphClass(), + ]); + + expect($model->state)->toBeInstanceOf(StateG::class); + + $this->assertDatabaseHas($model->getTable(), [ + 'state' => StateG::getMorphClass(), + ]); +}); + +it('state is immediately unserialized on property set', function () { + $model = new TestModel(); + + $model->state = StateA::class; + + expect($model->state)->toBeInstanceOf(StateA::class); +}); + +it('state is immediately unserialized on model construction', function () { + $model = new TestModel([ + 'state' => StateA::class, + ]); + + expect($model->state)->toBeInstanceOf(StateA::class); +}); + +it('state is unserialized on fetch', function () { + DB::table((new TestModel())->getTable())->insert([ + 'id' => 1, + 'state' => StateA::getMorphClass(), + ]); + + $model = TestModel::find(1); + + expect($model->state)->toBeInstanceOf(StateA::class); +}); + +it('default state is set when none provided', function () { + $model = (new class() extends TestModel { + public function registerStates(): void + { + $this + ->addState('state', ModelState::class) + ->default(StateA::class); + } + })->create(); + + expect($model->state)->toBeInstanceOf(StateA::class); + + $this->assertDatabaseHas($model->getTable(), [ + 'state' => StateA::getMorphClass(), + ]); +}); + +it('field is always populated when set', function () { + $model = new TestModel(); + + expect($model->state)->toBeInstanceOf(StateA::class); + + $model->state = new StateB($model); + + expect($model->state)->toBeInstanceOf(StateB::class); + + expect($model->state->getField())->toEqual('state'); +}); diff --git a/tests/StateTest.php b/tests/StateTest.php index 27c260b0..8e8ab4d0 100644 --- a/tests/StateTest.php +++ b/tests/StateTest.php @@ -1,7 +1,5 @@ assertEquals(StateA::class, ModelState::resolveStateClass(StateA::class)); - $this->assertEquals(StateC::class, ModelState::resolveStateClass(StateC::class)); - $this->assertEquals(StateC::class, ModelState::resolveStateClass(StateC::getMorphClass())); - $this->assertEquals(StateC::class, ModelState::resolveStateClass(StateC::$name)); - $this->assertEquals(StateD::class, ModelState::resolveStateClass(StateD::class)); - $this->assertEquals(StateD::class, ModelState::resolveStateClass(StateD::getMorphClass())); - $this->assertEquals(StateD::class, ModelState::resolveStateClass(StateD::$name)); - $this->assertEquals(StateE::class, ModelState::resolveStateClass(StateE::class)); - $this->assertEquals(StateE::class, ModelState::resolveStateClass(StateE::getMorphClass())); - $this->assertEquals(StateF::class, ModelState::resolveStateClass(StateF::getMorphClass())); - $this->assertEquals(StateG::class, ModelState::resolveStateClass(StateG::getMorphClass())); - $this->assertEquals(StateG::class, ModelState::resolveStateClass(StateG::getMorphClass())); - $this->assertEquals(StateH::class, ModelState::resolveStateClass(StateH::getMorphClass())); - $this->assertEquals(StateH::class, ModelState::resolveStateClass(StateH::getMorphClass())); - } - - /** @test */ - public function transitionable_states() - { - $modelA = TestModel::make(['state' => StateA::class]); - - $this->assertEquals([ +it('resolve state class', function () { + expect(ModelState::resolveStateClass(StateA::class))->toEqual(StateA::class); + expect(ModelState::resolveStateClass(StateC::class))->toEqual(StateC::class); + expect(ModelState::resolveStateClass(StateC::getMorphClass()))->toEqual(StateC::class); + expect(ModelState::resolveStateClass(StateC::$name))->toEqual(StateC::class); + expect(ModelState::resolveStateClass(StateD::class))->toEqual(StateD::class); + expect(ModelState::resolveStateClass(StateD::getMorphClass()))->toEqual(StateD::class); + expect(ModelState::resolveStateClass(StateD::$name))->toEqual(StateD::class); + expect(ModelState::resolveStateClass(StateE::class))->toEqual(StateE::class); + expect(ModelState::resolveStateClass(StateE::getMorphClass()))->toEqual(StateE::class); + expect(ModelState::resolveStateClass(StateF::getMorphClass()))->toEqual(StateF::class); + expect(ModelState::resolveStateClass(StateG::getMorphClass()))->toEqual(StateG::class); + expect(ModelState::resolveStateClass(StateG::getMorphClass()))->toEqual(StateG::class); + expect(ModelState::resolveStateClass(StateH::getMorphClass()))->toEqual(StateH::class); + expect(ModelState::resolveStateClass(StateH::getMorphClass()))->toEqual(StateH::class); +}); + +it('transitionable states', function () { + $modelA = TestModel::make(['state' => StateA::class]); + + expect( + [ StateB::getMorphClass(), StateC::getMorphClass(), StateD::getMorphClass(), StateF::getMorphClass(), - ], $modelA->state->transitionableStates()); - - $modelB = TestModelWithDefault::create([ - 'state' => StateC::class, - ]); - - $this->assertEquals([], $modelB->state->transitionableStates()); - } - - /** @test */ - public function transitionable_states_with_custom_transition() - { - $model = TestModelWithCustomTransition::create(['state' => StateX::class]); - $this->assertSame([StateY::class], $model->state->transitionableStates()); - } - - /** @test */ - public function test_equals() - { - $modelA = TestModelWithDefault::create(); - - $modelB = TestModelWithDefault::create(); - - $this->assertTrue($modelA->state->equals($modelB->state)); - - $modelA = TestModelWithDefault::create(); - - $modelB = TestModelWithDefault::create([ - 'state' => StateC::class, - ]); - - $this->assertFalse($modelA->state->equals($modelB->state)); - - $this->assertTrue($modelA->state->equals(StateA::class)); - } - - /** @test */ - public function test_can_transition_to() - { - $state = new StateA(new TestModel()); - $state->setField('state'); - - $this->assertTrue($state->canTransitionTo(StateB::class)); - $this->assertTrue($state->canTransitionTo(StateC::class)); - $this->assertTrue($state->canTransitionTo(StateF::class)); - - $state = new StateB(new TestModel()); - $state->setField('state'); - - $this->assertFalse($state->canTransitionTo(StateB::class)); - $this->assertFalse($state->canTransitionTo(StateA::class)); - } - - /** @test */ - public function test_get_states() - { - $states = TestModelWithDefault::getStates(); - - $this->assertEquals( - [ - 'state' => [ - StateA::getMorphClass(), - StateB::getMorphClass(), - StateC::getMorphClass(), - StateD::getMorphClass(), - StateE::getMorphClass(), - StateF::getMorphClass(), - StateG::getMorphClass(), - StateH::getMorphClass(), - ], - ], - $states->toArray() - ); - } + ] + )->toEqual($modelA->state->transitionableStates()); + + $modelB = TestModelWithDefault::create([ + 'state' => StateC::class, + ]); + + expect($modelB->state->transitionableStates())->toEqual([]); +}); + +it('transitionable states with custom transition', function () { + $model = TestModelWithCustomTransition::create(['state' => StateX::class]); + expect($model->state->transitionableStates())->toBe([StateY::class]); +}); + +it('equals', function () { + $modelA = TestModelWithDefault::create(); + + $modelB = TestModelWithDefault::create(); + + expect($modelA->state->equals($modelB->state))->toBeTrue(); + + $modelA = TestModelWithDefault::create(); + + $modelB = TestModelWithDefault::create([ + 'state' => StateC::class, + ]); + + expect($modelA->state->equals($modelB->state))->toBeFalse(); + + expect($modelA->state->equals(StateA::class))->toBeTrue(); +}); + +it('can transition to', function () { + $state = new StateA(new TestModel()); + $state->setField('state'); - /** @test */ - public function test_get_states_for() - { - $states = TestModelWithDefault::getStatesFor('state'); + expect($state->canTransitionTo(StateB::class))->toBeTrue(); + expect($state->canTransitionTo(StateC::class))->toBeTrue(); + expect($state->canTransitionTo(StateF::class))->toBeTrue(); - $this->assertEquals( - [ + $state = new StateB(new TestModel()); + $state->setField('state'); + + expect($state->canTransitionTo(StateB::class))->toBeFalse(); + expect($state->canTransitionTo(StateA::class))->toBeFalse(); +}); + +it('get states', function () { + $states = TestModelWithDefault::getStates(); + + expect( + [ + 'state' => [ StateA::getMorphClass(), StateB::getMorphClass(), StateC::getMorphClass(), @@ -141,51 +106,60 @@ public function test_get_states_for() StateG::getMorphClass(), StateH::getMorphClass(), ], - $states->toArray() - ); - } - - /** @test */ - public function test_get_default_states() - { - $states = TestModelWithDefault::getDefaultStates(); - - $this->assertEquals( - [ - 'state' => StateA::getMorphClass(), - ], - $states->toArray() - ); - } + ], + )->toEqual($states->toArray()); +}); + +it('get states for', function () { + $states = TestModelWithDefault::getStatesFor('state'); + + expect( + [ + StateA::getMorphClass(), + StateB::getMorphClass(), + StateC::getMorphClass(), + StateD::getMorphClass(), + StateE::getMorphClass(), + StateF::getMorphClass(), + StateG::getMorphClass(), + StateH::getMorphClass(), + ], + )->toEqual($states->toArray()); +}); + +it('get default states', function () { + $states = TestModelWithDefault::getDefaultStates(); + + expect( + [ + 'state' => StateA::getMorphClass(), + ], + )->toEqual($states->toArray()); +}); - /** @test */ - public function test_get_default_states_for() - { - $defaultState = TestModelWithDefault::getDefaultStateFor('state'); +it('get default states for', function () { + $defaultState = TestModelWithDefault::getDefaultStateFor('state'); - $this->assertEquals(StateA::getMorphClass(), $defaultState); - } + expect($defaultState)->toEqual(StateA::getMorphClass()); +}); - /** @test */ - public function test_make() - { - $stateA = ModelState::make(StateA::class, new TestModel()); +it('make', function () { + $stateA = ModelState::make(StateA::class, new TestModel()); - $this->assertInstanceOf(StateA::class, $stateA); + expect($stateA)->toBeInstanceOf(StateA::class); - $stateC = ModelState::make('C', new TestModel()); + $stateC = ModelState::make('C', new TestModel()); - $this->assertInstanceOf(StateC::class, $stateC); + expect($stateC)->toBeInstanceOf(StateC::class); - $stateD = ModelState::make(4, new TestModel()); + $stateD = ModelState::make(4, new TestModel()); - $this->assertInstanceOf(StateD::class, $stateD); - } + expect($stateD)->toBeInstanceOf(StateD::class); +}); - /** @test */ - public function test_all() - { - $this->assertEquals([ +it('all', function () { + expect( + [ StateA::getMorphClass() => StateA::class, StateB::getMorphClass() => StateB::class, StateC::getMorphClass() => StateC::class, @@ -194,53 +168,43 @@ public function test_all() StateF::getMorphClass() => StateF::class, StateG::getMorphClass() => StateG::class, StateH::getMorphClass() => StateH::class, - ], ModelState::all()->toArray()); - } + ] + )->toEqual(ModelState::all()->toArray()); +}); - /** @test */ - public function default_is_set_when_constructing_a_new_model() - { - $model = new TestModel(); +it('default is set when constructing a new model', function () { + $model = new TestModel(); - $this->assertTrue($model->state->equals(StateA::class)); - } + expect($model->state->equals(StateA::class))->toBeTrue(); +}); - /** @test */ - public function default_is_set_when_creating_a_new_model() - { - $model = TestModel::create(); +it('default is set when creating a new model', function () { + $model = TestModel::create(); - $this->assertTrue($model->state->equals(StateA::class)); - } + expect($model->state->equals(StateA::class))->toBeTrue(); +}); - /** @test */ - public function get_model() - { - $stateA = ModelState::make(StateA::class, new TestModel()); +it('get model', function () { + $stateA = ModelState::make(StateA::class, new TestModel()); - $this->assertInstanceOf(TestModel::class, $stateA->getModel()); - } + expect($stateA->getModel())->toBeInstanceOf(TestModel::class); +}); - /** @test */ - public function get_field() - { - $model = TestModel::create(); +it('get field', function () { + $model = TestModel::create(); - $this->assertEquals('state', $model->state->getField()); - } + expect($model->state->getField())->toEqual('state'); +}); - /** @test */ - public function can_override_default_transition() - { - Event::fake(); +it('can override default transition', function () { + Event::fake(); - config()->set( - 'model-states.default_transition', - \Spatie\ModelStates\Tests\Dummy\Transitions\CustomDefaultTransition::class - ); + config()->set( + 'model-states.default_transition', + \Spatie\ModelStates\Tests\Dummy\Transitions\CustomDefaultTransition::class + ); - TestModel::create()->state->transitionTo(StateB::class); + TestModel::create()->state->transitionTo(StateB::class); - Event::assertNotDispatched(TestModelUpdatingEvent::class); - } -} + Event::assertNotDispatched(TestModelUpdatingEvent::class); +}); diff --git a/tests/TestCase.php b/tests/TestCase.php index 0a1a7247..84478018 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -7,7 +7,7 @@ abstract class TestCase extends Orchestra { - public function setUp(): void + protected function setUp(): void { parent::setUp(); diff --git a/tests/TransitionTest.php b/tests/TransitionTest.php index 9cef0658..19b251a6 100644 --- a/tests/TransitionTest.php +++ b/tests/TransitionTest.php @@ -1,7 +1,5 @@ StateA::class, - ]); +it('allowed transition', function () { + $model = TestModel::create([ + 'state' => StateA::class, + ]); - $model->state->transitionTo(StateB::class); + $model->state->transitionTo(StateB::class); - $model->refresh(); + $model->refresh(); - $this->assertInstanceOf(StateB::class, $model->state); - } + expect($model->state)->toBeInstanceOf(StateB::class); +}); - /** @test */ - public function allowed_transition_with_morph_mame() - { - $model = TestModel::create([ - 'state' => StateA::class, - ]); +it('allowed transition with morph mame', function () { + $model = TestModel::create([ + 'state' => StateA::class, + ]); - $model->state->transitionTo(StateD::getMorphClass()); + $model->state->transitionTo(StateD::getMorphClass()); - $model->refresh(); + $model->refresh(); - $this->assertInstanceOf(StateD::class, $model->state); - } + expect($model->state)->toBeInstanceOf(StateD::class); +}); - /** @test */ - public function allowed_transition_configured_with_multiple_from() - { - $modelA = TestModel::create([ - 'state' => StateA::class, - ]); +it('allowed transition configured with multiple from', function () { + $modelA = TestModel::create([ + 'state' => StateA::class, + ]); - $modelA->state->transitionTo(StateC::getMorphClass()); + $modelA->state->transitionTo(StateC::getMorphClass()); - $modelA->refresh(); + $modelA->refresh(); - $this->assertInstanceOf(StateC::class, $modelA->state); + expect($modelA->state)->toBeInstanceOf(StateC::class); - $modelB = TestModel::create([ - 'state' => StateB::class, - ]); + $modelB = TestModel::create([ + 'state' => StateB::class, + ]); - $modelB->state->transitionTo(StateC::getMorphClass()); + $modelB->state->transitionTo(StateC::getMorphClass()); - $modelB->refresh(); + $modelB->refresh(); - $this->assertInstanceOf(StateC::class, $modelB->state); - } + expect($modelB->state)->toBeInstanceOf(StateC::class); +}); - /** @test */ - public function allowed_transition_configured_from_array() - { - $model = TestModelWithTransitionsFromArray::create([ - 'state' => StateA::class, - ]); +it('allowed transition configured from array', function () { + $model = TestModelWithTransitionsFromArray::create([ + 'state' => StateA::class, + ]); - $model->state->transitionTo(StateC::class); + $model->state->transitionTo(StateC::class); - $model->refresh(); + $model->refresh(); - $this->assertInstanceOf(StateC::class, $model->state); - } + expect($model->state)->toBeInstanceOf(StateC::class); +}); - /** @test */ - public function disallowed_transition() - { - $model = TestModel::create([ - 'state' => StateB::class, - ]); +it('disallowed transition', function () { + $model = TestModel::create([ + 'state' => StateB::class, + ]); - $this->expectException(TransitionNotFound::class); + $this->expectException(TransitionNotFound::class); - $model->state->transitionTo(StateA::class); - } + $model->state->transitionTo(StateA::class); +}); - /** @test */ - public function custom_transition_test() - { - $model = TestModelWithCustomTransition::create([ - 'state' => StateX::class, - ]); +it('custom transition test', function () { + $model = TestModelWithCustomTransition::create([ + 'state' => StateX::class, + ]); - $message = 'my message'; + $message = 'my message'; - $model->state->transitionTo(StateY::class, $message); + $model->state->transitionTo(StateY::class, $message); - $model->refresh(); + $model->refresh(); - $this->assertInstanceOf(StateY::class, $model->state); - $this->assertEquals($message, $model->message); - } + expect($model->state)->toBeInstanceOf(StateY::class); + expect($model->message)->toEqual($message); +}); - /** @test */ - public function directly_transition() - { - $model = TestModelWithCustomTransition::create([ - 'state' => StateX::class, - ]); +it('directly transition', function () { + $model = TestModelWithCustomTransition::create([ + 'state' => StateX::class, + ]); - $message = 'my message'; + $message = 'my message'; - $model->state->transition(new CustomTransition($model, $message)); + $model->state->transition(new CustomTransition($model, $message)); - $model->refresh(); + $model->refresh(); - $this->assertInstanceOf(StateY::class, $model->state); - $this->assertEquals($message, $model->message); - } + expect($model->state)->toBeInstanceOf(StateY::class); + expect($model->message)->toEqual($message); +}); - /** @test */ - public function test_cannot_transition() - { - $model = TestModelWithCustomTransition::create([ - 'state' => StateX::class, - ]); +it('test cannot transition', function () { + $model = TestModelWithCustomTransition::create([ + 'state' => StateX::class, + ]); - $this->expectException(TransitionNotAllowed::class); + $this->expectException(TransitionNotAllowed::class); - $model->state->transition(new CustomInvalidTransition($model)); - } + $model->state->transition(new CustomInvalidTransition($model)); +}); - /** @test */ - public function test_custom_transition_blocks_can_transition_to() - { - $model = TestModelWithCustomTransition::create([ - 'state' => StateX::class, - ]); +it('test custom transition blocks can transition to', function () { + $model = TestModelWithCustomTransition::create([ + 'state' => StateX::class, + ]); - $this->assertFalse($model->state->canTransitionTo(StateZ::class)); - } + expect($model->state->canTransitionTo(StateZ::class))->toBeFalse(); +}); - /** @test */ - public function test_custom_transition_doesnt_block_can_transition_to() - { - $model = TestModelWithCustomTransition::create([ - 'state' => StateX::class, - ]); +it('test custom transition doesnt block can transition to', function () { + $model = TestModelWithCustomTransition::create([ + 'state' => StateX::class, + ]); - $this->assertTrue($model->state->canTransitionTo(StateY::class)); - } + expect($model->state->canTransitionTo(StateY::class))->toBeTrue(); +}); - /** @test */ - public function event_is_triggered_after_transition() - { - Event::fake(); +it('event is triggered after transition', function () { + Event::fake(); - $model = TestModel::create([ - 'state' => StateA::class, - ]); + $model = TestModel::create([ + 'state' => StateA::class, + ]); - $model->state->transitionTo(StateB::class); + $model->state->transitionTo(StateB::class); - Event::assertDispatched(StateChanged::class, function (StateChanged $event) use ($model) { - return $event->transition instanceof DefaultTransition - && $event->initialState instanceof StateA - && $event->finalState instanceof StateB - && $event->model->is($model); - }); - } + Event::assertDispatched(StateChanged::class, function (StateChanged $event) use ($model) { + return $event->transition instanceof DefaultTransition + && $event->initialState instanceof StateA + && $event->finalState instanceof StateB + && $event->model->is($model); + }); +}); - /** @test */ - public function can_transition_twice() - { - $model = TestModel::create([ - 'state' => StateA::class, - ]); +it('can transition twice', function () { + $model = TestModel::create([ + 'state' => StateA::class, + ]); - $model->state->transitionTo(StateB::class); - $model->state->transitionTo(StateC::class); + $model->state->transitionTo(StateB::class); + $model->state->transitionTo(StateC::class); - $model->refresh(); + $model->refresh(); - $this->assertInstanceOf(StateC::class, $model->state); - } -} + expect($model->state)->toBeInstanceOf(StateC::class); +}); diff --git a/tests/ValidationRuleTest.php b/tests/ValidationRuleTest.php index af1a4a31..6ee9a789 100644 --- a/tests/ValidationRuleTest.php +++ b/tests/ValidationRuleTest.php @@ -1,45 +1,36 @@ assertTrue(! Validator::make( - ['state' => StateA::getMorphClass()], - ['state' => $rule] - )->fails()); - - $this->assertFalse(! Validator::make( - ['state' => 'wrong'], - ['state' => $rule] - )->fails()); - } - - /** @test */ - public function nullable_validation() - { - $rule = (new ValidStateRule(ModelState::class))->required(); - - $this->assertTrue(Validator::make( - ['state' => null], - ['state' => $rule] - )->fails()); - - $rule = (new ValidStateRule(ModelState::class))->nullable(); - - $this->assertFalse(Validator::make( - ['state' => null], - ['state' => $rule] - )->fails()); - } -} +it('test validation', function () { + $rule = new ValidStateRule(ModelState::class); + + expect(! Validator::make( + ['state' => StateA::getMorphClass()], + ['state' => $rule] + )->fails())->toBeTrue(); + + expect(! Validator::make( + ['state' => 'wrong'], + ['state' => $rule] + )->fails())->toBeFalse(); +}); + +it('nullable validation', function () { + $rule = (new ValidStateRule(ModelState::class))->required(); + + expect(Validator::make( + ['state' => null], + ['state' => $rule] + )->fails())->toBeTrue(); + + $rule = (new ValidStateRule(ModelState::class))->nullable(); + + expect(Validator::make( + ['state' => null], + ['state' => $rule] + )->fails())->toBeFalse(); +}); From 3f6db04f7168dd0c19adbaf4ea6888b0ce571709 Mon Sep 17 00:00:00 2001 From: Freek Van der Herten Date: Mon, 20 Feb 2023 07:41:28 +0100 Subject: [PATCH 2/2] This belongs in global gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 7a3e63f6..543318a3 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,3 @@ vendor coverage .phpunit.result.cache .php-cs-fixer.cache -.idea