Skip to content

Commit 4cb70b7

Browse files
committed
greenbarred all tests
1 parent 8a84798 commit 4cb70b7

File tree

7 files changed

+88
-63
lines changed

7 files changed

+88
-63
lines changed

src/Parameter.php

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

2424
use WikibaseSolutions\CypherDSL\Traits\BooleanTypeTrait;
2525
use WikibaseSolutions\CypherDSL\Traits\DateTimeTrait;
26+
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
2627
use WikibaseSolutions\CypherDSL\Traits\EscapeTrait;
2728
use WikibaseSolutions\CypherDSL\Traits\HasNameTrait;
2829
use WikibaseSolutions\CypherDSL\Traits\ListTypeTrait;
@@ -75,11 +76,7 @@ class Parameter implements
7576
use StringTypeTrait;
7677
use TimeTrait;
7778
use HasNameTrait;
78-
79-
/**
80-
* @var string The parameter name
81-
*/
82-
private string $parameter;
79+
use ErrorTrait;
8380

8481
/**
8582
* Parameter constructor.
@@ -89,11 +86,11 @@ class Parameter implements
8986
*/
9087
public function __construct(?string $parameter = null)
9188
{
92-
$parameter ??= self::generateName('param');
89+
$parameter ??= $this->generateName('param');
9390

94-
self::validateName($parameter);
91+
self::assertValidName($parameter);
9592

96-
$this->parameter = $parameter;
93+
$this->name = $parameter;
9794
}
9895

9996
/**
@@ -103,14 +100,14 @@ public function __construct(?string $parameter = null)
103100
*/
104101
public function getParameter(): string
105102
{
106-
return $this->parameter;
103+
return $this->getName();
107104
}
108105

109106
/**
110107
* @inheritDoc
111108
*/
112109
public function toQuery(): string
113110
{
114-
return sprintf('$%s', $this->parameter);
111+
return sprintf('$%s', $this->getName());
115112
}
116113
}

src/Traits/ErrorTrait.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ private static function assertValidName(string $name): void
170170
throw new InvalidArgumentException("A name cannot be an empty string");
171171
}
172172

