Skip to content

Commit 9a19b23

Browse files
committed
Use a concrete class to test AbstractWrapper
Although testing abstract classes is good, using mocks makes the code too complicated. Signed-off-by: Henrique Moody <[email protected]>
1 parent 57b4c06 commit 9a19b23

File tree

2 files changed

+31
-44
lines changed

2 files changed

+31
-44
lines changed

tests/library/Rules/WrapperStub.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* Copyright (c) Alexandre Gomes Gaigalas <[email protected]>
5+
* SPDX-License-Identifier: MIT
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Respect\Validation\Test\Rules;
11+
12+
use Respect\Validation\Rules\AbstractWrapper;
13+
14+
final class WrapperStub extends AbstractWrapper
15+
{
16+
}

tests/unit/Rules/AbstractWrapperTest.php

Lines changed: 15 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99

1010
namespace Respect\Validation\Rules;
1111

12+
use Respect\Validation\Exceptions\ValidationException;
13+
use Respect\Validation\Test\Rules\Stub;
14+
use Respect\Validation\Test\Rules\WrapperStub;
1215
use Respect\Validation\Test\TestCase;
13-
use Respect\Validation\Validatable;
1416

1517
/**
1618
* @test core
@@ -23,56 +25,29 @@ final class AbstractWrapperTest extends TestCase
2325
*/
2426
public function shouldUseWrappedToValidate(): void
2527
{
26-
$input = 'Whatever';
28+
$sut = new WrapperStub(new Stub(true));
2729

28-
$validatable = $this->createMock(Validatable::class);
29-
$validatable
30-
->expects(self::once())
31-
->method('validate')
32-
->with($input)
33-
->will(self::returnValue(true));
34-
35-
$wrapper = $this->getMockForAbstractClass(AbstractWrapper::class, [$validatable]);
36-
37-
self::assertTrue($wrapper->validate($input));
30+
self::assertTrue($sut->validate('Whatever'));
3831
}
3932

4033
/**
4134
* @test
4235
*/
4336
public function shouldUseWrappedToAssert(): void
4437
{
45-
$input = 'Whatever';
46-
47-
$validatable = $this->createMock(Validatable::class);
48-
$validatable
49-
->expects(self::once())
50-
->method('assert')
51-
->with($input)
52-
->will(self::returnValue(true));
53-
54-
$wrapper = $this->getMockForAbstractClass(AbstractWrapper::class, [$validatable]);
55-
56-
$wrapper->assert($input);
38+
$sut = new WrapperStub(new Stub(false));
39+
$this->expectException(ValidationException::class);
40+
$sut->assert('Whatever');
5741
}
5842

5943
/**
6044
* @test
6145
*/
6246
public function shouldUseWrappedToCheck(): void
6347
{
64-
$input = 'Whatever';
65-
66-
$validatable = $this->createMock(Validatable::class);
67-
$validatable
68-
->expects(self::once())
69-
->method('check')
70-
->with($input)
71-
->will(self::returnValue(true));
72-
73-
$wrapper = $this->getMockForAbstractClass(AbstractWrapper::class, [$validatable]);
74-
75-
$wrapper->check($input);
48+
$sut = new WrapperStub(new Stub(false));
49+
$this->expectException(ValidationException::class);
50+
$sut->check('Whatever');
7651
}
7752

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

85-
$validatable = $this->createMock(Validatable::class);
86-
$validatable
87-
->expects(self::once())
88-
->method('setName')
89-
->with($name)
90-
->will(self::returnValue($validatable));
60+
$rule = new Stub();
9161

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

94-
self::assertSame($wrapper, $wrapper->setName($name));
65+
self::assertSame($name, $rule->getName());
9566
}
9667
}

0 commit comments

Comments
 (0)