Completed
Pull Request — master (#4416)
by Craig
20:52
created

VariableApiTest::testSetEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\ExtensionsModule\Tests\Api;
15
16
use PHPUnit\Framework\TestCase;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\TestCase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaHttpKernelInterface;
18
use Zikula\ExtensionsModule\Api\ApiInterface\VariableApiInterface;
19
use Zikula\ExtensionsModule\Api\VariableApi;
20
use Zikula\ExtensionsModule\Tests\Api\Fixtures\ExtensionVarStubRepository;
21
use Zikula\ExtensionsModule\Tests\Fixtures\BaseBundle\BaseBundle;
22
23
class VariableApiTest extends TestCase
24
{
25
    /**
26
     * @var VariableApiInterface
27
     */
28
    private $api;
29
30
    protected function setUp(): void
31
    {
32
        $kernel = $this
33
            ->getMockBuilder(ZikulaHttpKernelInterface::class)
34
            ->disableOriginalConstructor()
35
            ->getMock()
36
        ;
37
38
        $kernel
39
            ->expects($this->atMost(1))
40
            ->method('getBundles')
41
            ->willReturn(['BaseBundle' => new BaseBundle()])
42
        ;
43
44
        $repo = new ExtensionVarStubRepository();
45
        $this->api = new VariableApi('3.0.0', $repo, $kernel, ['protected.systemvars' => []]);
46
    }
47
48
    /**
49
     * @covers VariableApi::has
50
     */
51
    public function testHas(): void
52
    {
53
        $this->assertFalse($this->api->has('BaseBundle', 'test'));
54
        $this->assertTrue($this->api->has('FooExtension', 'bar'));
55
    }
56
57
    /**
58
     * @covers VariableApi::get
59
     */
60
    public function testGet(): void
61
    {
62
        $this->assertEquals($this->api->get('FooExtension', 'bar'), 'test');
63
        $this->assertEquals($this->api->get('BarExtension', 'bar'), 7);
64
        $this->assertFalse($this->api->get('FooExtension', 'nonExistentVariable'));
65
        $this->assertEquals($this->api->get('FooExtension', 'nonExistentVariable', 'defaultValue'), 'defaultValue');
66
    }
67
68
    /**
69
     * @covers VariableApi::getSystemVar
70
     */
71
    public function testGetSystemVar(): void
72
    {
73
        $this->assertEquals($this->api->get('ZConfig', 'systemvar'), 'abc');
74
        $this->assertFalse($this->api->get('ZConfig', 'nonExistentVariable'));
75
        $this->assertEquals($this->api->get('ZConfig', 'nonExistentVariable', 'defaultValue'), 'defaultValue');
76
    }
77
78
    /**
79
     * @covers VariableApi::getAll
80
     */
81
    public function testGetAll(): void
82
    {
83
        $this->assertCount(1, $this->api->getAll('FooExtension'));
84
        $this->assertArrayHasKey('bar', $this->api->getAll('FooExtension'));
85
    }
86
87
    /**
88
     * @covers VariableApi::set
89
     */
90
    public function testSetAndGet(): void
91
    {
92
        $this->assertTrue($this->api->set('TestSet', 'int', 8));
93
        $this->assertEquals($this->api->get('TestSet', 'int'), 8);
94
    }
95
96
    /**
97
     * @covers VariableApi::set
98
     */
99
    public function testSetEmpty(): void
100
    {
101
        $this->expectException(\InvalidArgumentException::class);
102
        $this->api->set('', '', 5);
103
    }
104
105
    /**
106
     * @covers VariableApi::setAll
107
     */
108
    public function testSetAllAndGetAll(): void
109
    {
110
        $variables = [
111
            'int' => 9,
112
            'name' => 'john',
113
            'string' => 'aabbccdd'
114
        ];
115
        $this->assertTrue($this->api->setAll('TestSet', $variables));
116
        $this->assertEquals($this->api->getAll('TestSet'), $variables);
117
    }
118
119
    /**
120
     * @covers VariableApi::del
121
     */
122
    public function testDel(): void
123
    {
124
        $this->assertCount(3, $this->api->getAll('BarExtension'));
125
        $this->assertTrue($this->api->has('BarExtension', 'name'));
126
        $this->assertTrue($this->api->del('BarExtension', 'name'));
127
        $this->assertFalse($this->api->has('BarExtension', 'name'));
128
        $this->assertCount(2, $this->api->getAll('BarExtension'));
129
    }
130
131
    /**
132
     * @covers VariableApi::delAll
133
     */
134
    public function testDelAll(): void
135
    {
136
        $this->assertCount(3, $this->api->getAll('BarExtension'));
137
        $this->assertTrue($this->api->delAll('BarExtension'));
138
        $this->assertEquals([], $this->api->getAll('BarExtension'));
139
    }
140
}
141