forked from TYPO3/testing-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseTestCase.php
155 lines (142 loc) · 5.33 KB
/
BaseTestCase.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
<?php
declare(strict_types=1);
namespace TYPO3\TestingFramework\Core;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
/**
* The mother of all test cases.
*
* Don't sub class this test case but rather choose a more specialized base test case,
* such as UnitTestCase or FunctionalTestCase
*/
abstract class BaseTestCase extends TestCase
{
/**
* Creates a mock object which allows for calling protected methods and access of protected properties.
*
* @template T
* @param class-string<T> $originalClassName name of class to create the mock object of, must not be empty
* @param string[]|null $methods name of the methods to mock, null for "mock no methods"
* @param array $arguments arguments to pass to constructor
* @param string $mockClassName the class name to use for the mock class
* @param bool $callOriginalConstructor whether to call the constructor
* @param bool $callOriginalClone whether to call the __clone method
* @param bool $callAutoload whether to call any autoload function
*
* @return MockObject|AccessibleObjectInterface&T
* a mock of $originalClassName with access methods added
*
* @throws \InvalidArgumentException
*/
protected function getAccessibleMock(
$originalClassName,
$methods = [],
array $arguments = [],
$mockClassName = '',
$callOriginalConstructor = true,
$callOriginalClone = true,
$callAutoload = true
) {
if ($originalClassName === '') {
throw new \InvalidArgumentException('$originalClassName must not be empty.', 1334701880);
}
$mockBuilder = $this->getMockBuilder($this->buildAccessibleProxy($originalClassName))
->setMethods($methods)
->setConstructorArgs($arguments)
->setMockClassName($mockClassName);
if (!$callOriginalConstructor) {
$mockBuilder->disableOriginalConstructor();
}
if (!$callOriginalClone) {
$mockBuilder->disableOriginalClone();
}
if (!$callAutoload) {
$mockBuilder->disableAutoload();
}
return $mockBuilder->getMock();
}
/**
* Returns a mock object which allows for calling protected methods and access
* of protected properties. Concrete methods to mock can be specified with
* the last parameter
*
* @template T
* @param class-string<T> $originalClassName Full qualified name of the original class
* @param array $arguments
* @param string $mockClassName
* @param bool $callOriginalConstructor
* @param bool $callOriginalClone
* @param bool $callAutoload
* @param array $mockedMethods
* @return MockObject|AccessibleObjectInterface&T
*
* @throws \InvalidArgumentException
*/
protected function getAccessibleMockForAbstractClass(
$originalClassName,
array $arguments = [],
$mockClassName = '',
$callOriginalConstructor = true,
$callOriginalClone = true,
$callAutoload = true,
$mockedMethods = []
) {
if ($originalClassName === '') {
throw new \InvalidArgumentException('$originalClassName must not be empty.', 1384268260);
}
return $this->getMockForAbstractClass(
$this->buildAccessibleProxy($originalClassName),
$arguments,
$mockClassName,
$callOriginalConstructor,
$callOriginalClone,
$callAutoload,
$mockedMethods
);
}
/**
* Creates a proxy class of the specified class which allows
* for calling even protected methods and access of protected properties.
*
* @param string $className Name of class to make available, must not be empty
* @return string Fully qualified name of the built class, will not be empty
*/
protected function buildAccessibleProxy($className)
{
$accessibleClassName = $this->getUniqueId('Tx_Phpunit_AccessibleProxy');
$reflectionClass = new \ReflectionClass($className);
eval(
($reflectionClass->isAbstract() ? 'abstract ' : '') . 'class ' . $accessibleClassName .
' extends ' . $className . ' implements ' . AccessibleObjectInterface::class . ' {' .
' use ' . AccessibleProxyTrait::class . ';' .
'}'
);
return $accessibleClassName;
}
/**
* Create and return a unique id optionally prepended by a given string
*
* This function is used because on windows and in cygwin environments uniqid() has a resolution of one second which
* results in identical ids if simply uniqid('Foo'); is called.
*
* @param string $prefix
* @return string
*/
protected function getUniqueId($prefix = '')
{
$uniqueId = uniqid((string)mt_rand(), true);
return $prefix . str_replace('.', '', $uniqueId);
}
}