Completed
Push — master ( 8d344e...fc7ac0 )
by Thomas
01:55
created

DataSourceTest::testDataValue()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 26
nc 1
nop 0
dl 0
loc 41
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright © 2018 Thomas Klein, All rights reserved.
4
 * See LICENSE bundled with this library for license details.
5
 */
6
7
namespace LogicTree\Test;
8
9
use PHPUnit\Framework\TestCase;
10
use LogicTree\DataSource;
11
12
/**
13
 * Test the \LogicTree\DataSource object.
14
 */
15
class DataSourceTest extends TestCase
16
{
17
    /**
18
     * Test the data
19
     *
20
     * @return void
21
     */
22
    public function testData()
23
    {
24
        $dataSource = new DataSource();
25
26
        $data = ['key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'];
27
        $addData = ['key2' => 'newValue2', 'key4' => 'value4'];
28
        $mergedData = array_merge($data, $addData);
29
        $unsetData = ['key1', 'key4'];
30
        $finalData = ['key2' => 'newValue2', 'key3' => 'value3'];
31
        $dataSourceMirror = new DataSource($data);
32
33
        // Test getData
34
        $this->assertInternalType('array', $dataSource->getData());
35
        $this->assertSame([], $dataSource->getData());
36
37
        // Test setData
38
        $this->assertInstanceOf(DataSource::class, $dataSource->setData($data));
39
        $this->assertSame($data, $dataSource->getData());
40
41
        // Test Construct
42
        $this->assertEquals($dataSourceMirror, $dataSource);
43
        $this->assertSame($dataSourceMirror->getData(), $dataSource->getData());
44
45
        // Test addData
46
        $this->assertInstanceOf(DataSource::class, $dataSource->addData($addData));
47
        $this->assertSame($mergedData, $dataSource->getData());
48
49
        // Test unsetData
50
        $this->assertInstanceOf(DataSource::class, $dataSource->unsetData($unsetData));
51
        $this->assertSame($finalData, $dataSource->getData());
52
    }
53
54
    /**
55
     * Test the values
56
     *
57
     * @return void
58
     */
59
    public function testValue()
60
    {
61
        $dataSource = new DataSource();
62
63
        $key = 'key1';
64
        $value = 'value1';
65
66
        // Test getValue
67
        $this->assertNull($dataSource->getValue($key));
68
69
        // Test setValue
70
        $this->assertInstanceOf(DataSource::class, $dataSource->setValue($key, $value));
71
        $this->assertSame($value, $dataSource->getValue($key));
72
73
        // Test unsetValue
74
        $this->assertInstanceOf(DataSource::class, $dataSource->unsetValue($key));
75
        $this->assertNull($dataSource->getValue($key));
76
    }
77
78
    /**
79
     * Test the interoperability between data and values
80
     *
81
     * @return void
82
     */
83
    public function testDataValue()
84
    {
85
        $dataSource = new DataSource();
86
87
        $data = ['key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3'];
88
        $addData = ['key2' => 'newValue2', 'key4' => 'value4'];
89
        $mergedData = array_merge($data, $addData);
90
        $unsetData = ['key1', 'key4'];
91
        $finalData = ['key2' => 'newValue2', 'key3' => 'value3'];
92
        $key = 'key1';
93
        $value = 'value1';
94
95
        // Test getData
96
        $this->assertInternalType('array', $dataSource->getData());
97
        $this->assertSame([], $dataSource->getData());
98
        $this->assertNull($dataSource->getValue($key));
99
100
        // Test setData
101
        $this->assertInstanceOf(DataSource::class, $dataSource->setData($data));
102
        $this->assertSame($data, $dataSource->getData());
103
        $this->assertSame($value, $dataSource->getValue($key));
104
105
        // Test addData
106
        $this->assertInstanceOf(DataSource::class, $dataSource->addData($addData));
107
        $this->assertSame($mergedData, $dataSource->getData());
108
        $this->assertSame($value, $dataSource->getValue($key));
109
110
        // Test unsetData
111
        $this->assertInstanceOf(DataSource::class, $dataSource->unsetData($unsetData));
112
        $this->assertSame($finalData, $dataSource->getData());
113
        $this->assertNull($dataSource->getValue($key));
114
115
        // Test setValue
116
        $this->assertInstanceOf(DataSource::class, $dataSource->setValue($key, $value));
117
        $this->assertSame($value, $dataSource->getValue($key));
118
        $this->assertTrue(in_array($value, $dataSource->getData()));
119
120
        // Test unsetValue
121
        $this->assertInstanceOf(DataSource::class, $dataSource->unsetValue($key));
122
        $this->assertNull($dataSource->getValue($key));
123
        $this->assertFalse(in_array($value, $dataSource->getData()));
124
    }
125
}
126