Passed
Push — release/v2 ( 119cbe...817cd6 )
by Benoit
02:50
created

ChainFactoryTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 10
c 0
b 0
f 0
wmc 4
1
<?php
2
3
namespace JBen87\ParsleyBundle\Tests\Constraint\Factory;
4
5
use JBen87\ParsleyBundle\Constraint\Constraints as ParsleyAssert;
6
use JBen87\ParsleyBundle\Constraint\Factory\ChainFactory;
7
use JBen87\ParsleyBundle\Constraint\Factory\FactoryInterface;
8
use JBen87\ParsleyBundle\Constraint\Factory\RequiredFactory;
9
use JBen87\ParsleyBundle\Exception\ConstraintException;
10
use PHPUnit\Framework\TestCase;
11
use Symfony\Component\Validator\Constraints as Assert;
12
13
class ChainFactoryTest extends TestCase
14
{
15
    public function testCreate(): void
16
    {
17
        $expected = new ParsleyAssert\Required();
18
19
        $requiredFactory = $this->createMock(RequiredFactory::class);
20
        $requiredFactory
21
            ->expects($this->once())
22
            ->method('supports')
23
            ->willReturn(true)
24
        ;
25
        $requiredFactory
26
            ->expects($this->once())
27
            ->method('create')
28
            ->willReturn($expected)
29
        ;
30
31
        $factory = $this->createFactory([
32
            $requiredFactory,
33
        ]);
34
35
        $constraint = $factory->create(new Assert\NotBlank());
36
        $this->assertSame($expected, $constraint);
37
    }
38
39
    public function testCreateUnsupported(): void
40
    {
41
        $this->expectException(ConstraintException::class);
42
        $this->createFactory([])->create(new Assert\NotBlank());
43
    }
44
45
    public function testSupports(): void
46
    {
47
        $factory = $this->createFactory([]);
48
49
        $this->assertTrue($factory->supports(new Assert\NotBlank()));
50
        $this->assertTrue($factory->supports(new Assert\Valid()));
51
    }
52
53
    /**
54
     * @param FactoryInterface[] $factories
55
     *
56
     * @return ChainFactory
57
     */
58
    private function createFactory(array $factories): ChainFactory
59
    {
60
        return new ChainFactory($factories);
61
    }
62
}
63