Skip to content

Commit

Permalink
feat: add test helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad-Alavi committed Jun 4, 2023
1 parent bb52995 commit 6425583
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 2 deletions.
32 changes: 32 additions & 0 deletions Abstracts/Requests/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,38 @@ public static function injectData(array $parameters = [], User $user = null, arr
return $request;
}

/**
* Add properties to the request that are not part of the request data
* but are needed for the request to be processed.
* For example, in the unit tests, we can add the url parameters to the request which is not part of the request data.
* It is best used with the `injectData` method.
* @param array $properties
* @return $this
*/
public function withUrlParameters(array $properties): self
{
foreach ($properties as $key => $value) {
$this->{$key} = $value;
}

return $this;
}

public function getAccessArray(): array
{
return $this->access ?? [];
}

public function getDecodeArray(): array
{
return $this->decode ?? [];
}

public function getUrlParametersArray(): array
{
return $this->urlParameters ?? [];
}

/**
* check if a user has permission to perform an action.
* User can set multiple permissions (separated with "|") and if the user has
Expand Down
80 changes: 80 additions & 0 deletions Traits/TestTraits/PhpUnit/TestAssertionHelperTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Apiato\Core\Traits\TestsTraits\PhpUnit;

use Apiato\Core\Abstracts\Models\Model;
use Apiato\Core\Abstracts\Requests\Request;
use Apiato\Core\Exceptions\CoreInternalErrorException;
use Illuminate\Support\Collection;
use Mockery\MockInterface;

trait TestAssertionHelperTrait
{
/**
* Assert that the model casts field is empty.
* By default, the model casts will have the 'id' field as 'int'.
* This method will exclude this field from the assertion.
* If you want to add more fields, you can pass them as an array.
*/
public function assertModelCastsIsEmpty(Model $model, array ...$extraDefaultField): void
{
$defaultCasts = [
'id' => 'int',
];

foreach ($extraDefaultField as $field) {
$defaultCasts = array_merge($defaultCasts, $field);
}

$this->assertEmpty(array_diff($model->getCasts(), $defaultCasts));
}

/**
* Assert if the request authorize method calls the correct policy and the correct policy method with right parameters in the right order.
* @param Request $request
* @param string $policy
* @param string $policyMethodName
* @param array $policyMethodParameters Array elements can be a primitive type (e.g. NULL, boolean, integer, etc...) or an object. If it's an object, it will check if the parameter is an instance of that object.
* @throws \Throwable
*/
public function assertRequestAuthorizeCallsPolicyCorrectly(Request $request, string $policy, string $policyMethodName, array $policyMethodParameters): void
{
throw_if(!method_exists($this, 'authorize'),CoreInternalErrorException::class, 'The request does not have an authorize method.');

$policyMock = $this->mock($policy, function (MockInterface $mock) use ($policyMethodName, $policyMethodParameters) {
$mock->shouldReceive($policyMethodName)
->once()
->withArgs(function (...$parameters) use ($policyMethodParameters) {
if (count($parameters) !== count($policyMethodParameters)) {
return false;
}
for ($i = 0; $i < count($parameters); $i++) {
$parameterType = gettype($parameters[$i]);
if ($parameterType === 'object') {
if (!($parameters[$i]->is($policyMethodParameters[$i]))) {
return false;
}
} else {
if (!($parameterType == $policyMethodParameters[$i])) {
return false;
}
}
}
return true;
})->andReturn(false);
});

$request->authorize($policyMock);
}

/**
* Check if the given id is in the given model collection by comparing hashed ids.
* @param $id
* @param Collection $collection
* @return bool
*/
public function inIds($id, Collection $collection): bool
{
return in_array($id, $collection->map(fn ($item) => $item->getHashedKey())->toArray());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Hash;

trait TestsAuthHelperTrait
trait TestAuthHelperTrait
{
/**
* Logged in user object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use stdClass;
use Vinkla\Hashids\Facades\Hashids;

trait TestsRequestHelperTrait
trait TestRequestHelperTrait
{
/**
* property to be set on the user test class
Expand Down

0 comments on commit 6425583

Please sign in to comment.