173-
if (!ctype_alnum($name)) {
174-
throw new InvalidArgumentException('A name can only contain alphanumeric characters');
173+
if (!ctype_alnum(str_replace('_', '', $name))) {
174+
throw new InvalidArgumentException('A name can only contain alphanumeric characters and underscores');
175175
}
176176

177177
if (is_numeric($name[0])) {
@@ -191,11 +191,22 @@ private static function assertValidName(string $name): void
191191
*/
192192
private static function typeError(string $varName, $classNames, $userInput): TypeError
193193
{
194-
return new TypeError(sprintf(
194+
return new TypeError(self::getTypeErrorText($varName, $classNames, $userInput));
195+
}
196+
197+
/**
198+
* @param string $varName
199+
* @param $classNames
200+
* @param $userInput
201+
* @return string
202+
*/
203+
private static function getTypeErrorText(string $varName, $classNames, $userInput): string
204+
{
205+
return sprintf(
195206
'$%s should be a %s object, %s given.',
196207
$varName,
197208
implode(' or ', $classNames),
198209
self::getDebugType($userInput)
199-
));
210+
);
200211
}
201212
}

src/Traits/HasNameTrait.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,23 @@
99

1010
trait HasNameTrait
1111
{
12+
private string $name;
13+
1214
public static function automaticVariableLength(): int
1315
{
1416
return 32;
1517
}
1618

19+
/**
20+
* Returns the name.
21+
*
22+
* @return string
23+
*/
24+
public function getName(): string
25+
{
26+
return $this->name;
27+
}
28+
1729
/**
1830
* Generates a unique random identifier.
1931
*
@@ -26,7 +38,7 @@ public static function automaticVariableLength(): int
2638
*
2739
* @return string
2840
*/
29-
private static function generateName(string $prefix = 'var', int $length = null): string
41+
private function generateName(string $prefix = 'var', int $length = null): string
3042
{
3143
$length ??= self::automaticVariableLength();
3244

src/Variable.php

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,14 @@
2424
use WikibaseSolutions\CypherDSL\Traits\BooleanTypeTrait;
2525
use WikibaseSolutions\CypherDSL\Traits\DateTimeTrait;
2626
use WikibaseSolutions\CypherDSL\Traits\DateTrait;
27+
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
2728
use WikibaseSolutions\CypherDSL\Traits\EscapeTrait;
2829
use WikibaseSolutions\CypherDSL\Traits\HasNameTrait;
29-
use WikibaseSolutions\CypherDSL\Traits\HasPropertiesTrait;
3030
use WikibaseSolutions\CypherDSL\Traits\ListTypeTrait;
3131
use WikibaseSolutions\CypherDSL\Traits\LocalDateTimeTrait;
3232
use WikibaseSolutions\CypherDSL\Traits\LocalTimeTrait;
3333
use WikibaseSolutions\CypherDSL\Traits\MapTypeTrait;
3434
use WikibaseSolutions\CypherDSL\Traits\NumeralTypeTrait;
35-
use WikibaseSolutions\CypherDSL\Traits\PropertyTypeTrait;
36-
use WikibaseSolutions\CypherDSL\Traits\RelationshipTrait;
3735
use WikibaseSolutions\CypherDSL\Traits\PointTrait;
3836
use WikibaseSolutions\CypherDSL\Traits\StringTypeTrait;
3937
use WikibaseSolutions\CypherDSL\Traits\StructuralTypeTrait;
@@ -87,11 +85,7 @@ class Variable implements
8785
use TimeTrait;
8886
use HasNameTrait;
8987
use MapTypeTrait;
90-
91-
/**
92-
* @var string The variable
93-
*/
94-
private string $variable;
88+
use ErrorTrait;
9589

9690
/**
9791
* Variable constructor.
@@ -100,13 +94,11 @@ class Variable implements
10094
*/
10195
public function __construct(?string $variable = null)
10296
{
103-
$variable ??= self::generateName('var');
104-
105-
$variable = trim($variable);
97+
$variable ??= $this->generateName('var');
10698

107-
self::validateName($variable);
99+
self::assertValidName($variable);
108100

109-
$this->variable = $variable;
101+
$this->name = $variable;
110102
}
111103

112104
/**
@@ -143,21 +135,16 @@ public function assign(AnyType $value): Assignment
143135
return new Assignment($this, $value);
144136
}
145137

146-
public function getName(): string
147-
{
148-
return $this->variable;
149-
}
150-
151138
public function getVariable(): string
152139
{
153-
return $this->variable;
140+
return $this->getName();
154141
}
155142

156143
/**
157144
* @inheritDoc
158145
*/
159146
public function toQuery(): string
160147
{
161-
return $this->escape($this->variable);
148+
return self::escape($this->getName());
162149
}
163150
}

tests/Unit/ParameterTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ public function provideToQueryData(): array
5959
return [
6060
["a", '$a'],
6161
["b", '$b'],
62-
["0", '$0'],
63-
["_", '$_'],
6462
["foo_bar", '$foo_bar'],
6563
["A", '$A']
6664
];
@@ -72,7 +70,8 @@ public function provideThrowsExceptionOnInvalidData(): array
7270
[""],
7371
["@"],
7472
["!"],
75-
["-"]
73+
["-"],
74+
['']
7675
];
7776
}
7877
}

tests/Unit/Traits/ErrorTraitTest.php

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,34 @@
2424
use TypeError;
2525
use PHPUnit\Framework\TestCase;
2626
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
27+
use WikibaseSolutions\CypherDSL\Tests\Unit\Traits\ErrorHelperDummyA;
2728

2829
/**
2930
* Dummy classes
3031
*/
31-
class ErrorHelperDummyA {};
32-
class ErrorHelperDummyB {};
33-
class ErrorHelperDummyExtendsA extends ErrorHelperDummyA {};
34-
class ErrorHelperDummyExtendsB extends ErrorHelperDummyB {};
32+
class ErrorHelperDummyA
33+
{
34+
}
35+
36+
;
37+
38+
class ErrorHelperDummyB
39+
{
40+
}
41+
42+
;
43+
44+
class ErrorHelperDummyExtendsA extends ErrorHelperDummyA
45+
{
46+
}
47+
48+
;
49+
50+
class ErrorHelperDummyExtendsB extends ErrorHelperDummyB
51+
{
52+
}
53+
54+
;
3555

3656
/**
3757
* Tester/Mock class
@@ -65,21 +85,21 @@ public function setUp(): void
6585
* @doesNotPerformAssertions
6686
* @dataProvider CorrectAssertionsProvider
6787
*/
68-
public function testAssertClass($classNames, $userInput)
88+
public function testAssertClass($classNames, $userInput): void
6989
{
7090
$this->errorImpl->call('assertClass', ['foo', $classNames, $userInput]);
7191
}
7292

