Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API-1126-Add-test-assertions #198

Merged
merged 6 commits into from
Feb 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions src/Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Schema;
use JetBrains\PhpStorm\Deprecated;
use Mockery\MockInterface;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\MockObject\MockObject;

Expand Down Expand Up @@ -94,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
{
/** @var MockInterface $taskSpy */
$taskSpy = \Mockery::mock($className, [app($repositoryClassName)])
->shouldIgnoreMissing(null, true)
->makePartial();

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

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

return $taskSpy;
}

/**
* 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
{
$repositoryMock = $this->mock($repositoryClassName);

if (is_null($criteriaArgs)) {
$repositoryMock->expects('pushCriteria')->once();
} else {
$repositoryMock->expects('pushCriteriaWith')->once()->with($criteriaClassName, $criteriaArgs);
}

return $repositoryMock;
}

/**
* 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
{
$repositoryMock = $this->mock($repositoryClassName);
$repositoryMock->expects('pushCriteria')->never();

return $repositoryMock;
}

/**
* Allow "addRequestCriteria" method 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 filtering).
*/
protected function allowAddRequestCriteriaInvocation(MockInterface $repositoryMock): void
{
$repositoryMock->allows('addRequestCriteria')->andReturnSelf();
}
}
Loading