Skip to content

Commit

Permalink
Use a concrete class to test AbstractSearcher
Browse files Browse the repository at this point in the history
Although testing abstract classes is good, using mocks makes the code
too complicated.

Signed-off-by: Henrique Moody <[email protected]>
  • Loading branch information
henriquemoody committed Jan 28, 2024
1 parent 0c7f269 commit 57b4c06
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
35 changes: 35 additions & 0 deletions tests/library/Rules/SearcherStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]>
* SPDX-License-Identifier: MIT
*/

declare(strict_types=1);

namespace Respect\Validation\Test\Rules;

use Respect\Validation\Rules\AbstractSearcher;

use function call_user_func;

final class SearcherStub extends AbstractSearcher
{
/**
* @var callable
*/
private $dataSourceCallable;

public function __construct(callable $dataSourceCallable)
{
$this->dataSourceCallable = $dataSourceCallable;
}

/**
* @return array<mixed, array<mixed>>
*/
protected function getDataSource(mixed $input = null): array
{
return call_user_func($this->dataSourceCallable, $input);
}
}
25 changes: 5 additions & 20 deletions tests/unit/Rules/AbstractSearcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Respect\Validation\Rules;

use Respect\Validation\Test\DataProvider\UndefinedProvider;
use Respect\Validation\Test\Rules\SearcherStub;
use Respect\Validation\Test\TestCase;

/**
Expand All @@ -27,11 +28,7 @@ public function shouldValidateFromDataSource(): void
{
$input = 'BAZ';

$rule = $this->getMockForAbstractClass(AbstractSearcher::class);
$rule
->expects(self::once())
->method('getDataSource')
->willReturn(['FOO', $input, 'BAZ']);
$rule = new SearcherStub(static fn() => ['FOO', $input, 'BAZ']);

self::assertTrue($rule->validate($input));
}
Expand All @@ -43,11 +40,7 @@ public function shouldNotFindWhenNotIdentical(): void
{
$input = 2.0;

$rule = $this->getMockForAbstractClass(AbstractSearcher::class);
$rule
->expects(self::once())
->method('getDataSource')
->willReturn([1, (int) $input, 3]);
$rule = new SearcherStub(static fn() => [1, (int) $input, 3]);

self::assertFalse($rule->validate($input));
}
Expand All @@ -58,11 +51,7 @@ public function shouldNotFindWhenNotIdentical(): void
*/
public function shouldValidateWhenValueIsUndefinedAndDataSourceIsEmpty(mixed $input): void
{
$rule = $this->getMockForAbstractClass(AbstractSearcher::class);
$rule
->expects(self::once())
->method('getDataSource')
->willReturn([]);
$rule = new SearcherStub(static fn() => []);

self::assertTrue($rule->validate($input));
}
Expand All @@ -73,11 +62,7 @@ public function shouldValidateWhenValueIsUndefinedAndDataSourceIsEmpty(mixed $in
*/
public function shouldNotValidateWhenValueIsNotUndefinedAndDataSourceNotEmpty(mixed $input): void
{
$rule = $this->getMockForAbstractClass(AbstractSearcher::class);
$rule
->expects(self::once())
->method('getDataSource')
->willReturn([]);
$rule = new SearcherStub(static fn() => []);

self::assertFalse($rule->validate($input));
}
Expand Down

0 comments on commit 57b4c06

Please sign in to comment.