summaryrefslogtreecommitdiff
path: root/tests/Unit/Renderer/Twig/Extensions/UrlTest.php
blob: c7e40bea55b162f3d03d3f65e856190bb190c985 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php

namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions;

use Engelsystem\Http\UrlGenerator;
use Engelsystem\Renderer\Twig\Extensions\Url;
use PHPUnit\Framework\MockObject\MockObject;

class UrlTest extends ExtensionTest
{
    /**
     * @covers \Engelsystem\Renderer\Twig\Extensions\Url::__construct
     * @covers \Engelsystem\Renderer\Twig\Extensions\Url::getFunctions
     */
    public function testGetGlobals()
    {
        /** @var UrlGenerator|MockObject $urlGenerator */
        $urlGenerator = $this->createMock(UrlGenerator::class);

        $extension = new Url($urlGenerator);
        $functions = $extension->getFunctions();

        $this->assertExtensionExists('url', [$extension, 'getUrl'], $functions);
    }

    /**
     * @return string[][]
     */
    public function getUrls()
    {
        return [
            ['/', '/', 'http://foo.bar/'],
            ['/foo', '/foo', 'http://foo.bar/foo'],
            ['foo_bar', 'foo-bar', 'http://foo.bar/foo-bar'],
            ['dolor', 'dolor', 'http://foo.bar/dolor?lorem_ipsum=dolor', ['lorem_ipsum' => 'dolor']],
        ];
    }

    /**
     * @dataProvider getUrls
     *
     * @param string $url
     * @param string $return
     * @param string $urlTo
     * @param array  $parameters
     *
     * @covers       \Engelsystem\Renderer\Twig\Extensions\Url::getUrl
     */
    public function testGetUrl($url, $urlTo, $return, $parameters = [])
    {
        /** @var UrlGenerator|MockObject $urlGenerator */
        $urlGenerator = $this->createMock(UrlGenerator::class);

        $urlGenerator->expects($this->once())
            ->method('to')
            ->with($urlTo, $parameters)
            ->willReturn($return);

        $extension = new Url($urlGenerator);
        $generatedUrl = $extension->getUrl($url, $parameters);

        $this->assertEquals($return, $generatedUrl);
    }
}