-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added more parameterhelper and cypherformatter tests
- Loading branch information
1 parent
fece01b
commit b497051
Showing
4 changed files
with
220 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Laudis Neo4j package. | ||
* | ||
* (c) Laudis technologies <http://laudis.tech> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Laudis\Neo4j\Tests\Unit; | ||
|
||
use Bolt\structures\Node; | ||
use Bolt\structures\Path; | ||
use Bolt\structures\UnboundRelationship; | ||
use Laudis\Neo4j\Formatter\BoltCypherFormatter; | ||
use PHPUnit\Framework\TestCase; | ||
use stdClass; | ||
use UnexpectedValueException; | ||
|
||
final class BoltCypherFormatterTest extends TestCase | ||
{ | ||
private BoltCypherFormatter $formatter; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->formatter = new BoltCypherFormatter(); | ||
} | ||
|
||
public function testFormatPath(): void | ||
{ | ||
$path = new Path( | ||
[new Node(1, ['a'], ['a' => 1]), new Node(2, ['b'], ['a' => 2]), new Node(3, ['c'], ['a' => 3])], | ||
[new UnboundRelationship(4, 'a', ['d']), new UnboundRelationship(5, 'a', ['e'])], | ||
[1, 2, 3, 4, 5] | ||
); | ||
|
||
$results = [ | ||
[ | ||
$path, | ||
], | ||
[ | ||
], | ||
]; | ||
$result = $this->formatter->formatResult(['fields' => ['a']], $results); | ||
|
||
self::assertEquals(1, $result->count()); | ||
self::assertEquals(1, $result->first()->count()); | ||
self::assertEquals([ | ||
['a' => 1], | ||
[0 => 'd'], | ||
['a' => 2], | ||
[0 => 'e'], | ||
['a' => 3], | ||
], $result->first()->get('a', [])); | ||
} | ||
|
||
public function testInvalidObject(): void | ||
{ | ||
$this->expectException(UnexpectedValueException::class); | ||
$this->expectExceptionMessage('Cannot handle objects without a properties method. Class given: '.stdClass::class); | ||
$this->formatter->formatResult(['fields' => ['a']], [[new stdClass()], []]); | ||
} | ||
|
||
public function testResource(): void | ||
{ | ||
$resource = fopen('php://temp', 'b'); | ||
$this->expectException(UnexpectedValueException::class); | ||
$this->expectExceptionMessage('Did not expect to receive value of type: resource'); | ||
$this->formatter->formatResult(['fields' => ['a']], [[$resource], []]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Laudis Neo4j package. | ||
* | ||
* (c) Laudis technologies <http://laudis.tech> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Laudis\Neo4j\Tests\Unit; | ||
|
||
use Ds\Map; | ||
use Ds\Vector; | ||
use InvalidArgumentException; | ||
use Iterator; | ||
use Laudis\Neo4j\ParameterHelper; | ||
use PHPUnit\Framework\TestCase; | ||
use stdClass; | ||
|
||
final class ParameterHelperTest extends TestCase | ||
{ | ||
/** @var iterable<iterable|scalar|null> */ | ||
private static iterable $invalidIterable; | ||
|
||
public static function setUpBeforeClass(): void | ||
{ | ||
parent::setUpBeforeClass(); | ||
self::$invalidIterable = new class() implements Iterator { | ||
private bool $initial = true; | ||
|
||
public function current(): int | ||
{ | ||
return 1; | ||
} | ||
|
||
public function next(): void | ||
{ | ||
$this->initial = false; | ||
} | ||
|
||
public function key(): stdClass | ||
{ | ||
return new stdClass(); | ||
} | ||
|
||
public function valid(): bool | ||
{ | ||
return $this->initial; | ||
} | ||
|
||
public function rewind(): void | ||
{ | ||
$this->initial = true; | ||
} | ||
}; | ||
} | ||
|
||
public function testAsList(): void | ||
{ | ||
self::assertEquals([1, 2, 3], ParameterHelper::asList([2 => 1, 'a' => 2, 'd' => 3])->toArray()); | ||
} | ||
|
||
public function testAsMap(): void | ||
{ | ||
self::assertEquals([2 => 1, 'a' => 2, 'd' => 3], ParameterHelper::asMap([2 => 1, 'a' => 2, 'd' => 3])->toArray()); | ||
} | ||
|
||
public function testFormatParameterString(): void | ||
{ | ||
self::assertEquals(['a' => 'b', 'c' => 'd'], ParameterHelper::formatParameters([ | ||
'a' => 'b', | ||
'c' => 'd', | ||
])->toArray()); | ||
} | ||
|
||
public function testFormatParameterInteger(): void | ||
{ | ||
self::assertEquals([2 => 'b', 3 => 'd'], ParameterHelper::formatParameters([ | ||
2 => 'b', | ||
3 => 'd', | ||
])->toArray()); | ||
} | ||
|
||
public function testFormatParameterVector(): void | ||
{ | ||
self::assertEquals(['b', 'd'], ParameterHelper::formatParameters([ | ||
'b', | ||
'd', | ||
])->toArray()); | ||
} | ||
|
||
public function testFormatParameterIterable(): void | ||
{ | ||
self::assertEquals([[1, 2]], ParameterHelper::formatParameters([ | ||
[1, 2], | ||
])->toArray()); | ||
} | ||
|
||
public function testFormatParameterInvalidIterable(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
ParameterHelper::formatParameters(self::$invalidIterable); | ||
} | ||
|
||
public function testFormatParameterInvalidIterable2(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
ParameterHelper::formatParameters([ | ||
'a' => [ | ||
self::$invalidIterable, | ||
], | ||
]); | ||
} | ||
|
||
public function testAsParmeterEmptyVector(): void | ||
{ | ||
$result = ParameterHelper::asParameter(new Vector()); | ||
self::assertIsArray($result); | ||
self::assertCount(0, $result); | ||
} | ||
|
||
public function testAsParmeterEmptyMap(): void | ||
{ | ||
$result = ParameterHelper::asParameter(new Map()); | ||
self::assertInstanceOf(stdClass::class, $result); | ||
} | ||
|
||
public function testAsParmeterEmptyArray(): void | ||
{ | ||
$result = ParameterHelper::asParameter([]); | ||
self::assertInstanceOf(stdClass::class, $result); | ||
} | ||
} |