Completed
Pull Request — master (#168)
by
unknown
02:57
created

showSettingData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\SettingsBundle\Tests\Functional\Twig;
13
14
use ONGR\ElasticsearchBundle\Test\AbstractElasticsearchTestCase;
15
use ONGR\SettingsBundle\Exception\SettingNotFoundException;
16
use ONGR\SettingsBundle\Twig\GeneralSettingsWidgetExtension;
17
use ONGR\SettingsBundle\Tests\Fixtures\Security\LoginTestHelper;
18
19
/**
20
 * Class used to test GeneralSettingsExtension.
21
 */
22
class GeneralSettingsWidgetExtensionTest extends AbstractElasticsearchTestCase
23
{
24
    /**
25
     * @var Client
26
     */
27
    private $client;
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    protected function setUp()
33
    {
34
        parent::setUp();
35
        $this->client = new LoginTestHelper(static::createClient());
0 ignored issues
show
Documentation Bug introduced by
It seems like new \ONGR\SettingsBundle...static::createClient()) of type object<ONGR\SettingsBund...curity\LoginTestHelper> is incompatible with the declared type object<ONGR\SettingsBund...Functional\Twig\Client> of property $client.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
36
    }
37
38
    /**
39
     * Tests if extension is loaded correctly.
40
     */
41
    public function testGetExtension()
42
    {
43
        $container = $this->getContainer();
44
        /** @var GeneralSettingsWidgetExtension $extension */
45
        $extension = $container->get('ongr_settings.twig.personal_settings_extension');
46
        $this->assertInstanceOf(
47
            'ONGR\SettingsBundle\Twig\PersonalSettingWidgetExtension',
48
            $extension,
49
            'extension has wrong instance.'
50
        );
51
        $this->assertNotEmpty($extension->getFunctions(), 'extension does not have functions defined.');
52
    }
53
54
    /**
55
     * Data provider for testShowSetting.
56
     *
57
     * @return array[]
58
     */
59
    public function showSettingData()
60
    {
61
        // Case #0 not authenticated.
62
        $expectedOutput = '<div></div>';
63
        $out[] = [$expectedOutput, 'test_count_per_page', false];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$out was never initialized. Although not strictly required by PHP, it is generally a good practice to add $out = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
64
65
        // Case #1 default type (string).
66
        $expectedOutput = '<a href="http://localhost/settings/setting/count_per_page/edit/test" ';
67
        $out[] = [$expectedOutput, 'test_count_per_page', true];
68
69
        return $out;
70
    }
71
72
    /**
73
     * Test getPriceList().
74
     *
75
     * @param string $expectedOutput
76
     * @param string $settingId
77
     * @param bool   $isAuthenticated
78
     *
79
     * @dataProvider showSettingData
80
     */
81
    public function testShowSetting($expectedOutput, $settingId, $isAuthenticated)
0 ignored issues
show
Unused Code introduced by
The parameter $settingId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
    {
83
        if ($isAuthenticated) {
84
            $client = static::createClient(
85
                [],
86
                [
87
                    'PHP_AUTH_USER' => 'admin',
88
                    'PHP_AUTH_PW'   => 'admin',
89
                ]
90
            );
91
        } else {
92
            $client = static::createClient();
93
        }
94
95
        $settingTicUrl = 'settings/setting/change/'.base64_encode('ongr_settings_live_settings');
96
        // Tic the setting
97
        $client->request('GET', $settingTicUrl);
98
        $this->assertTrue($client->getResponse()->isOk());
99
100
        // Call controller with params to generate twig.
101
        $client->request('GET', '/test/twiggeneral');
102
103
        $this->assertContains($expectedOutput, $client->getResponse()->getContent());
104
    }
105
106
    /**
107
     * Test for getPersonalSetting() from general settings container.
108
     */
109
    public function testGetPersonalSetting()
110
    {
111
        $expectedValue = 'foo-bar';
112
113
        $settingContainer = $this->getMock('ONGR\SettingsBundle\Settings\General\SettingsContainerInterface');
114
        $settingContainer->expects($this->once())->method('get')->with('test')->willReturn($expectedValue);
115
116
        $extension = new GeneralSettingsWidgetExtension(null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<ONGR\SettingsBund...ersonalSettingsManager>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
117
        $extension->setSettingsContainer($settingContainer);
118
119
        $this->assertEquals($expectedValue, $extension->getPersonalSetting('test'));
120
    }
121
122
    /**
123
     * Test for getPersonalSetting()  from general settings container in case setting was not found.
124
     */
125
    public function testGetPersonalSettingException()
126
    {
127
        $settingContainer = $this->getMock('ONGR\SettingsBundle\Settings\General\SettingsContainerInterface');
128
        $settingContainer
129
            ->expects($this->once())
130
            ->method('get')
131
            ->with('test')
132
            ->willThrowException(new SettingNotFoundException());
133
134
        $extension = new GeneralSettingsWidgetExtension(null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<ONGR\SettingsBund...ersonalSettingsManager>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
135
        $extension->setSettingsContainer($settingContainer);
136
137
        $this->assertNull($extension->getPersonalSetting('test'));
138
    }
139
}
140