forked from directus/v8-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phpunit.php
192 lines (157 loc) · 6.51 KB
/
phpunit.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
$loader = require __DIR__ . '/vendor/autoload.php';
// utils for I/O testing
require __DIR__ . '/tests/utils/io_functions.php';
// force a timezone
date_default_timezone_set('America/New_York');
/**
* @param $testCase
* @param $className
* @param array|null $methods
* @param array|null $arguments
*
* @return PHPUnit_Framework_MockObject_MockObject
*/
function create_mock($testCase, $className, array $methods = null, array $arguments = null)
{
$builder = new PHPUnit_Framework_MockObject_MockBuilder($testCase, $className);
if (is_array($methods) && count($methods) > 0) {
$builder->setMethods($methods);
}
if (is_array($arguments) && count($arguments) > 0) {
$builder->setConstructorArgs($arguments);
}
return $builder->disableOriginalConstructor()
->disableOriginalClone()
->disableArgumentCloning()
->disallowMockingUnknownTypes()
->getMock();
}
/**
* @param \PHPUnit\Framework\TestCase $testCase
* @param $attributes - mock attributes
*
* @return \Zend\Db\Adapter\Adapter
*/
function get_mock_adapter($testCase, $attributes = [])
{
$mockDriver = get_mock_driver($testCase, $attributes);
// setup mock adapter
$methods = ['getDriver', 'getConnection', 'getCurrentSchema', 'getPlatform'];
$mockAdapter = create_mock($testCase, 'Zend\Db\Adapter\Adapter', $methods, [$mockDriver]);
$mockAdapter->expects($testCase->any())->method('getDriver')->will($testCase->returnValue($mockDriver));
$mockAdapter->expects($testCase->any())->method('getConnection')->will($testCase->returnValue(get_mock_adapter_connection($testCase)));
$mockAdapter->expects($testCase->any())->method('getCurrentSchema')->will($testCase->returnValue('directus'));
$mockAdapter->expects($testCase->any())->method('getPlatform')->will($testCase->returnValue(get_mock_platform($testCase)));
return $mockAdapter;
}
/**
* @param PHPUnit_Framework_TestCase $testCase
* @param array $attributes
*
* @return PHPUnit_Framework_MockObject_MockObject
*/
function get_mock_connection($testCase, $attributes = [])
{
$mockDriver = get_mock_driver($testCase, $attributes);
// setup mock connection adapter
$mockConnectionAdapter = create_mock($testCase, '\Directus\Database\Connection', null, [$mockDriver]);
$platform = get_mock_platform($testCase, $attributes);
$mockConnectionAdapter->expects($testCase->any())->method('getPlatform')->will($testCase->returnValue($platform));
$mockConnectionAdapter->expects($testCase->any())->method('getDriver')->will($testCase->returnValue($mockDriver));
return $mockConnectionAdapter;
}
function get_connection($test)
{
}
function get_mock_platform($testCase, array $attributes = [])
{
// Platform
$platformName = isset($attributes['platform_name']) ? $attributes['platform_name'] : 'Mysql';
$platform = create_mock($testCase, \Zend\Db\Adapter\Platform\Mysql::class);
$platform->expects($testCase->any())->method('getIdentifierSeparator')->will($testCase->returnValue('.'));
$platform->expects($testCase->any())->method('getName')->will($testCase->returnValue($platformName));
return $platform;
}
function get_mock_adapter_connection($testCase)
{
$mockConnection = create_mock($testCase, 'Zend\Db\Adapter\Driver\ConnectionInterface');
$mockConnection->expects($testCase->any())->method('getCurrentSchema')->will($testCase->returnValue('directus_schema'));
$mockConnection->expects($testCase->any())->method('connect');
// $mockConnection->expects($testCase->any())->method('getDatabasePlatformName')->will($testCase->returnValue($platformName));
// $mockConnection->expects($testCase->any())->method('getDriverName')->will($testCase->returnValue($platformName));
return $mockConnection;
}
/**
* @param PHPUnit_Framework_TestCase $testCase
* @param array $attributes
*
* @return PHPUnit_Framework_MockObject_MockObject
*/
function get_mock_driver($testCase, $attributes = [])
{
$resultCount = 0;
if (isset($attributes['result_count'])) {
$resultCount = (int)$attributes['result_count'];
}
$resultData = null;
if (isset($attributes['result_data'])) {
$resultData = $attributes['result_data'];
}
$platformName = 'Mysql';
if (isset($attributes['platform_name'])) {
$platformName = $attributes['platform_name'];
}
// mock the adapter, driver, and parts
$mockResult = create_mock($testCase, 'Zend\Db\Adapter\Driver\ResultInterface');
$mockResult->expects($testCase->any())->method('count')->will($testCase->returnValue($resultCount));
$mockResult->expects($testCase->any())->method('current')->will($testCase->returnValue($resultData));
$mockStatement = create_mock($testCase, 'Zend\Db\Adapter\Driver\StatementInterface');
$mockStatement->expects($testCase->any())->method('execute')->will($testCase->returnValue($mockResult));
// Connection
$mockAdapterConnection = get_mock_adapter_connection($testCase);
$mockDriver = create_mock($testCase,'Zend\Db\Adapter\Driver\DriverInterface');
$mockDriver->expects($testCase->any())->method('createStatement')->will($testCase->returnValue($mockStatement));
$mockDriver->expects($testCase->any())->method('getConnection')->will($testCase->returnValue($mockAdapterConnection));
$mockDriver->expects($testCase->any())->method('getDatabasePlatformName')->will($testCase->returnValue($platformName));
return $mockDriver;
}
function get_mysql_schema($testCase, $attributes = [])
{
$mockAdapter = get_mock_adapter($testCase, $attributes);
return new \Directus\Database\Schemas\Sources\MySQLSchema($mockAdapter);
}
/**
* @param \PHPUnit\Framework\TestCase $testCase
* @param array $methods
*
* @return mixed
*/
function get_mock_mysql_schema($testCase, $methods = [])
{
$mockAdapter = get_mock_adapter($testCase);
$mockSchema = $testCase->getMockBuilder('\Directus\Database\Schemas\Sources\MySQLSchema')
->setConstructorArgs([$mockAdapter])
->setMethods($methods)
->getMock();
return $mockSchema;
}
function get_array_session()
{
$storage = new \Directus\Session\Storage\ArraySessionStorage();
return new \Directus\Session\Session($storage);
}
/**
* @param string $url
* @param array $params
*
* @return string
*/
function add_query_params($url, array $params)
{
$urlParts = parse_url($url);
if (is_array($urlParts) && isset($urlParts['query'])) {
$urlParts['query'] = array_merge($urlParts['query'], $params);
}
return unparse_url($urlParts);
}