Skip to content

Commit 643f5cb

Browse files
author
Wout Gevaert
committed
Fix tests for trait
1 parent 020591a commit 643f5cb

File tree

1 file changed

+45
-34
lines changed

1 file changed

+45
-34
lines changed

tests/Unit/ErrorHandling/ErrorHelperTest.php renamed to tests/Unit/Traits/ErrorTraitTest.php

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
use TypeError;
2525
use PHPUnit\Framework\TestCase;
26-
use WikibaseSolutions\CypherDSL\ErrorHandling\ErrorHelper;
26+
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
2727

2828
/**
2929
* Dummy classes
@@ -34,24 +34,44 @@ class ErrorHelperDummyExtendsA extends ErrorHelperDummyA {};
3434
class ErrorHelperDummyExtendsB extends ErrorHelperDummyB {};
3535

3636
/**
37-
* @covers \WikibaseSolutions\CypherDSL\ErrorHandling\ErrorHelper
37+
* Tester/Mock class
3838
*/
39-
class ErrorHelperTest extends TestCase
39+
class ErrorImpl {
40+
use ErrorTrait;
41+
42+
/**
43+
* Overcome private method problems
44+
*/
45+
public function call($funcName, $args) {
46+
return call_user_func_array([self::class, $funcName], $args);
47+
}
48+
}
49+
50+
/**
51+
* @covers \WikibaseSolutions\CypherDSL\Traits\ErrorTrait
52+
*/
53+
class ErrorTraitTest extends TestCase
4054
{
55+
protected ErrorImpl $errorImpl;
56+
57+
public function setUp() : void {
58+
$this->errorImpl = new ErrorImpl();
59+
}
60+
4161
/**
4262
* @doesNotPerformAssertions
4363
* @dataProvider CorrectAssertionsProvider
4464
*/
4565
public function testAssertClass($classNames, $userInput) {
46-
ErrorHelper::assertClass('foo', $classNames, $userInput);
66+
$this->errorImpl->call('assertClass', ['foo', $classNames, $userInput]);
4767
}
4868

4969
/**
5070
* @dataProvider failingAssertionsProvider
5171
*/
5272
public function testAssertClassFailure($classNames, $userInput) {
5373
$this->expectException(TypeError::class);
54-
ErrorHelper::assertClass('foo', $classNames, $userInput);
74+
$this->errorImpl->call('assertClass', ['foo', $classNames, $userInput]);
5575
}
5676

5777
public function correctAssertionsProvider() {
@@ -74,44 +94,35 @@ public function failingAssertionsProvider() {
7494
public function testGetTypeErrorText() {
7595
$this->assertEquals(
7696
'$foo should be a WikibaseSolutions\CypherDSL\Tests\Unit\ErrorHandling\ErrorHelperDummyA object, integer "5" given.',
77-
ErrorHelper::getTypeErrorText('foo', [ErrorHelperDummyA::class], 5)
97+
$this->errorImpl->call('getTypeErrorText', ['foo', [ErrorHelperDummyA::class], 5])
7898
);
7999
$this->assertEquals(
80100
'$foo should be a ' .
81101
'WikibaseSolutions\CypherDSL\Tests\Unit\ErrorHandling\ErrorHelperDummyA or ' .
82102
'WikibaseSolutions\CypherDSL\Tests\Unit\ErrorHandling\ErrorHelperDummyB object, integer "5" given.',
83-
ErrorHelper::getTypeErrorText('foo', [ErrorHelperDummyA::class, ErrorHelperDummyB::class], 5)
103+
$this->errorImpl->call('getTypeErrorText', ['foo', [ErrorHelperDummyA::class, ErrorHelperDummyB::class], 5])
84104
);
85105
}
86106

87-
public function testGetUserInputInfo() {
88-
$this->assertEquals(
89-
'string "foo"',
90-
ErrorHelper::getUserInputInfo('foo')
91-
);
92-
$this->assertEquals(
93-
'integer "5"',
94-
ErrorHelper::getUserInputInfo(5)
95-
);
96-
$this->assertEquals(
97-
'double "3.14"',
98-
ErrorHelper::getUserInputInfo(3.14)
99-
);
100-
$this->assertEquals(
101-
'boolean "1"',
102-
ErrorHelper::getUserInputInfo(true)
103-
);
104-
$this->assertEquals(
105-
'array',
106-
ErrorHelper::getUserInputInfo(['foo', 'bar'])
107-
);
108-
$this->assertEquals(
109-
'anonymous class instance',
110-
ErrorHelper::getUserInputInfo(new class(){})
111-
);
107+
/**
108+
* @dataProvider getUserInputInfoProvider
109+
*/
110+
public function testGetUserInputInfo($expected, $input) {
112111
$this->assertEquals(
113-
'WikibaseSolutions\CypherDSL\Tests\Unit\ErrorHandling\ErrorHelperDummyA',
114-
ErrorHelper::getUserInputInfo(new ErrorHelperDummyA())
112+
$expected,
113+
$this->errorImpl->call('getUserInputInfo', [$input])
115114
);
116115
}
116+
117+
public function getUserInputInfoProvider() {
118+
return [
119+
[ 'string "foo"', 'foo' ],
120+
[ 'integer "5"', 5 ],
121+
[ 'double "3.14"', 3.14 ],
122+
[ 'boolean "1"', true ],
123+
[ 'array', ['foo', 'bar'] ],
124+
[ 'anonymous class instance', new class(){} ],
125+
[ 'WikibaseSolutions\CypherDSL\Tests\Unit\ErrorHandling\ErrorHelperDummyA', new ErrorHelperDummyA()]
126+
];
127+
}
117128
}

0 commit comments

Comments
 (0)