Completed
Push — master ( 770316...74fc07 )
by Jeroen
09:08 queued 02:44
created

Tests/unit/Helper/AdminRouteHelperTest.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminBundle\Tests\Helper;
4
5
use Kunstmaan\AdminBundle\Helper\AdminRouteHelper;
6
use PHPUnit\Framework\TestCase;
7
use Symfony\Component\HttpFoundation\Request;
8
use Kunstmaan\NodeBundle\Router\SlugRouter;
9
use Symfony\Component\HttpFoundation\RequestStack;
10
11
class AdminRouteHelperTest extends TestCase
12
{
13
    protected static $ADMIN_KEY = 'admin';
14
15
    protected static $ALTERNATIVE_ADMIN_KEY = 'vip';
16
17
    protected static $NON_ADMIN_URL = '/en/some_path/%s/nodes';
18
19
    protected static $ADMIN_URL = '/en/%s/nodes';
20
21
    protected static $PREVIEW_ADMIN_URL = '/en/%s/preview/blog/page/1';
22
23
    /**
24
     * @covers \Kunstmaan\AdminBundle\Helper\AdminRouteHelper::isAdminRoute
25
     */
26 View Code Duplication
    public function testIsAdminRouteReturnsTrueWhenAdminUrl()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
    {
28
        $adminRouteHelper = $this->getAdminRouteHelper(self::$ADMIN_KEY);
29
        $result = $adminRouteHelper->isAdminRoute(sprintf(self::$ADMIN_URL, self::$ADMIN_KEY));
30
        $this->assertTrue($result);
31
32
        $adminRouteHelper = $this->getAdminRouteHelper(self::$ALTERNATIVE_ADMIN_KEY);
33
        $result = $adminRouteHelper->isAdminRoute(sprintf(self::$ADMIN_URL, self::$ALTERNATIVE_ADMIN_KEY));
34
        $this->assertTrue($result);
35
    }
36
37
    /**
38
     * @covers \Kunstmaan\AdminBundle\Helper\AdminRouteHelper::isAdminRoute
39
     */
40 View Code Duplication
    public function testIsAdminRouteReturnsFalseWhenFrontendUrl()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
    {
42
        $adminRouteHelper = $this->getAdminRouteHelper(self::$ADMIN_KEY);
43
        $result = $adminRouteHelper->isAdminRoute(sprintf(self::$NON_ADMIN_URL, self::$ADMIN_KEY));
44
        $this->assertFalse($result);
45
46
        $adminRouteHelper = $this->getAdminRouteHelper(self::$ALTERNATIVE_ADMIN_KEY);
47
        $result = $adminRouteHelper->isAdminRoute(sprintf(self::$NON_ADMIN_URL, self::$ALTERNATIVE_ADMIN_KEY));
48
        $this->assertFalse($result);
49
    }
50
51
    /**
52
     * @covers \Kunstmaan\AdminBundle\Helper\AdminRouteHelper::isAdminRoute
53
     */
54
    public function testIsAdminRouteReturnsFalseWhenPreviewUrl()
55
    {
56
        $requestStack = new RequestStack();
57
        $requestStack->push($this->getPreviewRequest());
58
59
        $adminRouteHelper = new AdminRouteHelper(self::$ADMIN_KEY, $requestStack);
60
        $result = $adminRouteHelper->isAdminRoute(sprintf(self::$PREVIEW_ADMIN_URL, self::$ADMIN_KEY));
61
        $this->assertFalse($result);
62
63
        $adminRouteHelper = new AdminRouteHelper(self::$ALTERNATIVE_ADMIN_KEY, $requestStack);
64
        $result = $adminRouteHelper->isAdminRoute(sprintf(self::$PREVIEW_ADMIN_URL, self::$ALTERNATIVE_ADMIN_KEY));
65
        $this->assertFalse($result);
66
    }
67
68
    private function getAdminRouteHelper($adminKey)
69
    {
70
        return new AdminRouteHelper($adminKey, $this->getRequestStack());
71
    }
72
73
    private function getRequestStack()
74
    {
75
        $requestStack = new RequestStack();
76
        $requestStack->push($this->getRequest());
77
78
        return $requestStack;
79
    }
80
81
    private function getRequest()
82
    {
83
        return Request::create('http://domain.tld/');
84
    }
85
86
    private function getPreviewRequest()
87
    {
88
        return Request::create('http://domain.tld/', 'GET', array('_route' => SlugRouter::$SLUG_PREVIEW));
89
    }
90
}
91