Completed
Push — master ( 51af85...b489fc )
by Pavel
06:35
created

ProcessorTest::testProcessing()   B

Complexity

Conditions 4
Paths 10

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 24
nc 10
nop 4
1
<?php
2
3
namespace Bankiru\Seo\Tests\Unit;
4
5
use Bankiru\Seo\Destination;
6
use Bankiru\Seo\DestinationInterface;
7
use Bankiru\Seo\DestinationMatcher;
8
use Bankiru\Seo\Entity\TargetDefinition;
9
use Bankiru\Seo\Exception\MatchingException;
10
use Bankiru\Seo\Integration\Local\ExactCondition;
11
use Bankiru\Seo\Page\SeoPageBuilder;
12
use Bankiru\Seo\PageRepositoryInterface;
13
use Bankiru\Seo\Target\MatchScoreTargetSorter;
14
use Bankiru\Seo\TargetDefinitionInterface;
15
use Bankiru\Seo\TargetRepositoryInterface;
16
use Prophecy\Argument;
17
18
class ProcessorTest extends \PHPUnit_Framework_TestCase
19
{
20
    public function getItems()
21
    {
22
        $route = 'any_random_route_id';
23
        $c1    = new ExactCondition('1');
24
        $c2    = new ExactCondition(2);
25
26
        $d1      = new TargetDefinition($route);
27
        $d2      = new TargetDefinition($route);
28
        $d3      = new TargetDefinition($route);
29
        $d4      = new TargetDefinition($route);
30
        $targets = [$d1, $d2, $d3, $d4];
31
32
        foreach ($targets as $target) {
33
            $c1->attach($target, 'arg1');
34
            $c2->attach($target, 'arg2');
35
        }
36
37
        return [
38
            'exact match'   => [$route, $targets, ['arg1' => '1', 'arg2' => 2], true],
39
            'arg1 no match' => [$route, $targets, ['arg1' => 2, 'arg2' => 2], false],
40
            'arg2 no match' => [$route, $targets, ['arg1' => '1', 'arg2' => 1], false],
41
            'no match'      => [$route, $targets, ['arg1' => '5', 'arg2' => 3], false],
42
        ];
43
    }
44
45
    /**
46
     * @dataProvider getItems
47
     *
48
     * @param string $route
49
     * @param array  $targets
50
     * @param array  $items
51
     *
52
     * @internal     param $route
53
     */
54
    public function testProcessing($route, array $targets, array $items, $match)
55
    {
56
57
        $page = (new SeoPageBuilder())
58
            ->setTitle('Test Page')
59
            ->getSeoPage();
60
61
        $targetRepository = $this->prophesize(TargetRepositoryInterface::class);
62
        $targetRepository->findByRoute(Argument::exact($route))->willReturn($targets);
63
        $targetRepository = $targetRepository->reveal();
64
65
        $pageRepository = $this->prophesize(PageRepositoryInterface::class);
66
        $pageRepository
67
            ->getByTargetDestination(
68
                Argument::type(TargetDefinitionInterface::class),
69
                Argument::type(DestinationInterface::class)
70
            )
71
            ->willReturn($page);
72
        $pageRepository = $pageRepository->reveal();
73
74
        $processor = new DestinationMatcher(new MatchScoreTargetSorter($targetRepository), $pageRepository);
75
76
        $destination = new Destination($route, $items);
77
78
        try {
79
            $seoPage = $processor->match($destination);
80
            if (!$match) {
81
                self::fail('Should not match');
82
            }
83
            self::assertEquals($page, $seoPage);
84
        } catch (MatchingException $exception) {
85
            if ($match) {
86
                throw $exception;
87
            }
88
        }
89
    }
90
}
91