File tree Expand file tree Collapse file tree 3 files changed +91
-0
lines changed Expand file tree Collapse file tree 3 files changed +91
-0
lines changed Original file line number Diff line number Diff line change 2
2
3
3
namespace WikibaseSolutions \CypherDSL \Traits ;
4
4
5
+ use LogicException ;
5
6
use function bin2hex ;
6
7
use function ceil ;
7
8
use function openssl_random_pseudo_bytes ;
8
9
use function substr ;
9
10
10
11
trait HasNameTrait
11
12
{
13
+ use ErrorTrait;
14
+
12
15
private string $ name ;
13
16
14
17
/**
@@ -28,6 +31,10 @@ public static function automaticVariableLength(): int
28
31
*/
29
32
public function getName (): string
30
33
{
34
+ if (!isset ($ this ->name )) {
35
+ throw new LogicException ('Name is not yet configured. Please call `configureName` to setup a name. ' );
36
+ }
37
+
31
38
return $ this ->name ;
32
39
}
33
40
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace WikibaseSolutions \CypherDSL \Tests \Unit \Traits ;
4
+
5
+ use LogicException ;
6
+ use PHPUnit \Framework \TestCase ;
7
+ use WikibaseSolutions \CypherDSL \Traits \HasNameTrait ;
8
+
9
+ class HasNameTraitTest extends TestCase
10
+ {
11
+ private $ hasName ;
12
+
13
+ public function setUp (): void
14
+ {
15
+ $ this ->hasName = new class {
16
+ use HasNameTrait {
17
+ configureName as public ;
18
+ generateName as public ;
19
+ }
20
+ };
21
+ }
22
+
23
+ public function testHasName (): void
24
+ {
25
+ $ this ->expectException (LogicException::class);
26
+
27
+ $ this ->hasName ->getName ();
28
+ }
29
+
30
+ public function testGenerateName (): void
31
+ {
32
+ $ this ->assertMatchesRegularExpression ('/var\w{32}/ ' , $ this ->hasName ->generateName ());
33
+ $ this ->assertMatchesRegularExpression ('/x\w{16}/ ' , $ this ->hasName ->generateName ('x ' , 16 ));
34
+ }
35
+
36
+ public function testConfigureName (): void
37
+ {
38
+ $ this ->hasName ->configureName (null , 'y ' , 16 );
39
+
40
+ $ this ->assertMatchesRegularExpression ('/y\w{16}/ ' , $ this ->hasName ->getName ());
41
+ }
42
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace WikibaseSolutions \CypherDSL \Tests \Unit \Traits ;
4
+
5
+ use PHPUnit \Framework \TestCase ;
6
+ use WikibaseSolutions \CypherDSL \Traits \HasVariableTrait ;
7
+
8
+ class HasVariableTraitTest extends TestCase
9
+ {
10
+ private $ hasVariable ;
11
+
12
+ public function setUp (): void
13
+ {
14
+ $ this ->hasVariable = new class {
15
+ use HasVariableTrait;
16
+ };
17
+ }
18
+
19
+ public function testDefaultGeneration (): void
20
+ {
21
+ self ::assertNull ($ this ->hasVariable ->getVariable ());
22
+ self ::assertNotNull ($ this ->hasVariable ->getName ());
23
+
24
+ self ::assertMatchesRegularExpression ('/var\w{32}/ ' , $ this ->hasVariable ->getVariable ()->getName ());
25
+ }
26
+
27
+ public function testNamed (): void
28
+ {
29
+ $ this ->hasVariable ->named ('x ' );
30
+
31
+ self ::assertSame ($ this ->hasVariable ->getVariable (), $ this ->hasVariable ->getName ());
32
+ self ::assertEquals ('x ' , $ this ->hasVariable ->getVariable ()->getName ());
33
+ }
34
+
35
+ public function testSetName (): void
36
+ {
37
+ $ this ->hasVariable ->setName ('x ' );
38
+
39
+ self ::assertSame ($ this ->hasVariable ->getVariable (), $ this ->hasVariable ->getName ());
40
+ self ::assertEquals ('x ' , $ this ->hasVariable ->getVariable ()->getName ());
41
+ }
42
+ }
You can’t perform that action at this time.
0 commit comments