/
MockerTest.php
144 lines (104 loc) · 3.7 KB
/
MockerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
* Lithium: the most rad php framework
*
* @copyright Copyright 2012, Union of RAD (http://union-of-rad.org)
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*/
namespace lithium\tests\cases\test;
use lithium\test\Mocker;
/**
* WARNING:
* No unit test should mock the same test as another
*/
class MockerTest extends \lithium\test\Unit {
public function setUp() {
Mocker::register();
}
public function testAutoloadRegister() {
Mocker::register();
$registered = spl_autoload_functions();
$this->assertTrue(in_array(array(
'lithium\test\Mocker',
'create'
), $registered));
}
public function testBasicCreation() {
$mockee = 'lithium\console\command\Mock';
Mocker::create($mockee);
$this->assertTrue(class_exists($mockee));
}
public function testBasicCreationExtendsCorrectParent() {
$mocker = 'lithium\console\Request';
$mockeeObj = new \lithium\console\request\Mock();
$this->assertTrue(is_a($mockeeObj, $mocker));
}
public function testCannotMockNonLithiumClasses() {
$mockee = 'stdClass\Mock';
Mocker::create($mockee);
$this->assertTrue(!class_exists($mockee));
}
public function testCannotCreateNonStandardMockClass() {
$mockee = 'lithium\console\request\Mocker';
Mocker::create($mockee);
$this->assertTrue(!class_exists($mockee));
}
public function testFilteringNonStaticClass() {
$dispatcher = new \lithium\console\dispatcher\Mock();
$originalResult = $dispatcher->config();
$dispatcher->applyFilter('config', function($self, $params, $chain) {
return array();
});
$filteredResult = $dispatcher->config();
$this->assertEqual(0, count($filteredResult));
$this->assertNotEqual($filteredResult, $originalResult);
}
public function testFilteringNonStaticClassCanReturnOriginal() {
$response = new \lithium\console\response\Mock();
$originalResult = $response->styles();
$response->applyFilter('styles', function($self, $params, $chain) {
return $chain->next($self, $params, $chain);
});
$filteredResult = $response->styles();
$this->assertEqual($filteredResult, $originalResult);
}
public function testFilteringStaticClass() {
$mockee = 'lithium\analysis\parser\Mock';
$code = 'echo "foobar";';
$originalResult = $mockee::tokenize($code, array('wrap' => true));
$mockee::applyFilter('tokenize', function($self, $params, $chain) {
return array();
});
$filteredResult = $mockee::tokenize($code, array('wrap' => true));
$this->assertEqual(0, count($filteredResult));
$this->assertNotEqual($filteredResult, $originalResult);
}
public function testFilteringStaticClassCanReturnOriginal() {
$mockee = 'lithium\analysis\inspector\Mock';
$originalResult = $mockee::methods('lithium\analysis\Inspector');
$mockee::applyFilter('tokenize', function($self, $params, $chain) {
return $chain->next($self, $params, $chain);
});
$filteredResult = $mockee::methods('lithium\analysis\Inspector');
$this->assertEqual($filteredResult, $originalResult);
}
public function testOriginalMethodNotCalled() {
$http = new \lithium\tests\mocks\security\auth\adapter\mockHttp\Mock;
$this->assertEqual(0, count($http->headers));
$http->_writeHeader('Content-type: text/html');
$this->assertEqual(1, count($http->headers));
$http->applyFilter('_writeHeader', function($self, $params, $chain) {
return false;
});
$http->_writeHeader('Content-type: application/pdf');
$this->assertEqual(1, count($http->headers));
}
public function testFilteringAFilteredMethod() {
$adapt = 'lithium\core\adaptable\Mock';
$adapt::applyFilter('_initAdapter', function($self, $params, $chain) {
return false;
});
$this->assertFalse($adapt::_initAdapter('foo', array()));
}
}
?>