Completed
Branch trunk (78000f)
by SuperNova.WS
25:55 queued 09:00
created

AccessorsV2Test::testShare()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 20
nc 1
nop 0
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Created by Gorlum 10.08.2016 21:29
5
 */
6
7
use Common\AccessorsV2;
8
9
/**
10
 * Class AccessorsV2Test
11
 * @coversDefaultClass \Common\AccessorsV2
12
 */
13
class AccessorsV2Test extends PHPUnit_Framework_TestCase {
14
15
  /**
16
   * @var AccessorsV2 $object
17
   */
18
  protected $object;
19
20
  public function setUp() {
21
    parent::setUp();
22
23
    $this->object = new AccessorsV2();
24
  }
25
26
  public function tearDown() {
27
    unset($this->object);
28
    parent::tearDown();
29
  }
30
31
  /**
32
   * @covers ::__get
33
   * @covers ::__isset
34
   * @covers ::__set
35
   * @covers ::__unset
36
   * @covers ::__call
37
   * @covers ::isEmpty
38
   */
39
  public function test__get() {
40
    $this->assertTrue($this->object->isEmpty());
41
    $this->assertFalse(isset($this->object->test));
42
    $this->assertNull($this->object->test);
43
44
    $callable = function () {
45
      static $q = '';
46
47
      return 'value' . $q++;
48
    };
49
    $this->object->test = $callable;
50
    $this->assertFalse($this->object->isEmpty());
51
    $this->assertTrue(isset($this->object->test));
52
    $this->assertAttributeEquals(array('test' => $callable), 'accessors', $this->object);
53
    $this->assertAttributeEquals(array(), 'shared', $this->object);
54
    $this->assertAttributeEquals(array(), 'executed', $this->object);
55
56
    $this->assertEquals('value', $this->object->test());
57
    $this->assertAttributeEquals(array(), 'shared', $this->object);
58
    $this->assertAttributeEquals(array('test' => 'value'), 'executed', $this->object);
59
60
    // Checking that executed result is NOT shared - i.e. accessor function called every time
61
    $this->assertEquals('value1', $this->object->test());
62
    $this->assertAttributeEquals(array(), 'shared', $this->object);
63
    $this->assertAttributeEquals(array('test' => 'value1'), 'executed', $this->object);
64
65
    unset($this->object->test);
66
    $this->assertTrue($this->object->isEmpty());
67
    $this->assertFalse(isset($this->object->test));
68
    $this->assertNull($this->object->test);
69
    $this->assertAttributeEquals(array(), 'accessors', $this->object);
70
    $this->assertAttributeEquals(array(), 'shared', $this->object);
71
    $this->assertAttributeEquals(array(), 'executed', $this->object);
72
73
  }
74
75
  /**
76
   * Testing shared functions behavior and clear() function
77
   *
78
   * @covers ::__call
79
   * @covers ::share
80
   * @covers ::clear
81
   */
82
  public function testShare() {
83
    $callable = function () {
84
      static $q = '';
85
86
      return 'value' . $q++;
87
    };
88
89
    $this->object->share('test', $callable);
90
    $this->assertFalse($this->object->isEmpty());
91
    $this->assertTrue(isset($this->object->test));
92
    $this->assertAttributeEquals(array('test' => $callable), 'accessors', $this->object);
93
    $this->assertAttributeEquals(array('test' => true), 'shared', $this->object);
94
    $this->assertAttributeEquals(array(), 'executed', $this->object);
95
96
    $this->assertEquals('value', $this->object->test());
97
    $this->assertAttributeEquals(array('test' => true), 'shared', $this->object);
98
    $this->assertAttributeEquals(array('test' => 'value'), 'executed', $this->object);
99
100
    // Sequental call should NOT change stored value of shared function
101
    $this->assertEquals('value', $this->object->test());
102
    $this->assertAttributeEquals(array('test' => true), 'shared', $this->object);
103
    $this->assertAttributeEquals(array('test' => 'value'), 'executed', $this->object);
104
105
    $this->object->clear();
106
    $this->assertAttributeEquals(array(), 'accessors', $this->object);
107
    $this->assertAttributeEquals(array(), 'shared', $this->object);
108
    $this->assertAttributeEquals(array(), 'executed', $this->object);
109
  }
110
111
  // TODO - method test
112
  // TODO - Invoker test
113
114
  /**
115
   * @covers ::__set
116
   */
117
  public function testUseFunctionName() {
118
    $this->object->test = 'mt_rand';
119
    $this->assertFalse($this->object->isEmpty());
120
    $this->assertTrue($this->object->test instanceof \Common\Invoker);
121
    $this->assertAttributeNotEmpty('accessors', $this->object);
122
    $this->assertAttributeEquals(array(), 'shared', $this->object);
123
    $this->assertAttributeEquals(array(), 'executed', $this->object);
124
125
    $this->assertTrue(is_integer($stored = $this->object->test()));
126
    $this->assertAttributeEquals(array(), 'shared', $this->object);
127
    $this->assertAttributeEquals(array('test' => $stored), 'executed', $this->object);
128
  }
129
130
131
  public function helperMethod() {
132
    return 'value';
133
  }
134
135
  /**
136
   * How setting hook on class method performs
137
   *
138
   * @covers ::__set
139
   */
140
  public function testUseClass() {
141
    $this->object->test = array($this, 'helperMethod');
142
    $this->assertFalse($this->object->isEmpty());
143
    $this->assertTrue($this->object->test instanceof \Common\Invoker);
144
    $this->assertAttributeNotEmpty('accessors', $this->object);
145
    $this->assertAttributeEquals(array(), 'shared', $this->object);
146
    $this->assertAttributeEquals(array(), 'executed', $this->object);
147
148
    $this->assertEquals('value', $this->object->test());
149
    $this->assertAttributeEquals(array(), 'shared', $this->object);
150
    $this->assertAttributeEquals(array('test' => 'value'), 'executed', $this->object);
151
  }
152
153
  /**
154
   * @covers ::__set
155
   */
156
  public function testExceptionSettingNotExistingCallable() {
157
    $this->setExpectedException(
158
      '\Exception',
159
      'Error assigning callable in Common\AccessorsV2::set()! Callable labeled [test] is not a callable or not accessible in this scope'
160
    );
161
    $this->object->test = 12345;
162
  }
163
164
  protected function hiddenMethod() {
165
    return 'protected';
166
  }
167
168
  /**
169
   * @covers ::__set
170
   */
171
  public function testExceptionSettingInaccessibleMethod() {
172
    $this->setExpectedException(
173
      '\Exception',
174
      'Error assigning callable in Common\AccessorsV2::set()! Callable labeled [test] is not a callable or not accessible in this scope'
175
    );
176
    $this->object->test = array($this, 'hiddenMethod');
177
  }
178
179
  /**
180
   * @covers ::__call
181
   */
182
  public function testExceptionCallNotExistingFunction() {
183
    $this->setExpectedException('\Exception', 'No [test] accessor found on Common\AccessorsV2::Common\AccessorsV2::__call');
184
    $this->object->test();
185
  }
186
187
}
188