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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
<?php
namespace phpmock\generator;
use PHPUnit\Framework\TestCase;
/**
* Tests ParameterBuilder.
*
* @author Markus Malkusch <markus@malkusch.de>
* @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
* @license http://www.wtfpl.net/txt/copying/ WTFPL
* @see ParameterBuilder
*/
class ParameterBuilderTest extends TestCase
{
/**
* Tests build().
*
* @param string $expectedSignature The expected signature parameters.
* @param string $expectedBody The expected body parameters.
* @param string $function The function name.
*
* @dataProvider provideTestBuild
*/
public function testBuild($expectedSignature, $expectedBody, $function)
{
$builder = new ParameterBuilder();
$builder->build($function);
$this->assertSame($expectedSignature, $builder->getSignatureParameters());
$this->assertSame($expectedBody, $builder->getBodyParameters());
}
/**
* Returns test cases for testBuild().
*
* @return string[][][] The test cases.
*/
public static function provideTestBuild()
{
// @codingStandardsIgnoreStart
function testNoParameter()
{
}
function testOneParameter($one)
{
}
function testTwoParameters($one, $two)
{
}
function testOptionalParameters1($one = 1)
{
}
function testOptionalParameters2($one = 1, $two = 2)
{
}
function testOptionalParameters3($one, $two = 2)
{
}
function testReference1(&$one)
{
}
function testReference2(&$one, $two)
{
}
function testReference3($one, &$two)
{
}
function testReference4(&$one, &$two)
{
}
function testCombined($one, &$two, $three = 3, &$four = 4)
{
}
function testPHPVariadics1(...$one)
{
}
function testPHPVariadics2($one, ...$two)
{
}
function testPHPVariadics3($one, $two = 2, ...$three)
{
}
function testPHPVariadics4(&$one, $two = 2, ...$three)
{
}
// When declaring a function or a method, adding a required parameter
// after optional parameters is deprecated since PHP 8.0. So, let's
// use conditional eval() here and avoid parsing this part of file
// as a function in PHP8.0+.
if (version_compare(PHP_VERSION, '8.0', '<')) {
eval(
'namespace ' . __NAMESPACE__ . ';
function testOptionalParametersBeforeRequired($one = 1, $two)
{
}'
);
}
// @codingStandardsIgnoreEnd
// PHP8.0+ has a different signature wording.
$return_value = version_compare(PHP_VERSION, '8', '<') ? "return_value" : "result_code";
// HHVM has a different signature wording.
if (defined('HHVM_VERSION')) {
$return_value = "return_var";
}
$cases = [
["", "", __NAMESPACE__ . "\\testDoesNotExist"],
["", "", __NAMESPACE__ . "\\testNoParameter"],
['$one', '$one', __NAMESPACE__ . "\\testOneParameter"],
['$one, $two', '$one, $two', __NAMESPACE__ . "\\testTwoParameters"],
['$one, $two', '$one, $two', __NAMESPACE__ . "\\testTwoParameters"],
['&$one', '&$one', __NAMESPACE__ . "\\testReference1"],
['&$one, $two', '&$one, $two', __NAMESPACE__ . "\\testReference2"],
['$one, &$two', '$one, &$two', __NAMESPACE__ . "\\testReference3"],
['&$one, &$two', '&$one, &$two', __NAMESPACE__ . "\\testReference4"],
[
sprintf(
"\$command, &\$output = '%1\$s', &\${$return_value} = '%1\$s'",
MockFunctionGenerator::DEFAULT_ARGUMENT
),
"\$command, &\$output, &\${$return_value}",
"exec"
],
[
sprintf(
"\$one = '%s'",
MockFunctionGenerator::DEFAULT_ARGUMENT
),
'$one',
__NAMESPACE__ . "\\testOptionalParameters1"
],
[
sprintf(
"\$one = '%1\$s', \$two = '%1\$s'",
MockFunctionGenerator::DEFAULT_ARGUMENT
),
'$one, $two',
__NAMESPACE__ . "\\testOptionalParameters2"
],
[
sprintf(
"\$one, \$two = '%s'",
MockFunctionGenerator::DEFAULT_ARGUMENT
),
'$one, $two',
__NAMESPACE__ . "\\testOptionalParameters3"
],
[
sprintf(
"\$one, &\$two, \$three = '%1\$s', &\$four = '%1\$s'",
MockFunctionGenerator::DEFAULT_ARGUMENT
),
'$one, &$two, $three, &$four',
__NAMESPACE__ . "\\testCombined"
],
["", "", __NAMESPACE__ . "\\testPHPVariadics1"],
['$one', '$one', __NAMESPACE__ . "\\testPHPVariadics2"],
];
if (defined('HHVM_VERSION')) {
// HHVM has different implementation details
$cases = array_merge($cases, [
['$value1', '$value1', "min"],
['$one, $two', '$one, $two', __NAMESPACE__ . "\\testPHPVariadics3"],
['&$one, $two', '&$one, $two', __NAMESPACE__ . "\\testPHPVariadics4"],
]);
} else {
$cases = array_merge($cases, [
version_compare(PHP_VERSION, '8', '<') ? ["", "", "min"] : ['$value', '$value', "min"],
[
sprintf(
"\$one, \$two = '%s'",
MockFunctionGenerator::DEFAULT_ARGUMENT
),
'$one, $two',
__NAMESPACE__ . "\\testPHPVariadics3"
],
[
sprintf(
"&\$one, \$two = '%s'",
MockFunctionGenerator::DEFAULT_ARGUMENT
),
'&$one, $two',
__NAMESPACE__ . "\\testPHPVariadics4"
],
]);
}
if (version_compare(PHP_VERSION, '8.0', '<')) {
$cases = array_merge($cases, [
[
sprintf(
"\$one, \$two",
MockFunctionGenerator::DEFAULT_ARGUMENT
),
'$one, $two',
__NAMESPACE__ . "\\testOptionalParametersBeforeRequired"
],
]);
}
return $cases;
}
}
|