-
Notifications
You must be signed in to change notification settings - Fork 2
/
TestCase.php
147 lines (119 loc) · 4.22 KB
/
TestCase.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
145
146
147
<?php
namespace ModelTest;
use Model\Cluster\Schema;
abstract class TestCase extends \PHPUnit_Framework_TestCase
{
/**
* @var \Model\Db\Mysql
*/
private static $db;
/**
* @var Schema
*/
protected static $_schema;
public static function setDb($db)
{
self::$db = $db;
}
/**
* @param bool $isRenew
* @return Schema
*/
protected function getSchema($isRenew = false)
{
if (self::$_schema instanceof Schema && !$isRenew) {
return self::$_schema;
}
self::$_schema = new \Model\Cluster\Schema('model_test', $this->getDb());
self::$_schema->init();
return self::$_schema;
}
/**
* @param $class
* @param $methodName
* @return \ReflectionMethod
*/
protected static function getMethod($class, $methodName)
{
$class = new ReflectionClass($class);
$method = $class->getMethod($methodName);
$method->setAccessible(true);
return $method;
}
protected function _testParamSettersAndGetters($object, $name, $elementType = null)
{
if (!is_object($object)) {
return;
}
$nameAsCameCase = implode('', array_map('ucfirst', explode('_', $name)));
$objectClass = get_class($object);
$objectMethods = get_class_methods($objectClass);
$setMethodName = 'set' . $nameAsCameCase;
if (in_array($setMethodName, $objectMethods)) {
$value = $this->getRandomValue($elementType);
$this->assertInstanceOf($objectClass, $object->$setMethodName('name', $value));
$getMethodName = 'get' . $nameAsCameCase;
if (in_array($getMethodName, $objectMethods)) {
$this->assertEquals($value, $object->$getMethodName('name'));
}
}
$getMethodName = 'get' . $nameAsCameCase;
$getExists = (in_array($getMethodName, $objectMethods));
if (!$getExists) {
return;
}
$getsMethodName = 'get' . $nameAsCameCase . 's';
if (in_array($getsMethodName, $objectMethods)) {
$this->assertInternalType('array', $object->$getsMethodName());
}
}
protected function _testSettersAndGetters($object, $name, $elementType = null, $allowDefault = false)
{
if (!is_object($object)) {
return;
}
$nameAsCameCase = implode('', array_map('ucfirst', explode('_', $name)));
$objectClass = get_class($object);
$objectMethods = get_class_methods($objectClass);
$setMethodName = 'set' . $nameAsCameCase;
if (in_array($setMethodName, $objectMethods)) {
$value = $this->getRandomValue($elementType);
$this->assertInstanceOf($objectClass, $object->$setMethodName($value));
$getMethodName = 'get' . $nameAsCameCase;
if (in_array($getMethodName, $objectMethods)) {
$this->assertEquals($value, $object->$getMethodName());
if ($elementType == 'bool') {
$this->assertInternalType('bool', $object->$setMethodName('on')->$getMethodName());
$this->assertEquals(true, $object->$setMethodName('on')->$getMethodName());
$this->assertEquals(true, $object->$setMethodName('off')->$getMethodName());
$this->assertEquals(true, $object->$setMethodName('-1')->$getMethodName());
$this->assertEquals(false, $object->$setMethodName('0')->$getMethodName());
$this->assertEquals(true, $object->$setMethodName('1')->$getMethodName());
if ($allowDefault) {
$object->$setMethodName(null);
$this->assertEquals(true, $object->$getMethodName(true));
}
}
}
}
}
public function getRandomValue($type)
{
switch ($type) {
case 'boolean':
case 'bool':
return (bool)mt_rand(0,1);
break;
default:
return uniqid('', true);
}
}
/**
*
* @return \Model\Db\Mysql
*/
public function getDb()
{
return self::$db;
}
}