7393
/**
7494
* @dataProvider failingAssertionsProvider
7595
*/
76-
public function testAssertClassFailure($classNames, $userInput)
96+
public function testAssertClassFailure($classNames, $userInput): void
7797
{
7898
$this->expectException(TypeError::class);
7999
$this->errorImpl->call('assertClass', ['foo', $classNames, $userInput]);
80100
}
81101

82-
public function correctAssertionsProvider()
102+
public function correctAssertionsProvider(): array
83103
{
84104
return [
85105
[ErrorHelperDummyA::class, new ErrorHelperDummyA()],
@@ -89,7 +109,7 @@ public function correctAssertionsProvider()
89109
];
90110
}
91111

92-
public function failingAssertionsProvider()
112+
public function failingAssertionsProvider(): array
93113
{
94114
return [
95115
[ErrorHelperDummyA::class, new ErrorHelperDummyB()],
@@ -98,41 +118,42 @@ public function failingAssertionsProvider()
98118
];
99119
}
100120

101-
public function testGetTypeErrorText()
121+
public function testGetTypeErrorText(): void
102122
{
103123
$this->assertEquals(
104-
'$foo should be a WikibaseSolutions\CypherDSL\Tests\Unit\Traits\ErrorHelperDummyA object, integer "5" given.',
124+
'$foo should be a WikibaseSolutions\CypherDSL\Tests\Unit\Traits\ErrorHelperDummyA object, int given.',
105125
$this->errorImpl->call('getTypeErrorText', ['foo', [ErrorHelperDummyA::class], 5])
106126
);
107127
$this->assertEquals(
108128
'$foo should be a ' .
109129
'WikibaseSolutions\CypherDSL\Tests\Unit\Traits\ErrorHelperDummyA or ' .
110-
'WikibaseSolutions\CypherDSL\Tests\Unit\Traits\ErrorHelperDummyB object, integer "5" given.',
130+
'WikibaseSolutions\CypherDSL\Tests\Unit\Traits\ErrorHelperDummyB object, int given.',
111131
$this->errorImpl->call('getTypeErrorText', ['foo', [ErrorHelperDummyA::class, ErrorHelperDummyB::class], 5])
112132
);
113133
}
114134

115135
/**
116136
* @dataProvider getUserInputInfoProvider
117137
*/
118-
public function testGetUserInputInfo($expected, $input)
138+
public function testGetUserInputInfo($expected, $input): void
119139
{
120140
$this->assertEquals(
121141
$expected,
122-
$this->errorImpl->call('getUserInputInfo', [$input])
142+
$this->errorImpl->call('getDebugType', [$input])
123143
);
124144
}
125145

126-
public function getUserInputInfoProvider()
146+
public function getUserInputInfoProvider(): array
127147
{
128148
return [
129-
[ 'string "foo"', 'foo' ],
130-
[ 'integer "5"', 5 ],
131-
[ 'double "3.14"', 3.14 ],
132-
[ 'boolean "1"', true ],
133-
[ 'array', ['foo', 'bar'] ],
134-
[ 'anonymous class instance', new class () {} ],
135-
[ 'WikibaseSolutions\CypherDSL\Tests\Unit\Traits\ErrorHelperDummyA', new ErrorHelperDummyA()]
149+
['string', 'foo'],
150+
['int', 5],
151+
['float', 3.14],
152+
['bool', true],
153+
['array', ['foo', 'bar']],
154+
['class@anonymous', new class () {
155+
}],
156+
[ErrorHelperDummyA::class, new ErrorHelperDummyA()]
136157
];
137158
}
138159
}

tests/Unit/VariableTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,15 @@ public function provideToQueryData(): array
7171
{
7272
return [
7373
["a", "a"],
74-
["b", "b"],
75-
["0", "`0`"]
74+
["b", "b"]
7675
];
7776
}
7877

7978
public function providePropertyData(): array
8079
{
8180
return [
8281
["a", "a", new Property(new Variable("a"), "a")],
83-
["a", "b", new Property(new Variable("a"), "b")],
84-
["0", "0", new Property(new Variable("0"), "0")]
82+
["a", "b", new Property(new Variable("a"), "b")]
8583
];
8684
}
8785
}

0 commit comments

Comments
 (0)