Passed
Pull Request — master (#9)
by Korotkov
01:49
created

ContainerTraitTest::testPut()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author    : Korotkov Danila <[email protected]>
7
 * @copyright Copyright (c) 2016, Korotkov Danila
8
 * @license   http://www.gnu.org/licenses/gpl.html GNU GPLv3.0
9
 *
10
 *  phpunit src/tests/ContainerTraitTest --coverage-html src/tests/coverage-html
11
 */
12
13
namespace Rudra\Tests;
14
15
use PHPUnit\Framework\TestCase as PHPUnit_Framework_TestCase;
16
use Rudra\ContainerInterface;
17
use Rudra\Container;
18
use Rudra\Tests\Stub\ClassWithContainerTrait;
19
use Rudra\Tests\Stub\ClassWithDefaultParameters;
20
use Rudra\Tests\Stub\ClassWithoutConstructor;
21
use Rudra\Tests\Stub\ClassWithoutParameters;
22
23
/**
24
 * Class ContainerTraitTest
25
 */
26
class ContainerTraitTest extends PHPUnit_Framework_TestCase
27
{
28
29
    /**
30
     * @var ClassWithContainerTrait
31
     */
32
    protected $stub;
33
34
    /**
35
     * @var string
36
     */
37
    protected $stubNamespace = 'Rudra\\Tests\\Stub\\';
38
39
    protected function setUp(): void
40
    {
41
        Container::app()->setBinding(ContainerInterface::class, Container::$app);
42
43
        $app = [
44
            'contracts' => [
45
                ContainerInterface::class => Container::$app
46
            ],
47
48
            'services' => [
49
                'validation' => [ClassWithoutConstructor::class],
50
                'redirect'   => [ClassWithoutParameters::class],
51
                'db'         => [ClassWithDefaultParameters::class, ['param' => '123']],
52
            ]
53
        ];
54
55
        Container::$app->setServices($app);
56
57
        $this->stub = new ClassWithContainerTrait(Container::$app);
58
    }
59
60
    public function testValidation(): void
61
    {
62
        $this->assertInstanceOf(ClassWithoutConstructor::class, $this->getStub()->validation());
63
    }
64
65
    public function testRedirect(): void
66
    {
67
        $this->assertInstanceOf(ClassWithoutParameters::class, $this->getStub()->redirect());
68
    }
69
70
    public function testDb(): void
71
    {
72
        $this->assertInstanceOf(ClassWithDefaultParameters::class, $this->getStub()->db());
73
    }
74
75
    public function testNew(): void
76
    {
77
        $newClassWithoutConstructor = $this->getStub()->new(ClassWithoutConstructor::class);
78
        $this->assertInstanceOf(ClassWithoutConstructor::class, $newClassWithoutConstructor);
79
    }
80
81
    public function testSetPagination(): void
82
    {
83
        $this->getMockBuilder('Rudra\Pagination')->getMock();
84
        $this->getStub()->setPagination(['id' => 1]);
85
        $this->assertInstanceOf('Rudra\Pagination', $this->getStub()->pagination());
86
    }
87
88
    public function testPost(): void
89
    {
90
        Container::$app->setPost(['key' => 'value']);
0 ignored issues
show
Bug introduced by
The method setPost() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

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

90
        Container::$app->/** @scrutinizer ignore-call */ 
91
                         setPost(['key' => 'value']);
Loading history...
91
        $this->assertEquals('value', $this->getStub()->post('key'));
92
    }
93
94
    public function testPut(): void
95
    {
96
        Container::$app->setPut(['key' => 'value']);
0 ignored issues
show
Bug introduced by
The method setPut() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

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

96
        Container::$app->/** @scrutinizer ignore-call */ 
97
                         setPut(['key' => 'value']);
Loading history...
97
        $this->assertTrue($this->getStub()->container()->hasPut('key'));
0 ignored issues
show
Bug introduced by
The method hasPut() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

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

97
        $this->assertTrue($this->getStub()->container()->/** @scrutinizer ignore-call */ hasPut('key'));
Loading history...
98
        $this->assertEquals('value', $this->getStub()->container()->getPut('key'));
0 ignored issues
show
Bug introduced by
The method getPut() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

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

98
        $this->assertEquals('value', $this->getStub()->container()->/** @scrutinizer ignore-call */ getPut('key'));
Loading history...
99
    }
100
101
    public function testPatch(): void
102
    {
103
        Container::$app->setPatch(['key' => 'value']);
0 ignored issues
show
Bug introduced by
The method setPatch() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

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

103
        Container::$app->/** @scrutinizer ignore-call */ 
104
                         setPatch(['key' => 'value']);
Loading history...
104
        $this->assertTrue($this->getStub()->container()->hasPatch('key'));
0 ignored issues
show
Bug introduced by
The method hasPatch() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

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

104
        $this->assertTrue($this->getStub()->container()->/** @scrutinizer ignore-call */ hasPatch('key'));
Loading history...
105
        $this->assertEquals('value', $this->getStub()->container()->getPatch('key'));
0 ignored issues
show
Bug introduced by
The method getPatch() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

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

105
        $this->assertEquals('value', $this->getStub()->container()->/** @scrutinizer ignore-call */ getPatch('key'));
Loading history...
106
    }
107
108
    public function testDelete(): void
109
    {
110
        Container::$app->setDelete(['key' => 'value']);
0 ignored issues
show
Bug introduced by
The method setDelete() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

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

110
        Container::$app->/** @scrutinizer ignore-call */ 
111
                         setDelete(['key' => 'value']);
Loading history...
111
        $this->assertTrue($this->getStub()->container()->hasDelete('key'));
0 ignored issues
show
Bug introduced by
The method hasDelete() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

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

111
        $this->assertTrue($this->getStub()->container()->/** @scrutinizer ignore-call */ hasDelete('key'));
Loading history...
112
        $this->assertEquals('value', $this->getStub()->container()->getDelete('key'));
0 ignored issues
show
Bug introduced by
The method getDelete() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

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

112
        $this->assertEquals('value', $this->getStub()->container()->/** @scrutinizer ignore-call */ getDelete('key'));
Loading history...
113
    }
114
115
    public function testSessionData(): void
116
    {
117
        $this->getStub()->setSession('key', 'value');
118
        $this->getStub()->setSession('subKey', 'value', 'subSet');
119
        $this->getStub()->setSession('increment', 'value', 'increment');
120
        $this->assertEquals('value', Container::$app->getSession('key'));
0 ignored issues
show
Bug introduced by
The method getSession() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

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

120
        $this->assertEquals('value', Container::$app->/** @scrutinizer ignore-call */ getSession('key'));
Loading history...
121
        $this->assertEquals('value', Container::$app->getSession('subKey', 'subSet'));
122
        $this->assertEquals('value', Container::$app->getSession('increment', '0'));
123
        $this->assertNull($this->getStub()->unsetSession('key'));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getStub()->unsetSession('key') targeting Rudra\Tests\Stub\ClassWi...erTrait::unsetSession() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
124
        $this->assertNull($this->getStub()->unsetSession('subKey', 'subSet'));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->getStub()->unsetS...ion('subKey', 'subSet') targeting Rudra\Tests\Stub\ClassWi...erTrait::unsetSession() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
125
        $this->assertFalse(Container::$app->hasSession('key'));
0 ignored issues
show
Bug introduced by
The method hasSession() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

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

125
        $this->assertFalse(Container::$app->/** @scrutinizer ignore-call */ hasSession('key'));
Loading history...
126
        $this->assertFalse(Container::$app->hasSession('subKey', 'subSet'));
127
    }
128
129
    public function testConfig(): void
130
    {
131
        Container::$app->setConfig(['key' => ['subKey' => 'value']]);
0 ignored issues
show
Bug introduced by
The method setConfig() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

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

131
        Container::$app->/** @scrutinizer ignore-call */ 
132
                         setConfig(['key' => ['subKey' => 'value']]);
Loading history...
132
133
        $this->assertInternalType('array', Container::$app->config('key'));
0 ignored issues
show
Bug introduced by
The method config() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

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

133
        $this->assertInternalType('array', Container::$app->/** @scrutinizer ignore-call */ config('key'));
Loading history...
134
        $this->assertEquals('value', Container::$app->config('key', 'subKey'));
135
    }
136
137
    /**
138
     * @runInSeparateProcess
139
     */
140
    public function testJsonResponse(): void
141
    {
142
        $data = ['key' => ['subKey' => 'value']];
143
144
        ob_start();
145
        Container::$app->jsonResponse($data);
0 ignored issues
show
Bug introduced by
The method jsonResponse() does not exist on Rudra\ContainerInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Rudra\ContainerInterface. ( Ignorable by Annotation )

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

145
        Container::$app->/** @scrutinizer ignore-call */ 
146
                         jsonResponse($data);
Loading history...
146
        $json = ob_get_clean();
147
148
        $this->assertEquals(json_encode($data), $json);
149
    }
150
151
    /**
152
     * @return ClassWithContainerTrait
153
     */
154
    public function getStub(): ClassWithContainerTrait
155
    {
156
        return $this->stub;
157
    }
158
}
159