|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Neo4j\Neo4jBundle\Tests\Unit; |
| 6 | + |
| 7 | +use Laudis\Neo4j\Contracts\DriverInterface; |
| 8 | +use Laudis\Neo4j\Databags\ServerInfo; |
| 9 | +use Laudis\Neo4j\Databags\SessionConfiguration; |
| 10 | +use Neo4j\Neo4jBundle\Decorators\SymfonyDriver; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | +use ReflectionClass; |
| 13 | +use ReflectionMethod; |
| 14 | +use ReflectionNamedType; |
| 15 | + |
| 16 | +final class SymfonyDriverTest extends TestCase |
| 17 | +{ |
| 18 | + private ReflectionClass $symfonyDriverReflection; |
| 19 | + private ReflectionClass $driverInterfaceReflection; |
| 20 | + |
| 21 | + #[\Override] |
| 22 | + protected function setUp(): void |
| 23 | + { |
| 24 | + $this->symfonyDriverReflection = new ReflectionClass(SymfonyDriver::class); |
| 25 | + $this->driverInterfaceReflection = new ReflectionClass(DriverInterface::class); |
| 26 | + } |
| 27 | + |
| 28 | + public function testImplementsDriverInterface(): void |
| 29 | + { |
| 30 | + $this->assertTrue( |
| 31 | + $this->symfonyDriverReflection->implementsInterface(DriverInterface::class), |
| 32 | + 'SymfonyDriver must implement DriverInterface' |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + public function testHasGetServerInfoMethod(): void |
| 37 | + { |
| 38 | + $this->assertTrue( |
| 39 | + $this->symfonyDriverReflection->hasMethod('getServerInfo'), |
| 40 | + 'SymfonyDriver must have getServerInfo method' |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + public function testGetServerInfoMethodSignature(): void |
| 45 | + { |
| 46 | + $method = $this->symfonyDriverReflection->getMethod('getServerInfo'); |
| 47 | + |
| 48 | + $this->assertMethodSignatureMatchesInterface($method, 'getServerInfo'); |
| 49 | + } |
| 50 | + |
| 51 | + public function testVerifyConnectivityMethodSignature(): void |
| 52 | + { |
| 53 | + $method = $this->symfonyDriverReflection->getMethod('verifyConnectivity'); |
| 54 | + |
| 55 | + $this->assertMethodSignatureMatchesInterface($method, 'verifyConnectivity'); |
| 56 | + } |
| 57 | + |
| 58 | + public function testCloseConnectionsMethodSignature(): void |
| 59 | + { |
| 60 | + $method = $this->symfonyDriverReflection->getMethod('closeConnections'); |
| 61 | + |
| 62 | + $this->assertMethodSignatureMatchesInterface($method, 'closeConnections'); |
| 63 | + } |
| 64 | + |
| 65 | + public function testCreateSessionMethodSignature(): void |
| 66 | + { |
| 67 | + $method = $this->symfonyDriverReflection->getMethod('createSession'); |
| 68 | + |
| 69 | + $interfaceMethod = $this->driverInterfaceReflection->getMethod('createSession'); |
| 70 | + |
| 71 | + $this->assertSame( |
| 72 | + $interfaceMethod->getNumberOfParameters(), |
| 73 | + $method->getNumberOfParameters(), |
| 74 | + 'createSession parameter count must match interface' |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + public function testGetServerInfoReturnType(): void |
| 79 | + { |
| 80 | + $method = $this->symfonyDriverReflection->getMethod('getServerInfo'); |
| 81 | + $returnType = $method->getReturnType(); |
| 82 | + |
| 83 | + $this->assertInstanceOf(ReflectionNamedType::class, $returnType); |
| 84 | + $this->assertSame(ServerInfo::class, $returnType->getName()); |
| 85 | + } |
| 86 | + |
| 87 | + public function testGetServerInfoParameterType(): void |
| 88 | + { |
| 89 | + $method = $this->symfonyDriverReflection->getMethod('getServerInfo'); |
| 90 | + $parameters = $method->getParameters(); |
| 91 | + |
| 92 | + $this->assertCount(1, $parameters); |
| 93 | + $this->assertSame('config', $parameters[0]->getName()); |
| 94 | + $this->assertTrue($parameters[0]->allowsNull()); |
| 95 | + |
| 96 | + $paramType = $parameters[0]->getType(); |
| 97 | + $this->assertInstanceOf(ReflectionNamedType::class, $paramType); |
| 98 | + $this->assertSame(SessionConfiguration::class, $paramType->getName()); |
| 99 | + } |
| 100 | + |
| 101 | + public function testAllInterfaceMethodsAreImplemented(): void |
| 102 | + { |
| 103 | + $interfaceMethods = $this->driverInterfaceReflection->getMethods(); |
| 104 | + |
| 105 | + foreach ($interfaceMethods as $interfaceMethod) { |
| 106 | + $methodName = $interfaceMethod->getName(); |
| 107 | + |
| 108 | + $this->assertTrue( |
| 109 | + $this->symfonyDriverReflection->hasMethod($methodName), |
| 110 | + "SymfonyDriver must implement method: {$methodName}" |
| 111 | + ); |
| 112 | + |
| 113 | + $implementedMethod = $this->symfonyDriverReflection->getMethod($methodName); |
| 114 | + |
| 115 | + $this->assertFalse( |
| 116 | + $implementedMethod->isAbstract(), |
| 117 | + "Method {$methodName} must not be abstract" |
| 118 | + ); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private function assertMethodSignatureMatchesInterface(ReflectionMethod $method, string $methodName): void |
| 123 | + { |
| 124 | + $interfaceMethod = $this->driverInterfaceReflection->getMethod($methodName); |
| 125 | + |
| 126 | + $this->assertSame( |
| 127 | + $interfaceMethod->getNumberOfParameters(), |
| 128 | + $method->getNumberOfParameters(), |
| 129 | + "{$methodName} parameter count must match interface" |
| 130 | + ); |
| 131 | + |
| 132 | + $interfaceParams = $interfaceMethod->getParameters(); |
| 133 | + $implParams = $method->getParameters(); |
| 134 | + |
| 135 | + foreach ($interfaceParams as $i => $interfaceParam) { |
| 136 | + $implParam = $implParams[$i]; |
| 137 | + |
| 138 | + $this->assertSame( |
| 139 | + $interfaceParam->getName(), |
| 140 | + $implParam->getName(), |
| 141 | + "{$methodName} parameter {$i} name must match interface" |
| 142 | + ); |
| 143 | + |
| 144 | + $this->assertSame( |
| 145 | + $interfaceParam->allowsNull(), |
| 146 | + $implParam->allowsNull(), |
| 147 | + "{$methodName} parameter {$i} nullability must match interface" |
| 148 | + ); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments