Skip to content

Commit 6945978

Browse files
author
Wout Gevaert
committed
Improve ErrorHelper and write tests
1 parent f14a523 commit 6945978

File tree

8 files changed

+303
-13
lines changed

8 files changed

+303
-13
lines changed

src/ErrorHandling/ErrorHelper.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
11
<?php
22

3+
/*
4+
* Cypher DSL
5+
* Copyright (C) 2021 Wikibase Solutions
6+
*
7+
* This program is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU General Public License
9+
* as published by the Free Software Foundation; either version 2
10+
* of the License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20+
*/
21+
322
namespace WikibaseSolutions\CypherDSL\ErrorHandling;
423

524
use TypeError;
25+
use ReflectionClass;
626

727
/**
828
* Convenience class including simple assertions and error reporting functions
@@ -49,7 +69,7 @@ public static function getTypeErrorText(
4969
return
5070
"\$$varName should be a " .
5171
implode(' or ', $classNames) . " object, " .
52-
self::getUserInputInfo($userInput) . "given.";
72+
self::getUserInputInfo($userInput) . " given.";
5373
}
5474

5575
/**
@@ -61,8 +81,12 @@ public static function getTypeErrorText(
6181
public static function getUserInputInfo($userInput) : string {
6282
$info = gettype( $userInput );
6383
if ( $info === 'object' ) {
64-
$info = get_class( $userInput );
65-
} else {
84+
if ((new ReflectionClass($userInput))->isAnonymous()) {
85+
$info = 'anonymous class instance';
86+
} else {
87+
$info = get_class( $userInput );
88+
}
89+
} elseif (is_scalar($userInput)) {
6690
$info .= ' "' . (string) $userInput . '"';
6791
}
6892
return $info;

tests/Unit/Clauses/CallProcedureClauseTest.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
namespace WikibaseSolutions\CypherDSL\Tests\Unit\Clauses;
2323

2424
use PHPUnit\Framework\TestCase;
25+
use TypeError;
2526
use WikibaseSolutions\CypherDSL\Clauses\CallProcedureClause;
2627
use WikibaseSolutions\CypherDSL\Tests\Unit\TestHelper;
2728
use WikibaseSolutions\CypherDSL\Types\AnyType;
@@ -93,6 +94,7 @@ public function testWithArguments()
9394
public function testWithYield()
9495
{
9596
$callProcedureClause = new CallProcedureClause();
97+
9698
$callProcedureClause->setProcedure("apoc.json");
9799

98100
$a = $this->getQueryConvertableMock(Variable::class, "a");
@@ -104,4 +106,26 @@ public function testWithYield()
104106

105107
$this->assertSame("CALL apoc.json() YIELD a, b, c", $callProcedureClause->toQuery());
106108
}
107-
}
109+
110+
public function testYieldDoesNotAcceptAnyType() {
111+
$callProcedureClause = new CallProcedureClause();
112+
113+
$a = $this->getQueryConvertableMock(AnyType::class, "a");
114+
115+
$this->expectException(TypeError::class);
116+
117+
// callProcedureClause->yields requires a Variable
118+
$callProcedureClause->yields([$a]);
119+
}
120+
121+
public function testArgumentsOnlyAcceptsAnyType() {
122+
$callProcedureClause = new CallProcedureClause();
123+
124+
$a = new class() {};
125+
126+
$this->expectException(TypeError::class);
127+
128+
// $callProcedureClause->withArguments() requires Anytype
129+
$callProcedureClause->withArguments([$a]);
130+
}
131+
}

tests/Unit/Clauses/SetClauseTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use PHPUnit\Framework\TestCase;
2525
use TypeError;
2626
use WikibaseSolutions\CypherDSL\Assignment;
27+
use WikibaseSolutions\CypherDSL\Label;
2728
use WikibaseSolutions\CypherDSL\Clauses\SetClause;
2829
use WikibaseSolutions\CypherDSL\Tests\Unit\TestHelper;
2930
use WikibaseSolutions\CypherDSL\Types\AnyType;
@@ -77,6 +78,19 @@ public function testAcceptsAssignment()
7778
$set->toQuery();
7879
}
7980

81+
/**
82+
* @doesNotPerformAssertions
83+
*/
84+
public function testAcceptsLabel()
85+
{
86+
$set = new SetClause();
87+
$expression = $this->getQueryConvertableMock(Label::class, "(a)");
88+
89+
$set->addAssignment($expression);
90+
91+
$set->toQuery();
92+
}
93+
8094
public function testDoesNotAcceptAnyType()
8195
{
8296
$set = new SetClause();
@@ -88,4 +102,4 @@ public function testDoesNotAcceptAnyType()
88102

89103
$set->toQuery();
90104
}
91-
}
105+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
/*
4+
* Cypher DSL
5+
* Copyright (C) 2021 Wikibase Solutions
6+
*
7+
* This program is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU General Public License
9+
* as published by the Free Software Foundation; either version 2
10+
* of the License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20+
*/
21+
22+
namespace WikibaseSolutions\CypherDSL\Tests\Unit\ErrorHandling;
23+
24+
use TypeError;
25+
use PHPUnit\Framework\TestCase;
26+
use WikibaseSolutions\CypherDSL\ErrorHandling\ErrorHelper;
27+
28+
/**
29+
* Dummy classes
30+
*/
31+
class ErrorHelperDummyA {};
32+
class ErrorHelperDummyB {};
33+
class ErrorHelperDummyExtendsA extends ErrorHelperDummyA {};
34+
class ErrorHelperDummyExtendsB extends ErrorHelperDummyB {};
35+
36+
/**
37+
* @covers \WikibaseSolutions\CypherDSL\ErrorHandling\ErrorHelper
38+
*/
39+
class ErrorHelperTest extends TestCase
40+
{
41+
/**
42+
* @doesNotPerformAssertions
43+
* @dataProvider CorrectAssertionsProvider
44+
*/
45+
public function testAssertClass($classNames, $userInput) {
46+
ErrorHelper::assertClass('foo', $classNames, $userInput);
47+
}
48+
49+
/**
50+
* @dataProvider failingAssertionsProvider
51+
*/
52+
public function testAssertClassFailure($classNames, $userInput) {
53+
$this->expectException(TypeError::class);
54+
ErrorHelper::assertClass('foo', $classNames, $userInput);
55+
}
56+
57+
public function correctAssertionsProvider() {
58+
return [
59+
[ErrorHelperDummyA::class, new ErrorHelperDummyA()],
60+
[ErrorHelperDummyA::class, new ErrorHelperDummyExtendsA()],
61+
[[ErrorHelperDummyA::class, ErrorHelperDummyB::class], new ErrorHelperDummyB()],
62+
[[ErrorHelperDummyA::class, ErrorHelperDummyB::class], new ErrorHelperDummyExtendsB()],
63+
];
64+
}
65+
66+
public function failingAssertionsProvider() {
67+
return [
68+
[ErrorHelperDummyA::class, new ErrorHelperDummyB()],
69+
[ErrorHelperDummyExtendsA::class, new ErrorHelperDummyA()],
70+
[[ErrorHelperDummyA::class, ErrorHelperDummyExtendsB::class], new ErrorHelperDummyB()],
71+
];
72+
}
73+
74+
public function testGetTypeErrorText() {
75+
$this->assertEquals(
76+
'$foo should be a WikibaseSolutions\CypherDSL\Tests\Unit\ErrorHandling\ErrorHelperDummyA object, integer "5" given.',
77+
ErrorHelper::getTypeErrorText('foo', [ErrorHelperDummyA::class], 5)
78+
);
79+
$this->assertEquals(
80+
'$foo should be a ' .
81+
'WikibaseSolutions\CypherDSL\Tests\Unit\ErrorHandling\ErrorHelperDummyA or ' .
82+
'WikibaseSolutions\CypherDSL\Tests\Unit\ErrorHandling\ErrorHelperDummyB object, integer "5" given.',
83+
ErrorHelper::getTypeErrorText('foo', [ErrorHelperDummyA::class, ErrorHelperDummyB::class], 5)
84+
);
85+
}
86+
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+
);
112+
$this->assertEquals(
113+
'WikibaseSolutions\CypherDSL\Tests\Unit\ErrorHandling\ErrorHelperDummyA',
114+
ErrorHelper::getUserInputInfo(new ErrorHelperDummyA())
115+
);
116+
}
117+
}

