Skip to content
This repository was archived by the owner on May 13, 2024. It is now read-only.

Commit 813e5e5

Browse files
committed
Adds method to AbstractTestCase to create a missing argument expectation.
1 parent aefe80b commit 813e5e5

File tree

1 file changed

+106
-20
lines changed

1 file changed

+106
-20
lines changed

tests/unit/AbstractTestCase.php

Lines changed: 106 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,37 @@
66

77
abstract class AbstractTestCase extends TestCase
88
{
9-
//////////////////////////////// PUBLIC API \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
9+
//////////////////////////// SETTERS AND GETTERS \\\\\\\\\\\\\\\\\\\\\\\\\\\
1010

11-
/**
12-
* @param int $argumentCount
13-
*
14-
* @return array
15-
*/
16-
final public function createMissingArgumentExpectation($argumentCount)
11+
/** @return string[] */
12+
private function getArgumentCountError()
1713
{
1814
$expectedExceptions = array(
1915
'5' => class_exists('\\PHPUnit_Framework_Error') ? '\\PHPUnit_Framework_Error' : '\\PHPUnit\\Framework\\Error',
2016
'7' => '\\ArgumentCountError',
2117
'70' => class_exists('\\PHPUnit_Framework_Error_Warning') ? '\\PHPUnit_Framework_Error_Warning' : '\\PHPUnit\\Framework\\Error\\Warning',
2218
);
23-
24-
$expectedExceptionMessages = array(
25-
'5' => 'Missing argument ' . $argumentCount,
26-
'7' => 'Too few arguments',
27-
'70' => 'Missing argument ' . $argumentCount,
28-
);
29-
30-
$function = $this->getExpectExpectationMethodName();
31-
3219
$expectedException = $expectedExceptions[PHP_MAJOR_VERSION];
3320
if (array_key_exists(PHP_MAJOR_VERSION . PHP_MINOR_VERSION, $expectedExceptions) === true) {
3421
$expectedException = $expectedExceptions[PHP_MAJOR_VERSION . PHP_MINOR_VERSION];
3522
}
3623

37-
$expectedExceptionMessage = $expectedExceptionMessages[PHP_MAJOR_VERSION];
38-
if (array_key_exists(PHP_MAJOR_VERSION . PHP_MINOR_VERSION, $expectedExceptionMessages) === true) {
39-
$expectedExceptionMessage = $expectedExceptionMessages[PHP_MAJOR_VERSION . PHP_MINOR_VERSION];
24+
return $expectedException;
25+
}
26+
27+
/** @return string[] */
28+
private function getArgumentTypeError()
29+
{
30+
$expectedExceptions = array(
31+
'5' => class_exists('\\PHPUnit_Framework_Error') ? '\\PHPUnit_Framework_Error' : '\\PHPUnit\\Framework\\Error',
32+
'7' => '\\TypeError',
33+
);
34+
$expectedException = $expectedExceptions[PHP_MAJOR_VERSION];
35+
if (array_key_exists(PHP_MAJOR_VERSION . PHP_MINOR_VERSION, $expectedExceptions) === true) {
36+
$expectedException = $expectedExceptions[PHP_MAJOR_VERSION . PHP_MINOR_VERSION];
4037
}
4138

42-
return array($function, $expectedException, $expectedExceptionMessage);
39+
return $expectedException;
4340
}
4441

4542
/**
@@ -48,14 +45,103 @@ final public function createMissingArgumentExpectation($argumentCount)
4845
final public function getExpectExpectationMethodName()
4946
{
5047
$function = 'setExpectedException';
48+
5149
if (method_exists($this, $function) === false) {
5250
$function = 'expectException';
5351
}
5452

5553
return $function;
5654
}
5755

56+
/**
57+
* @return string
58+
*/
59+
final public function getExpectExpectationRegexpMethodName()
60+
{
61+
$function = 'setExpectedExceptionRegExp';
62+
if (method_exists($this, $function) === false) {
63+
$function = 'expectExceptionMessageRegExp';
64+
}
65+
66+
return $function;
67+
}
68+
69+
//////////////////////////////// PUBLIC API \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
70+
71+
/**
72+
* @param int $argumentCount
73+
*
74+
* @return string[]
75+
*/
76+
final public function createMissingArgumentExpectation($argumentCount)
77+
{
78+
$function = $this->getExpectExpectationMethodName();
79+
80+
$expectedException = $this->getArgumentCountError();
81+
82+
$expectedExceptionMessage = $this->getMissingArgumentMessage($argumentCount);
83+
84+
return array($function, $expectedException, $expectedExceptionMessage);
85+
}
86+
87+
/**
88+
* @param string $argumentType
89+
*
90+
* @return string[]
91+
*/
92+
final public function createWrongArgumentTypeExpectation($argumentType)
93+
{
94+
$function = $this->getExpectExpectationMethodName();
95+
96+
$expectedException = $this->getArgumentTypeError();
97+
98+
$expectedExceptionMessage = $this->getWrongArgumentTypeMessage($argumentType);
99+
100+
return array($function, $expectedException, $expectedExceptionMessage);
101+
}
102+
58103
////////////////////////////// UTILITY METHODS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\
104+
105+
/**
106+
* @param int $argumentCount
107+
*
108+
* @return string[]
109+
*/
110+
private function getMissingArgumentMessage($argumentCount)
111+
{
112+
$expectedExceptionMessages = array(
113+
'5' => 'Missing argument ' . $argumentCount,
114+
'7' => 'Too few arguments',
115+
'70' => 'Missing argument ' . $argumentCount,
116+
);
117+
$expectedExceptionMessage = $expectedExceptionMessages[PHP_MAJOR_VERSION];
118+
if (array_key_exists(PHP_MAJOR_VERSION . PHP_MINOR_VERSION, $expectedExceptionMessages) === true) {
119+
$expectedExceptionMessage = $expectedExceptionMessages[PHP_MAJOR_VERSION . PHP_MINOR_VERSION];
120+
}
121+
122+
return $expectedExceptionMessage;
123+
}
124+
125+
/**
126+
* @param string $argumentType
127+
*
128+
* @return string[]
129+
*/
130+
private function getWrongArgumentTypeMessage($argumentType)
131+
{
132+
$expectedExceptionMessages = array(
133+
'5' => 'must be of the type ' . $argumentType,
134+
'7' => 'must be of the type ' . $argumentType,
135+
'53' => 'must be an ' . $argumentType,
136+
'71' => 'Too few arguments',
137+
);
138+
$expectedExceptionMessage = $expectedExceptionMessages[PHP_MAJOR_VERSION];
139+
if (array_key_exists(PHP_MAJOR_VERSION . PHP_MINOR_VERSION, $expectedExceptionMessages) === true) {
140+
$expectedExceptionMessage = $expectedExceptionMessages[PHP_MAJOR_VERSION . PHP_MINOR_VERSION];
141+
}
142+
143+
return $expectedExceptionMessage;
144+
}
59145
}
60146

61147
/*EOF*/

0 commit comments

Comments
 (0)