Completed
Push — master ( f88f6e...5d85cb )
by Pavel
03:14
created

TargetLinkResolverTest   B

Complexity

Total Complexity 2

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 17

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 17
dl 0
loc 65
rs 7.8571
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A testLinksGenerating() 0 61 2
1
<?php
2
3
namespace Bankiru\Seo\Tests\Unit\Resolver;
4
5
use Bankiru\Seo\Destination\CartesianDestinationGenerator;
6
use Bankiru\Seo\Destination\Compiler\SymfonyRouterCompiler;
7
use Bankiru\Seo\Destination\Normalizer\DelegatingNormalizer;
8
use Bankiru\Seo\Destination\Normalizer\ScalarNormalizer;
9
use Bankiru\Seo\Entity\LinkInterface;
10
use Bankiru\Seo\Entity\TargetDefinition;
11
use Bankiru\Seo\Integration\Local\CallbackFiller;
12
use Bankiru\Seo\Integration\Local\CollectionSource;
13
use Bankiru\Seo\Integration\Local\TargetLink;
14
use Bankiru\Seo\Resolver\SourceRegistry;
15
use Bankiru\Seo\Resolver\TargetLinkResolver;
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Symfony\Component\Routing\Generator\UrlGenerator;
18
use Symfony\Component\Routing\RequestContext;
19
use Symfony\Component\Routing\Route;
20
use Symfony\Component\Routing\RouteCollection;
21
22
class TargetLinkResolverTest extends \PHPUnit_Framework_TestCase
23
{
24
    public function testLinksGenerating()
25
    {
26
        $route = 'random_route_id';
27
28
        $target = new TargetDefinition($route);
29
30
        $registry = new SourceRegistry();
31
        $registry->add(
32
            'my_source',
33
            new CollectionSource(
34
                new ArrayCollection(
35
                    [
36
                        'quas',
37
                        'wex',
38
                        'exort',
39
                    ]
40
                )
41
            )
42
        );
43
44
        $normalizer = new DelegatingNormalizer([new ScalarNormalizer()]);
45
        $collection = new RouteCollection();
46
        $collection->add(
47
            $route,
48
            new Route('/my/{source}')
49
        );
50
        $generator = new UrlGenerator($collection, new RequestContext());
51
52
        $compiler = new SymfonyRouterCompiler($normalizer, null, $generator);
53
54
        $filler = new CallbackFiller(
55
            ['source'],
56
            function ($items) {
57
                return 'filled_' . array_shift($items);
58
            }
59
        );
60
61
        $link = new TargetLink(
62
            $target,
63
            'My link {{ source }} {{ filler}}',
64
            ['source' => 'my_source'],
65
            ['filler' => $filler]
66
        );
67
68
        $resolver = new TargetLinkResolver($registry, $compiler, new CartesianDestinationGenerator());
69
        /** @var LinkInterface[] $links */
70
        $links = $resolver->resolve($link);
71
72
        self::assertCount(3, $links);
73
        $res = [];
74
        foreach ($links as $compiledLink) {
75
            $res[$compiledLink->getHref()] = $compiledLink->getTitle();
76
        }
77
78
        self::assertArrayHasKey('/my/quas?filler=filled_quas', $res);
79
        self::assertArrayHasKey('/my/wex?filler=filled_wex', $res);
80
        self::assertArrayHasKey('/my/exort?filler=filled_exort', $res);
81
        self::assertEquals('My link quas filled_quas', $res['/my/quas?filler=filled_quas']);
82
        self::assertEquals('My link wex filled_wex', $res['/my/wex?filler=filled_wex']);
83
        self::assertEquals('My link exort filled_exort', $res['/my/exort?filler=filled_exort']);
84
    }
85
86
}
87