OptionServiceTest::testSet()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
/*
4
 * (c) Jim Martens <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace TwoMartens\Bundle\CoreBundle\Tests\Option;
11
12
use Symfony\Component\Finder\Finder;
13
use Symfony\Component\Finder\SplFileInfo;
14
use Symfony\Component\Yaml\Yaml;
15
use TwoMartens\Bundle\CoreBundle\Model\OptionCategory;
16
use TwoMartens\Bundle\CoreBundle\Option\OptionService;
17
use TwoMartens\Bundle\CoreBundle\Option\OptionServiceInterface;
18
use TwoMartens\Bundle\CoreBundle\Util\ConfigUtil;
19
20
/**
21
 * Tests the OptionService.
22
 *
23
 * @author    Jim Martens <[email protected]>
24
 * @copyright 2013-2015 Jim Martens
25
 */
26
class OptionServiceTest extends \PHPUnit_Framework_TestCase
27
{
28
    /**
29
     * the service
30
     * @var OptionServiceInterface
31
     */
32
    private $optionService;
33
34
    /**
35
     * the dummy parser
36
     * @var \PHPUnit_Framework_MockObject_MockObject
37
     */
38
    private $dummyParser;
39
40
    /**
41
     * the dummy dumper
42
     * @var \PHPUnit_Framework_MockObject_MockObject
43
     */
44
    private $dummyDumper;
45
46
    /**
47
     * the dummy filesystem
48
     * @var \PHPUnit_Framework_MockObject_MockObject
49
     */
50
    private $dummyFilesystem;
51
52
    /**
53
     * the yaml parsed data
54
     * @var array
55
     */
56
    private $yamlData;
57
58
    /**
59
     * the option data
60
     * @var OptionCategory
61
     */
62
    private $optionData;
63
64
    /**
65
     * Sets the test environment up.
66
     */
67
    public function setUp()
68
    {
69
        $finder = new Finder();
70
        $finder->files()->in(__DIR__.'/config/');
71
        $finder->name('config.yml');
72
        $this->dummyParser = $this->getMock('Symfony\Component\Yaml\Parser');
73
        $this->dummyDumper = $this->getMock('Symfony\Component\Yaml\Dumper');
74
        $this->dummyFilesystem = $this->getMock('Symfony\Component\Filesystem\Filesystem');
75
76
        foreach ($finder as $file) {
77
            /** @var SplFileInfo $file */
78
            $this->yamlData = Yaml::parse($file->getContents());
0 ignored issues
show
Documentation Bug introduced by
It seems like \Symfony\Component\Yaml\...e($file->getContents()) can also be of type string or object<stdClass>. However, the property $yamlData is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
79
            $this->optionData = ConfigUtil::convertToOptions($this->yamlData);
80
            $this->dummyParser->expects($this->once())->method('parse')
81
                ->willReturn($this->yamlData);
82
            $this->dummyDumper->expects($this->any())->method('dump')
83
                ->willReturn($file->getContents());
84
            $this->dummyFilesystem->expects($this->any())->method('dumpFile')
85
                ->with($file->getRealPath(), $file->getContents());
86
        }
87
88
        $this->optionService = new OptionService(
89
            $finder,
90
            $this->dummyParser,
91
            $this->dummyDumper,
92
            $this->dummyFilesystem
93
        );
94
    }
95
96
    /**
97
     * Tests the state after the construct method has finished.
98
     */
99
    public function testConstruct()
100
    {
101
        $options = $this->optionService->getOptions();
102
        $categories = $options->getCategories();
103
        /** @var OptionCategory $firstCategory */
104
        $firstCategory = $categories[0];
105
        $this->assertEquals('twomartens.core', $firstCategory->getName());
106
        $options = $firstCategory->getOptions();
107
108
        $this->assertEquals('optionOne', $options[0]->getName());
109
        $this->assertEquals('boolean', $options[0]->getType());
110
        $this->assertEquals('optionTwo', $options[1]->getName());
111
        $this->assertEquals('string', $options[1]->getType());
112
        $this->assertEquals('optionThree', $options[2]->getName());
113
        $this->assertEquals('array', $options[2]->getType());
114
    }
115
116
    /**
117
     * Tests the get method.
118
     */
119
    public function testGet()
120
    {
121
        $optionValue = $this->optionService->get('twomartens.core', 'optionOne');
122
        $this->assertEquals('optionOne', $optionValue->getName());
123
        $this->assertEquals('boolean', $optionValue->getType());
124
        $this->assertTrue($optionValue->getValue());
125
126
        $optionValue = $this->optionService->get('twomartens.core', 'optionTwo');
127
        $this->assertEquals('optionTwo', $optionValue->getName());
128
        $this->assertEquals('string', $optionValue->getType());
129
        $this->assertEquals('Sascha', $optionValue->getValue());
130
131
        $optionValue = $this->optionService->get('twomartens.core', 'optionThree');
132
        $this->assertEquals('optionThree', $optionValue->getName());
133
        $this->assertEquals('array', $optionValue->getType());
134
        $this->assertEquals([1, 2, 3], $optionValue->getValue());
135
    }
136
137
    /**
138
     * Tests the set method.
139
     */
140
    public function testSet()
141
    {
142
        $optionValue = $this->optionService->get('twomartens.core', 'optionOne');
143
        $optionValue->setValue(false);
144
        $this->optionService->set('twomartens.core', 'optionOne', $optionValue);
145
146
        $firstOption = $this->optionService->get('twomartens.core', 'optionOne');
147
        $this->assertFalse($firstOption->getValue());
148
        $this->assertEquals('boolean', $firstOption->getType());
149
    }
150
151
    /**
152
     * Tests the getOptions method.
153
     */
154
    public function testSetOptions()
155
    {
156
        $superCategory = $this->optionService->getOptions();
157
        $categories = $superCategory->getCategories();
158
159
        $firstCategory = $categories[0];
160
        $options = $firstCategory->getOptions();
161
        $firstOption = $options[0];
162
        $firstOption->setValue(false);
163
164
        $this->optionService->setOptions($superCategory);
165
        $testOption = $this->optionService->get('twomartens.core', 'optionOne');
166
        $this->assertFalse($testOption->getValue());
167
    }
168
}
169