Skip to content

Commit

Permalink
Use a concrete class to test AbstractWrapper
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 57b4c06 commit 9a19b23
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 44 deletions.
16 changes: 16 additions & 0 deletions tests/library/Rules/WrapperStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?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\AbstractWrapper;

final class WrapperStub extends AbstractWrapper
{
}
59 changes: 15 additions & 44 deletions tests/unit/Rules/AbstractWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

namespace Respect\Validation\Rules;

use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Test\Rules\Stub;
use Respect\Validation\Test\Rules\WrapperStub;
use Respect\Validation\Test\TestCase;
use Respect\Validation\Validatable;

/**
* @test core
Expand All @@ -23,56 +25,29 @@ final class AbstractWrapperTest extends TestCase
*/
public function shouldUseWrappedToValidate(): void
{
$input = 'Whatever';
$sut = new WrapperStub(new Stub(true));

$validatable = $this->createMock(Validatable::class);
$validatable
->expects(self::once())
->method('validate')
->with($input)
->will(self::returnValue(true));

$wrapper = $this->getMockForAbstractClass(AbstractWrapper::class, [$validatable]);

self::assertTrue($wrapper->validate($input));
self::assertTrue($sut->validate('Whatever'));
}

/**
* @test
*/
public function shouldUseWrappedToAssert(): void
{
$input = 'Whatever';

$validatable = $this->createMock(Validatable::class);
$validatable
->expects(self::once())
->method('assert')
->with($input)
->will(self::returnValue(true));

$wrapper = $this->getMockForAbstractClass(AbstractWrapper::class, [$validatable]);

$wrapper->assert($input);
$sut = new WrapperStub(new Stub(false));
$this->expectException(ValidationException::class);
$sut->assert('Whatever');
}

/**
* @test
*/
public function shouldUseWrappedToCheck(): void
{
$input = 'Whatever';

$validatable = $this->createMock(Validatable::class);
$validatable
->expects(self::once())
->method('check')
->with($input)
->will(self::returnValue(true));

$wrapper = $this->getMockForAbstractClass(AbstractWrapper::class, [$validatable]);

$wrapper->check($input);
$sut = new WrapperStub(new Stub(false));
$this->expectException(ValidationException::class);
$sut->check('Whatever');
}

/**
Expand All @@ -82,15 +57,11 @@ public function shouldPassNameOnToWrapped(): void
{
$name = 'Whatever';

$validatable = $this->createMock(Validatable::class);
$validatable
->expects(self::once())
->method('setName')
->with($name)
->will(self::returnValue($validatable));
$rule = new Stub();

$wrapper = $this->getMockForAbstractClass(AbstractWrapper::class, [$validatable]);
$sut = new WrapperStub($rule);
$sut->setName($name);

self::assertSame($wrapper, $wrapper->setName($name));
self::assertSame($name, $rule->getName());
}
}

0 comments on commit 9a19b23

Please sign in to comment.