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

Commit eccc99f

Browse files
committed
Adds tests for the AbstractTraitShim class.
1 parent 813e5e5 commit eccc99f

File tree

1 file changed

+154
-0
lines changed

1 file changed

+154
-0
lines changed
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
3+
namespace Potherca\PhpUnit\Shim;
4+
5+
use Potherca\PhpUnit\AbstractTestCase;
6+
7+
/*/ Mock function from global scope. Ugly but functional /*/
8+
$container = new \stdClass();
9+
$container->args = null;
10+
function call_user_func_array($function, $params) {
11+
global $container;
12+
13+
$container->args = func_get_args();
14+
}
15+
16+
/**
17+
* Tests for the AbstractTraitShim class
18+
*
19+
* @coversDefaultClass \Potherca\PhpUnit\Shim\AbstractTraitShim
20+
* @covers ::<!public>
21+
*/
22+
abstract class AbstractTraitShimTest extends AbstractTestCase
23+
{
24+
/////////////////////////////////// TESTS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
25+
26+
/**
27+
* @coversNothing
28+
*/
29+
final public function testShimShouldComplainWhenInstantiatedWithoutAnything()
30+
{
31+
list($function, $expectedException, $expectedExceptionMessage) = $this->createMissingArgumentExpectation(1);
32+
33+
$this->{$function}($expectedException, $expectedExceptionMessage);
34+
35+
/** @noinspection PhpParamsInspection */
36+
new GetCompatibleExceptionName();
37+
}
38+
39+
/**
40+
* @covers \Potherca\PhpUnit\Shim\AbstractTraitShim::__construct
41+
*/
42+
final public function testShimShouldComplainWhenInstantiatedWithoutTestCase()
43+
{
44+
$expectedException = '\\Potherca\\PhpUnit\\InvalidArgumentException';
45+
$expectedExceptionMessage = 'Argument 1 passed to Potherca\PhpUnit\Shim\AbstractTraitShim::__construct must be an instance of "\PHPUnit_Framework_TestCase" or "\PHPUnit\Framework\TestCase", stdClass given';
46+
47+
$function = $this->getExpectExpectationMethodName();
48+
49+
$this->{$function}($expectedException, $expectedExceptionMessage);
50+
51+
new GetCompatibleExceptionName(new \stdClass());
52+
}
53+
54+
/**
55+
* @covers \Potherca\PhpUnit\Shim\AbstractTraitShim::__construct
56+
*
57+
* @return array
58+
*/
59+
final public function testShimShouldBeInstantiatedWhenGivenTestCase()
60+
{
61+
$calledClass = get_called_class();
62+
$calledClass = substr($calledClass, 0, -4); // 4 = strlen('Test')
63+
64+
$mockName = class_exists('\PHPUnit_Framework_TestCase')
65+
? '\PHPUnit_Framework_TestCase'
66+
: '\PHPUnit\Framework\TestCase'
67+
;
68+
69+
$mockTestCase = $this->getMockBuilder($mockName)->disableOriginalConstructor()->getMockForAbstractClass();
70+
71+
$shim = new $calledClass($mockTestCase);
72+
73+
$this->assertInstanceOf($calledClass, $shim);
74+
75+
return array(
76+
'mock-test-case' => $mockTestCase,
77+
'shim' => $shim,
78+
);
79+
}
80+
81+
/**
82+
* @param array $param
83+
*
84+
* @return array
85+
*
86+
* @covers \Potherca\PhpUnit\Shim\AbstractTraitShim::getTestcase
87+
*
88+
* @depends testShimShouldBeInstantiatedWhenGivenTestCase
89+
*/
90+
final public function testShimShouldReturnGivenTestcaseWhenAskedToGetTestCase(array $param)
91+
{
92+
$actual = $param['shim']->getTestcase();
93+
$expected = $param['mock-test-case'];
94+
95+
$this->assertSame($actual, $expected);
96+
97+
return $param;
98+
}
99+
100+
/**
101+
* @param array $param
102+
*
103+
* @covers \Potherca\PhpUnit\Shim\AbstractTraitShim::__invoke
104+
*
105+
* @depends testShimShouldReturnGivenTestcaseWhenAskedToGetTestCase
106+
*/
107+
final public function testShimShouldComplainWhenInvokedWithoutParameters(array $param)
108+
{
109+
list($function, $expectedException, $expectedExceptionMessage) = $this->createWrongArgumentTypeExpectation('array');
110+
111+
$this->{$function}($expectedException, $expectedExceptionMessage);
112+
113+
$actual = $param['shim']();
114+
}
115+
116+
/**
117+
* @param array $param
118+
*
119+
* @covers \Potherca\PhpUnit\Shim\AbstractTraitShim::__invoke
120+
*
121+
* @depends testShimShouldReturnGivenTestcaseWhenAskedToGetTestCase
122+
*/
123+
final public function testShimShouldMethodWithSameNameAsShimWhenInvokedWithParameters(array $param)
124+
{
125+
global $container;
126+
127+
/* Setup test */
128+
$shim = $param['shim'];
129+
$mockParameter = array('mock-key' => 'mock-value');
130+
131+
/* Call system under test */
132+
$shim($mockParameter);
133+
134+
/* Set up expectations */
135+
$parts = explode('\\', get_class($shim));
136+
$expectedMethod = array_pop($parts);
137+
138+
$expected = array(
139+
array($shim, $expectedMethod),
140+
$mockParameter,
141+
);
142+
143+
$actual = $container->args;
144+
145+
$this->assertSame($expected, $actual);
146+
}
147+
148+
final public function testShim_Should_When_GetExistingClassName()
149+
{
150+
$this->markTestIncomplete('@TODO: Write tests for \Potherca\PhpUnit\Shim\AbstractTraitShim::getExistingClassName()');
151+
}
152+
}
153+
154+
/*EOF*/

0 commit comments

Comments
 (0)