tests/Unit/ExpressionListTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
namespace WikibaseSolutions\CypherDSL\Tests\Unit;
2323

24+
use TypeError;
2425
use PHPUnit\Framework\TestCase;
2526
use WikibaseSolutions\CypherDSL\ExpressionList;
2627
use WikibaseSolutions\CypherDSL\Query;
@@ -80,4 +81,12 @@ public function provideMultidimensionalData(): array
8081
[[new ExpressionList([Query::literal('12'), Query::literal('14')]), new ExpressionList([Query::literal('13')])], "[['12', '14'], ['13']]"]
8182
];
8283
}
83-
}
84+
85+
public function testRequiresAnyType() {
86+
$a = new class() {};
87+
88+
$this->expectException(TypeError::class);
89+
90+
new ExpressionList([$a]);
91+
}
92+
}

tests/Unit/Functions/RawFunctionTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
namespace WikibaseSolutions\CypherDSL\Tests\Unit\Functions;
2323

24+
use TypeError;
2425
use PHPUnit\Framework\TestCase;
2526
use WikibaseSolutions\CypherDSL\Functions\RawFunction;
2627
use WikibaseSolutions\CypherDSL\Tests\Unit\TestHelper;
@@ -43,4 +44,12 @@ public function testToQuery()
4344

4445
$this->assertSame("foobar(a, b, c)", $raw->toQuery());
4546
}
46-
}
47+
48+
public function testRequiresAnyTypeParameters() {
49+
$a = new class() {};
50+
51+
$this->expectException( TypeError::class );
52+
53+
new RawFunction('foobar', [$a]);
54+
}
55+
}

tests/Unit/PropertyMapTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
namespace WikibaseSolutions\CypherDSL\Tests\Unit;
2323

24+
use TypeError;
2425
use PHPUnit\Framework\TestCase;
2526
use WikibaseSolutions\CypherDSL\Literals\StringLiteral;
2627
use WikibaseSolutions\CypherDSL\PropertyMap;
@@ -126,4 +127,12 @@ public function provideNestedPropertyMapsData()
126127
[['a' => new PropertyMap(['b' => $this->getQueryConvertableMock(AnyType::class, "'c'")]), 'b' => $this->getQueryConvertableMock(AnyType::class, "'d'")], "{a: {b: 'c'}, b: 'd'}"]
127128
];
128129
}
129-
}
130+
131+
public function testRequiresAnyTypeProperties() {
132+
$a = new class() {};
133+
134+
$this->expectException( TypeError::class );
135+
136+
new PropertyMap([$a]);
137+
}
138+
}

0 commit comments

Comments
 (0)