1 | <?php |
||
15 | class AccessLoggedTest extends \PHPUnit_Framework_TestCase { |
||
16 | |||
17 | /** |
||
18 | * @var AccessLogged $object |
||
19 | */ |
||
20 | protected $object; |
||
21 | |||
22 | public function setUp() { |
||
23 | parent::setUp(); |
||
24 | |||
25 | $this->object = new AccessLogged(); |
||
26 | } |
||
27 | |||
28 | public function tearDown() { |
||
29 | unset($this->object); |
||
30 | parent::tearDown(); |
||
31 | } |
||
32 | |||
33 | public function test__construct() { |
||
34 | // Checking initial state |
||
35 | $this->assertTrue($this->object->isEmpty()); |
||
36 | $this->assertFalse(isset($this->object->test)); |
||
37 | $this->assertNull($this->object->test); |
||
38 | $this->assertAttributeEquals([], 'values', $this->object); |
||
39 | $this->assertAttributeEquals([], '_startValues', $this->object); |
||
40 | $this->assertAttributeEquals([], '_changes', $this->object); |
||
41 | $this->assertAttributeEquals([], '_deltas', $this->object); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Checking values setting |
||
46 | * |
||
47 | * @covers ::__set |
||
48 | * @covers ::valueSet |
||
49 | * @covers ::blockChange |
||
50 | * @covers ::getChanges |
||
51 | */ |
||
52 | public function testSet() { |
||
53 | // Checking base __set() |
||
54 | $this->object->test = 5; |
||
55 | $this->assertFalse($this->object->isEmpty()); |
||
56 | $this->assertTrue(isset($this->object->test)); |
||
57 | $this->assertTrue($this->object->__isset('test')); |
||
58 | $this->assertEquals(5, $this->object->test); |
||
59 | $this->assertAttributeEquals(['test' => 5], 'values', $this->object); |
||
60 | $this->assertAttributeEquals(['test' => 5], '_startValues', $this->object); |
||
61 | $this->assertAttributeEquals([], '_changes', $this->object); |
||
62 | $this->assertAttributeEquals([], '_deltas', $this->object); |
||
63 | |||
64 | // Checking first value change |
||
65 | $this->object->test = 6; |
||
66 | $this->assertEquals(6, $this->object->test); |
||
67 | $this->assertAttributeEquals(['test' => 6], 'values', $this->object); |
||
68 | $this->assertAttributeEquals(['test' => 5], '_startValues', $this->object); |
||
69 | $this->assertAttributeEquals(['test' => 6], '_changes', $this->object); |
||
70 | $this->assertAttributeEquals([], '_deltas', $this->object); |
||
71 | |||
72 | // Checking second value change |
||
73 | $this->object->test = 7; |
||
74 | $this->assertEquals(7, $this->object->test); |
||
75 | $this->assertAttributeEquals(['test' => 7], 'values', $this->object); |
||
76 | $this->assertAttributeEquals(['test' => 5], '_startValues', $this->object); |
||
77 | $this->assertAttributeEquals(['test' => 7], '_changes', $this->object); |
||
78 | $this->assertAttributeEquals([], '_deltas', $this->object); |
||
79 | |||
80 | // Checking changes extraction |
||
81 | $this->assertAttributeEquals($this->object->getChanges(), '_changes', $this->object); |
||
82 | |||
83 | // Checking change block after increment |
||
84 | $this->expectExceptionMessage('Common\AccessLogged::test2 already INCREMENTED/DECREMENTED - can not CHANGE'); |
||
85 | $this->object->inc()->test2 = 7; |
||
86 | $this->object->test2 = 10; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Checking values changing |
||
91 | * |
||
92 | * @covers ::inc |
||
93 | * @covers ::dec |
||
94 | * @covers ::__set |
||
95 | * @covers ::valueSet |
||
96 | * @covers ::valueDelta |
||
97 | * @covers ::getDeltas |
||
98 | * @covers ::blockDelta |
||
99 | * @covers ::clear |
||
100 | * @covers ::acceptChanges |
||
101 | */ |
||
102 | public function testDelta() { |
||
103 | // Checking base inc() |
||
104 | $this->object->inc()->fromZero = 5; |
||
105 | $this->assertFalse($this->object->isEmpty()); |
||
106 | $this->assertTrue(isset($this->object->fromZero)); |
||
107 | $this->assertTrue($this->object->__isset('fromZero')); |
||
108 | $this->assertEquals(5, $this->object->fromZero); |
||
109 | $this->assertAttributeEquals(['fromZero' => 5], 'values', $this->object); |
||
110 | $this->assertAttributeEquals(['fromZero' => 0], '_startValues', $this->object); |
||
111 | $this->assertAttributeEquals(['fromZero' => 5], '_deltas', $this->object); |
||
112 | $this->assertAttributeEquals([], '_changes', $this->object); |
||
113 | |||
114 | // Checking base dec() |
||
115 | $this->object->dec()->fromZeroDec = 5; |
||
116 | $this->assertFalse($this->object->isEmpty()); |
||
117 | $this->assertTrue(isset($this->object->fromZeroDec)); |
||
118 | $this->assertTrue($this->object->__isset('fromZeroDec')); |
||
119 | $this->assertEquals(-5, $this->object->fromZeroDec); |
||
120 | $this->assertAttributeEquals(['fromZero' => 5, 'fromZeroDec' => -5], 'values', $this->object); |
||
121 | $this->assertAttributeEquals(['fromZero' => 0, 'fromZeroDec' => 0], '_startValues', $this->object); |
||
122 | $this->assertAttributeEquals(['fromZero' => 5, 'fromZeroDec' => -5], '_deltas', $this->object); |
||
123 | $this->assertAttributeEquals([], '_changes', $this->object); |
||
124 | |||
125 | // Checking deltas extraction |
||
126 | $this->assertAttributeEquals($this->object->getDeltas(), '_deltas', $this->object); |
||
127 | |||
128 | // Checking flush() |
||
129 | $this->object->changed = 7; |
||
130 | $this->object->changed = 8; |
||
131 | $this->assertAttributeEquals(['changed' => 8], '_changes', $this->object); |
||
132 | $this->object->acceptChanges(); |
||
133 | $this->assertAttributeEquals(['fromZero' => 5, 'fromZeroDec' => -5, 'changed' => 8], 'values', $this->object); |
||
134 | $this->assertAttributeEquals(['fromZero' => 5, 'fromZeroDec' => -5, 'changed' => 8], '_startValues', $this->object); |
||
135 | $this->assertAttributeEquals([], '_deltas', $this->object); |
||
136 | $this->assertAttributeEquals([], '_changes', $this->object); |
||
137 | |||
138 | // Checking clear() |
||
139 | $this->object->changed = 9; |
||
140 | $this->object->inc()->fromZero = 5; |
||
141 | $this->object->dec()->fromZeroDec = 5; |
||
142 | $this->assertAttributeEquals(['fromZero' => 10, 'fromZeroDec' => -10, 'changed' => 9], 'values', $this->object); |
||
143 | $this->assertAttributeEquals(['fromZero' => 5, 'fromZeroDec' => -5, 'changed' => 8], '_startValues', $this->object); |
||
144 | $this->assertAttributeEquals(['fromZero' => 5, 'fromZeroDec' => -5], '_deltas', $this->object); |
||
145 | $this->assertAttributeEquals(['changed' => 9], '_changes', $this->object); |
||
146 | $this->object->clear(); |
||
147 | $this->test__construct(); |
||
148 | |||
149 | // Checking delta block after direct change |
||
150 | $this->expectExceptionMessage('Common\AccessLogged::changed already changed - can not use DELTA'); |
||
151 | // First change is legit - filling startValue |
||
152 | $this->object->changed = 7; |
||
153 | // Second change makes value unchangeable by delta |
||
154 | $this->object->changed = 8; |
||
155 | // Raising exception |
||
156 | $this->object->inc()->changed = 10; |
||
157 | } |
||
158 | |||
159 | } |
||
160 |