forked from directus/directus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phpunit.php
107 lines (83 loc) · 3.54 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
<?php
$loader = require __DIR__ . '/vendor/autoload.php';
if (!defined('BASE_PATH')) {
define('BASE_PATH', __DIR__);
}
if (!defined('DIRECTUS_PATH')) {
define('DIRECTUS_PATH', '/');
}
if (!defined('STATUS_COLUMN_NAME')) {
define('STATUS_COLUMN_NAME', 'active');
}
// force a timezone
date_default_timezone_set('America/New_York');
/**
* @param $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
$mockAdapter = $testCase->getMock('Zend\Db\Adapter\Adapter', null, [$mockDriver]);
return $mockAdapter;
}
function get_mock_connection($testCase, $attributes = [])
{
$mockDriver = get_mock_driver($testCase, $attributes);
// setup mock connection adapter
$mockConnectionAdapter = $testCase->getMock('Directus\Database\Connection', null, [$mockDriver]);
return $mockConnectionAdapter;
}
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 = $testCase->getMock('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 = $testCase->getMock('Zend\Db\Adapter\Driver\StatementInterface');
$mockStatement->expects($testCase->any())->method('execute')->will($testCase->returnValue($mockResult));
// Connection
$mockConnection = $testCase->getMock('Zend\Db\Adapter\Driver\ConnectionInterface');
$mockConnection->expects($testCase->any())->method('getCurrentSchema')->will($testCase->returnValue('directus_schema'));
// $mockConnection->expects($testCase->any())->method('getDatabasePlatformName')->will($testCase->returnValue($platformName));
// $mockConnection->expects($testCase->any())->method('getDriverName')->will($testCase->returnValue($platformName));
$mockDriver = $testCase->getMock('Zend\Db\Adapter\Driver\DriverInterface');
$mockDriver->expects($testCase->any())->method('createStatement')->will($testCase->returnValue($mockStatement));
$mockDriver->expects($testCase->any())->method('getConnection')->will($testCase->returnValue($mockConnection));
$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);
}
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);
}