Skip to content

Commit 807523d

Browse files
committed
added has name trait test
1 parent 58109f5 commit 807523d

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

src/Traits/HasNameTrait.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
namespace WikibaseSolutions\CypherDSL\Traits;
44

5+
use LogicException;
56
use function bin2hex;
67
use function ceil;
78
use function openssl_random_pseudo_bytes;
89
use function substr;
910

1011
trait HasNameTrait
1112
{
13+
use ErrorTrait;
14+
1215
private string $name;
1316

1417
/**
@@ -28,6 +31,10 @@ public static function automaticVariableLength(): int
2831
*/
2932
public function getName(): string
3033
{
34+
if (!isset($this->name)) {
35+
throw new LogicException('Name is not yet configured. Please call `configureName` to setup a name.');
36+
}
37+
3138
return $this->name;
3239
}
3340

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
}

0 commit comments

Comments
 (0)