From c2b6a583f6ef896c813094783b18b27a96729518 Mon Sep 17 00:00:00 2001 From: Zan Baldwin Date: Fri, 7 Jun 2024 11:56:33 +0200 Subject: [PATCH] Remove Doctrine Functionality from Library --- README.md | 6 +- composer.json | 7 +- docs/08-doctrine.md | 5 + src/Doctrine/AbstractType.php | 151 ------------------------- src/Doctrine/IPv4Type.php | 29 ----- src/Doctrine/IPv6Type.php | 27 ----- src/Doctrine/MultiType.php | 27 ----- tests/Doctrine/IPv4TypeTest.php | 183 ------------------------------- tests/Doctrine/IPv6TypeTest.php | 183 ------------------------------- tests/Doctrine/MultiTypeTest.php | 183 ------------------------------- tests/Doctrine/TestPlatform.php | 13 --- 11 files changed, 8 insertions(+), 806 deletions(-) delete mode 100644 src/Doctrine/AbstractType.php delete mode 100644 src/Doctrine/IPv4Type.php delete mode 100644 src/Doctrine/IPv6Type.php delete mode 100644 src/Doctrine/MultiType.php delete mode 100644 tests/Doctrine/IPv4TypeTest.php delete mode 100644 tests/Doctrine/IPv6TypeTest.php delete mode 100644 tests/Doctrine/MultiTypeTest.php delete mode 100644 tests/Doctrine/TestPlatform.php diff --git a/README.md b/README.md index 2ec9d30..01e23e5 100644 --- a/README.md +++ b/README.md @@ -18,10 +18,8 @@ This library has extensive test coverage using PHPUnit on PHP versions: `5.6`, Static analysis is performed with PHPStan at `max` level on PHP `8.3`, using core, bleeding edge, and deprecation rules. -The Doctrine features included in this library are compatible with Doctrine DBAL -`^2.3 || ^3.0`. This is **not** enforced via Composer (in case you want to use -this library in a project that uses Doctrine DBAL `4+` without using the -Doctrine features this library provides). +> The Doctrine features for this library have been split off into their own +> package, [`darsyn/ip-doctrine`](https://packagist.org/packages/darsyn/ip-doctrine). ## Brief Example diff --git a/composer.json b/composer.json index d3282c8..8ca380a 100644 --- a/composer.json +++ b/composer.json @@ -28,18 +28,13 @@ } }, "require-dev": { - "ext-pdo": "*", - "doctrine/dbal": "^2.3 || ^3", "phpunit/phpunit": "*" }, - "conflict": { - "doctrine/dbal": ">=4" - }, "prefer-stable": true, "config": { "sort-packages": true }, "suggest": { - "doctrine/dbal": "to use IP as a column type" + "darsyn/ip-doctrine": "to use IP as a Doctrine column type" } } diff --git a/docs/08-doctrine.md b/docs/08-doctrine.md index b71b55f..0ad99df 100644 --- a/docs/08-doctrine.md +++ b/docs/08-doctrine.md @@ -1,5 +1,10 @@ # Doctrine Support +> This documentation is for the package `darsyn/ip-doctrine`, which was split +> into a separate package in v5+ so that PHP version requirements could be +> updated independently. Require that as a Composer dependency to use this +> functionality. + This library can be used to support IP address as column types with Doctrine DBAL versions `^2.3 || ^3.0`. diff --git a/src/Doctrine/AbstractType.php b/src/Doctrine/AbstractType.php deleted file mode 100644 index 90b89e9..0000000 --- a/src/Doctrine/AbstractType.php +++ /dev/null @@ -1,151 +0,0 @@ -getBinaryTypeDeclarationSQL(['length' => static::IP_LENGTH]); - } - - /** - * {@inheritdoc} - * @throws \Doctrine\DBAL\Types\ConversionException - * @return \Darsyn\IP\IpInterface|null - */ - public function convertToPHPValue($value, AbstractPlatform $platform) - { - /** @var string|resource|\Darsyn\IP\IpInterface|null $value */ - // PostgreSQL will return the binary data as a resource instead of a string (like MySQL). - if (\is_resource($value)) { - if (\get_resource_type($value) !== 'stream' || false === $value = \stream_get_contents($value)) { - throw new ConversionException(sprintf( - 'Could not convert database value to Doctrine Type "%s" (could not convert non-stream resource to a string).', - self::NAME - )); - } - } - /** @var string|\Darsyn\IP\IpInterface|null $value */ - if (empty($value)) { - return null; - } - if (\is_object($value) && \is_a($value, $this->getIpClass(), false)) { - /** @var \Darsyn\IP\IpInterface $value */ - return $value; - } - try { - return $this->createIpObject($value); - } catch (IpException $e) { - throw ConversionException::conversionFailed($value, static::NAME); - } - } - - /** - * {@inheritdoc} - * @throws \Doctrine\DBAL\Types\ConversionException - * @return string|null - */ - public function convertToDatabaseValue($value, AbstractPlatform $platform) - { - if (empty($value)) { - return null; - } - if (\is_string($value)) { - try { - $value = $this->createIpObject($value); - } catch (IpException $e) { - throw new ConversionException(sprintf( - 'Could not convert PHP value "%s" to valid IP address ready for database insertion.', - (string) $value - ), 0, $e); - } - } - - if (!\is_object($value)) { - throw new ConversionException(sprintf( - 'Could not convert PHP value of type "%s" to valid IP address ready for database insertion.', - gettype($value) - )); - } - - if (!\is_a($value, IpInterface::class, false)) { - throw new ConversionException(sprintf( - 'Could not convert PHP object "%s" to a valid IP instance ready for database insertion.', - \get_class($value) - )); - } - - if (static::IP_LENGTH !== $valueLength = MbString::getLength($value->getBinary())) { - throw new ConversionException(sprintf( - 'Cannot fit IPv%d address (%d bytes) into database column (%d bytes). Reconfigure Doctrine types to use a different IP class.', - $value->getVersion(), - $valueLength, - static::IP_LENGTH - )); - } - - return $value->getBinary(); - } - - /** - * {@inheritdoc} - * @return string - */ - public function getName() - { - return self::NAME; - } - - /** - * {@inheritdoc} - * @return int - */ - public function getBindingType() - { - return \PDO::PARAM_LOB; - } - - /** - * {@inheritdoc} - * @return bool - */ - public function requiresSQLCommentHint(AbstractPlatform $platform) - { - return true; - } -} diff --git a/src/Doctrine/IPv4Type.php b/src/Doctrine/IPv4Type.php deleted file mode 100644 index a93b121..0000000 --- a/src/Doctrine/IPv4Type.php +++ /dev/null @@ -1,29 +0,0 @@ -markTestSkipped('Skipping test that requires "doctrine/dbal".'); - } - - $this->platform = new TestPlatform; - $type = Type::getType('ipv4'); - $this->assertInstanceOf(IPv4Type::class, $type); - $this->type = $type; - } - - /** - * @test - * @return void - */ - #[PHPUnit\Test] - public function testIpConvertsToDatabaseValue() - { - $ip = IP::factory('12.34.56.78'); - - $expected = $ip->getBinary(); - $actual = $this->type->convertToDatabaseValue($ip, $this->platform); - - $this->assertEquals($expected, $actual); - } - - /** - * @test - * @return void - */ - #[PHPUnit\Test] - public function testInvalidIpConversionForDatabaseValue() - { - $this->expectException(\Doctrine\DBAL\Types\ConversionException::class); - $this->type->convertToDatabaseValue('abcdefg', $this->platform); - } - - /** - * @test - * @return void - */ - #[PHPUnit\Test] - public function testNullConversionForDatabaseValue() - { - $this->assertNull($this->type->convertToDatabaseValue(null, $this->platform)); - } - - /** - * @test - * @return void - */ - #[PHPUnit\Test] - public function testIpConvertsToPHPValue() - { - $ip = IP::factory('12.34.56.78'); - /** @var IP $dbIp */ - $dbIp = $this->type->convertToPHPValue($ip->getBinary(), $this->platform); - $this->assertInstanceOf(IP::class, $dbIp); - $this->assertEquals('12.34.56.78', $dbIp->getDotAddress()); - } - - /** - * @test - * @return void - */ - #[PHPUnit\Test] - public function testIpObjectConvertsToPHPValue() - { - $ip = IP::factory('12.34.56.78'); - /** @var IP $dbIp */ - $dbIp = $this->type->convertToPHPValue($ip, $this->platform); - $this->assertInstanceOf(IP::class, $dbIp); - $this->assertSame($ip, $dbIp); - } - - /** - * @test - * @return void - */ - #[PHPUnit\Test] - public function testStreamConvertsToPHPValue() - { - $ip = IP::factory('12.34.56.78'); - $stream = fopen('php://memory','r+'); - // assertIsResource() isn't available for PHP 5.6 and 7.0 (PHPUnit < 7.0). - $this->assertTrue(is_resource($stream)); - fwrite($stream, $ip->getBinary()); - rewind($stream); - /** @var IP $dbIp */ - $dbIp = $this->type->convertToPHPValue($stream, $this->platform); - $this->assertInstanceOf(IP::class, $dbIp); - $this->assertEquals('12.34.56.78', $dbIp->getDotAddress()); - } - - /** - * @test - * @return void - */ - #[PHPUnit\Test] - public function testInvalidIpConversionForPHPValue() - { - $this->expectException(\Doctrine\DBAL\Types\ConversionException::class); - $this->type->convertToPHPValue('abcdefg', $this->platform); - } - - /** - * @test - * @return void - */ - #[PHPUnit\Test] - public function testNullConversionForPHPValue() - { - $this->assertNull($this->type->convertToPHPValue(null, $this->platform)); - } - - /** - * @test - * @return void - */ - #[PHPUnit\Test] - public function testGetBinaryTypeDeclarationSQL() - { - $this->assertEquals('DUMMYBINARY()', $this->type->getSQLDeclaration(['length' => 4], $this->platform)); - } - - /** - * @test - * @return void - */ - #[PHPUnit\Test] - public function testBindingTypeIsAValidPDOTypeConstant() - { - // Get all constants of the PDO class. - $constants = (new \ReflectionClass(PDO::class))->getConstants(); - // Now filter out any constants that don't begin with "PARAM_". - $paramConstants = array_intersect_key( - $constants, - array_flip(array_filter(array_keys($constants), function ($key) { - return strpos($key, 'PARAM_') === 0; - })) - ); - // Check that the return value of the Type's binding value is a valid - // PDO PARAM constant. - $this->assertContains($this->type->getBindingType(), $paramConstants); - } -} diff --git a/tests/Doctrine/IPv6TypeTest.php b/tests/Doctrine/IPv6TypeTest.php deleted file mode 100644 index 9c4f85b..0000000 --- a/tests/Doctrine/IPv6TypeTest.php +++ /dev/null @@ -1,183 +0,0 @@ -markTestSkipped('Skipping test that requires "doctrine/dbal".'); - } - - $this->platform = new TestPlatform; - $type = Type::getType('ipv6'); - $this->assertInstanceOf(IPv6Type::class, $type); - $this->type = $type; - } - - /** - * @test - * @return void - */ - #[PHPUnit\Test] - public function testIpConvertsToDatabaseValue() - { - $ip = IP::factory('::1'); - - $expected = $ip->getBinary(); - $actual = $this->type->convertToDatabaseValue($ip, $this->platform); - - $this->assertEquals($expected, $actual); - } - - /** - * @test - * @return void - */ - #[PHPUnit\Test] - public function testInvalidIpConversionForDatabaseValue() - { - $this->expectException(\Doctrine\DBAL\Types\ConversionException::class); - $this->type->convertToDatabaseValue('abcdefg', $this->platform); - } - - /** - * @test - * @return void - */ - #[PHPUnit\Test] - public function testNullConversionForDatabaseValue() - { - $this->assertNull($this->type->convertToDatabaseValue(null, $this->platform)); - } - - /** - * @test - * @return void - */ - #[PHPUnit\Test] - public function testIpConvertsToPHPValue() - { - $ip = IP::factory('::1'); - /** @var IP $dbIp */ - $dbIp = $this->type->convertToPHPValue($ip->getBinary(), $this->platform); - $this->assertInstanceOf(IP::class, $dbIp); - $this->assertEquals('::1', $dbIp->getCompactedAddress()); - } - - /** - * @test - * @return void - */ - #[PHPUnit\Test] - public function testIpObjectConvertsToPHPValue() - { - $ip = IP::factory('::1'); - /** @var IP $dbIp */ - $dbIp = $this->type->convertToPHPValue($ip, $this->platform); - $this->assertInstanceOf(IP::class, $dbIp); - $this->assertSame($ip, $dbIp); - } - - /** - * @test - * @return void - */ - #[PHPUnit\Test] - public function testStreamConvertsToPHPValue() - { - $ip = IP::factory('::1'); - $stream = fopen('php://memory','r+'); - // assertIsResource() isn't available for PHP 5.6 and 7.0 (PHPUnit < 7.0). - $this->assertTrue(is_resource($stream)); - fwrite($stream, $ip->getBinary()); - rewind($stream); - /** @var IP $dbIp */ - $dbIp = $this->type->convertToPHPValue($stream, $this->platform); - $this->assertInstanceOf(IP::class, $dbIp); - $this->assertEquals('::1', $dbIp->getCompactedAddress()); - } - - /** - * @test - * @return void - */ - #[PHPUnit\Test] - public function testInvalidIpConversionForPHPValue() - { - $this->expectException(\Doctrine\DBAL\Types\ConversionException::class); - $this->type->convertToPHPValue('abcdefg', $this->platform); - } - - /** - * @test - * @return void - */ - #[PHPUnit\Test] - public function testNullConversionForPHPValue() - { - $this->assertNull($this->type->convertToPHPValue(null, $this->platform)); - } - - /** - * @test - * @return void - */ - #[PHPUnit\Test] - public function testGetBinaryTypeDeclarationSQL() - { - $this->assertEquals('DUMMYBINARY()', $this->type->getSQLDeclaration(['length' => 16], $this->platform)); - } - - /** - * @test - * @return void - */ - #[PHPUnit\Test] - public function testBindingTypeIsAValidPDOTypeConstant() - { - // Get all constants of the PDO class. - $constants = (new \ReflectionClass(PDO::class))->getConstants(); - // Now filter out any constants that don't begin with "PARAM_". - $paramConstants = array_intersect_key( - $constants, - array_flip(array_filter(array_keys($constants), function ($key) { - return strpos($key, 'PARAM_') === 0; - })) - ); - // Check that the return value of the Type's binding value is a valid - // PDO PARAM constant. - $this->assertContains($this->type->getBindingType(), $paramConstants); - } -} diff --git a/tests/Doctrine/MultiTypeTest.php b/tests/Doctrine/MultiTypeTest.php deleted file mode 100644 index 81bf5c0..0000000 --- a/tests/Doctrine/MultiTypeTest.php +++ /dev/null @@ -1,183 +0,0 @@ -markTestSkipped('Skipping test that requires "doctrine/dbal".'); - } - - $this->platform = new TestPlatform; - $type = Type::getType('ip_multi'); - $this->assertInstanceOf(MultiType::class, $type); - $this->type = $type; - } - - /** - * @test - * @return void - */ - #[PHPUnit\Test] - public function testIpConvertsToDatabaseValue() - { - $ip = IP::factory('12.34.56.78'); - - $expected = $ip->getBinary(); - $actual = $this->type->convertToDatabaseValue($ip, $this->platform); - - $this->assertEquals($expected, $actual); - } - - /** - * @test - * @return void - */ - #[PHPUnit\Test] - public function testInvalidIpConversionForDatabaseValue() - { - $this->expectException(\Doctrine\DBAL\Types\ConversionException::class); - $this->type->convertToDatabaseValue('abcdefg', $this->platform); - } - - /** - * @test - * @return void - */ - #[PHPUnit\Test] - public function testNullConversionForDatabaseValue() - { - $this->assertNull($this->type->convertToDatabaseValue(null, $this->platform)); - } - - /** - * @test - * @return void - */ - #[PHPUnit\Test] - public function testIpConvertsToPHPValue() - { - $ip = IP::factory('12.34.56.78'); - /** @var IP $dbIp */ - $dbIp = $this->type->convertToPHPValue($ip->getBinary(), $this->platform); - $this->assertInstanceOf(IP::class, $dbIp); - $this->assertEquals('12.34.56.78', $dbIp->getDotAddress()); - } - - /** - * @test - * @return void - */ - #[PHPUnit\Test] - public function testIpObjectConvertsToPHPValue() - { - $ip = IP::factory('12.34.56.78'); - /** @var IP $dbIp */ - $dbIp = $this->type->convertToPHPValue($ip, $this->platform); - $this->assertInstanceOf(IP::class, $dbIp); - $this->assertSame($ip, $dbIp); - } - - /** - * @test - * @return void - */ - #[PHPUnit\Test] - public function testStreamConvertsToPHPValue() - { - $ip = IP::factory('12.34.56.78'); - $stream = fopen('php://memory','r+'); - // assertIsResource() isn't available for PHP 5.6 and 7.0 (PHPUnit < 7.0). - $this->assertTrue(is_resource($stream)); - fwrite($stream, $ip->getBinary()); - rewind($stream); - /** @var IP $dbIp */ - $dbIp = $this->type->convertToPHPValue($stream, $this->platform); - $this->assertInstanceOf(IP::class, $dbIp); - $this->assertEquals('12.34.56.78', $dbIp->getDotAddress()); - } - - /** - * @test - * @return void - */ - #[PHPUnit\Test] - public function testInvalidIpConversionForPHPValue() - { - $this->expectException(\Doctrine\DBAL\Types\ConversionException::class); - $this->type->convertToPHPValue('abcdefg', $this->platform); - } - - /** - * @test - * @return void - */ - #[PHPUnit\Test] - public function testNullConversionForPHPValue() - { - $this->assertNull($this->type->convertToPHPValue(null, $this->platform)); - } - - /** - * @test - * @return void - */ - #[PHPUnit\Test] - public function testGetBinaryTypeDeclarationSQL() - { - $this->assertEquals('DUMMYBINARY()', $this->type->getSQLDeclaration(['length' => 16], $this->platform)); - } - - /** - * @test - * @return void - */ - #[PHPUnit\Test] - public function testBindingTypeIsAValidPDOTypeConstant() - { - // Get all constants of the PDO class. - $constants = (new \ReflectionClass(PDO::class))->getConstants(); - // Now filter out any constants that don't begin with "PARAM_". - $paramConstants = array_intersect_key( - $constants, - array_flip(array_filter(array_keys($constants), function ($key) { - return strpos($key, 'PARAM_') === 0; - })) - ); - // Check that the return value of the Type's binding value is a valid - // PDO PARAM constant. - $this->assertContains($this->type->getBindingType(), $paramConstants); - } -} diff --git a/tests/Doctrine/TestPlatform.php b/tests/Doctrine/TestPlatform.php deleted file mode 100644 index 11da815..0000000 --- a/tests/Doctrine/TestPlatform.php +++ /dev/null @@ -1,13 +0,0 @@ -