Issues (14)

tests/src/Sections/SectionTest.php (3 issues)

Labels
Severity
1
<?php
2
3
namespace Nip\Mvc\Tests\Sections;
4
5
use Mockery\Mock;
6
use Nip\Container\Container;
7
use Nip\Mvc\Sections\Section;
8
use Nip\Mvc\Sections\SectionsManager;
9
use Nip\Mvc\Tests\AbstractTest;
10
11
/**
12
 * Class SectionTest
13
 * @package Nip\Mvc\Tests\Sections
14
 */
15
class SectionTest extends AbstractTest
16
{
17
    public function test_visibleIn()
18
    {
19
        $section = new Section();
20
        $section->writeData(['visibleIn' => ['admin']]);
21
22
        self::assertTrue($section->visibleIn('admin'));
23
        self::assertFalse($section->visibleIn('frontend'));
24
    }
25
26
    public function test_visibleIn_magicCall()
27
    {
28
        $section = new Section();
29
        $section->writeData(['visibleIn' => ['admin','admin_menu']]);
30
31
        self::assertTrue($section->isAdmin());
0 ignored issues
show
The method isAdmin() does not exist on Nip\Mvc\Sections\Section. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
        self::assertTrue($section->/** @scrutinizer ignore-call */ isAdmin());
Loading history...
32
        self::assertTrue($section->isAdminMenu());
0 ignored issues
show
The method isAdminMenu() does not exist on Nip\Mvc\Sections\Section. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
        self::assertTrue($section->/** @scrutinizer ignore-call */ isAdminMenu());
Loading history...
33
        self::assertFalse($section->isFrontend());
0 ignored issues
show
The method isFrontend() does not exist on Nip\Mvc\Sections\Section. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
        self::assertFalse($section->/** @scrutinizer ignore-call */ isFrontend());
Loading history...
34
    }
35
36
    public function test_printIcon()
37
    {
38
        $section = new Section();
39
        $section->writeData(['icon' => '<path class="fil0" d="M512 402l0 110" >']);
40
41
        self::assertSame(
42
            '<svg viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg"  focusable="false" aria-hidden="true" role="presentation"><path class="fil0" d="M512 402l0 110" ></svg>',
43
            $section->printIcon()
44
        );
45
    }
46
47
    /**
48
     * @dataProvider data_getURL
49
     */
50
    public function test_getURL($expected, $input)
51
    {
52
        $this->initRequest();
53
        $section = $this->newSectionMock();
54
        $section->writeData(['subdomain' => 'test']);
55
56
        self::assertSame($expected, $section->getURL($input));
57
    }
58
59
    /**
60
     * @return array
61
     */
62
    public function data_getURL()
63
    {
64
        return [
65
            ['http://test.mydomain.com/subfolder', ''],
66
            ['http://test.mydomain.com/subfolder/MyController/Action', '/MyController/Action'],
67
            ['http://test.mydomain.com/MyController/Action', 'http://mydomain.com/MyController/Action'],
68
        ];
69
    }
70
71
    public function test_getBaseUrl()
72
    {
73
        $this->initRequest();
74
        $section = $this->newSectionMock();
75
        $section->writeData(['subdomain' => 'test']);
76
77
        self::assertSame('http://test.mydomain.com/subfolder', $section->getBaseUrl());
78
    }
79
80
    /**
81
     * @return Mock|Section
82
     */
83
    protected function newSectionMock()
84
    {
85
        $manager = new SectionsManager();
86
87
        /** @var Section|Mock $section */
88
        $section = \Mockery::mock(Section::class)->shouldAllowMockingProtectedMethods()->makePartial();
89
        $section->shouldReceive('getManager')->andReturn($manager);
90
91
        return $section;
92
    }
93
94
    protected function initRequest()
95
    {
96
        $server = [
97
            'HTTP_HOST' => 'mydomain.com',
98
            'SERVER_NAME' => 'mydomain.com',
99
            'SCRIPT_FILENAME' => '/home/app/subfolder/index.php',
100
            'SCRIPT_NAME' => '/subfolder/index.php',
101
            'REQUEST_URI' => '/subfolder/MyController/Action',
102
        ];
103
        $request = new \Nip\Http\Request([], [], [], [], [], $server);
104
105
        Container::getInstance()->set('request', $request);
106
    }
107
}
108