Skip to content

Commit

Permalink
Update TestAssertionHelperTrait.php
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad-Alavi committed Feb 5, 2024
1 parent 063bca1 commit 34192ca
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,76 @@ protected function getInaccessiblePropertyValue(object $object, string $property

return $property->getValue($object);
}

/**
* Create a spy for an Action, SubAction or a Task that uses a repository.
*
* @param string $className the Action, SubAction or a Task class name
* @param string $repositoryClassName the repository class name
*/
protected function createSpyWithRepository(string $className, string $repositoryClassName, bool $allowRun = true): MockInterface

Check warning on line 105 in src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php#L105

Added line #L105 was not covered by tests
{
/** @var MockInterface $taskSpy */
$taskSpy = \Mockery::mock($className, [app($repositoryClassName)])
->shouldIgnoreMissing(null, true)
->makePartial();

Check warning on line 110 in src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php#L108-L110

Added lines #L108 - L110 were not covered by tests

if ($allowRun) {
$taskSpy->allows('run')->andReturn();

Check warning on line 113 in src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php#L112-L113

Added lines #L112 - L113 were not covered by tests
}

$this->swap($className, $taskSpy);

Check warning on line 116 in src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php#L116

Added line #L116 was not covered by tests

return $taskSpy;

Check warning on line 118 in src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php#L118

Added line #L118 was not covered by tests
}

/**
* Mock a repository and assert that the given criteria is pushed to it.
*
* @param string $repositoryClassName the repository class name
* @param string $criteriaClassName the criteria class name
* @param array<string, mixed>|null $criteriaArgs the criteria constructor arguments
*
* @return MockInterface repository mock
*
* @example $this->
* assertCriteriaPushedToRepository(UserRepository::class, SearchUsersCriteria::class, ['parameterName' => 'value']);
*/
protected function assertCriteriaPushedToRepository(string $repositoryClassName, string $criteriaClassName, array|null $criteriaArgs = null): MockInterface

Check warning on line 133 in src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php#L133

Added line #L133 was not covered by tests
{
$repositoryMock = $this->mock($repositoryClassName);

Check warning on line 135 in src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php#L135

Added line #L135 was not covered by tests

if (is_null($criteriaArgs)) {
$repositoryMock->expects('pushCriteria')->once();

Check warning on line 138 in src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php#L137-L138

Added lines #L137 - L138 were not covered by tests
} else {
$repositoryMock->expects('pushCriteriaWith')->once()->with($criteriaClassName, $criteriaArgs);

Check warning on line 140 in src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php#L140

Added line #L140 was not covered by tests
}

return $repositoryMock;

Check warning on line 143 in src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php#L143

Added line #L143 was not covered by tests
}

/**
* Assert that no criteria are pushed to the repository.
*
* @param string $repositoryClassName the repository class name
*
* @return MockInterface repository mock
*/
protected function assertNoCriteriaPushedToRepository(string $repositoryClassName): MockInterface

Check warning on line 153 in src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php#L153

Added line #L153 was not covered by tests
{
$repositoryMock = $this->mock($repositoryClassName);
$repositoryMock->expects('pushCriteria')->never();

Check warning on line 156 in src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php#L155-L156

Added lines #L155 - L156 were not covered by tests

return $repositoryMock;

Check warning on line 158 in src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php#L158

Added line #L158 was not covered by tests
}

/**
* Allow "addRequestCriteria" invocation on the repository mock.
* This is particularly useful when you want to test a repository that uses the RequestCriteria
* (e.g., for search and filter).
*/
protected function allowAddRequestCriteriaInvocation(MockInterface $repositoryMock): void

Check warning on line 166 in src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php#L166

Added line #L166 was not covered by tests
{
$repositoryMock->allows('addRequestCriteria')->andReturnSelf();

Check warning on line 168 in src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php

View check run for this annotation

Codecov / codecov/patch

src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php#L168

Added line #L168 was not covered by tests
}
}

0 comments on commit 34192ca

Please sign in to comment.