Skip to content

Commit

Permalink
Add support for doctrine/dbal ^4
Browse files Browse the repository at this point in the history
  • Loading branch information
gndk committed Oct 20, 2024
1 parent befc168 commit 05f8697
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 74 deletions.
14 changes: 0 additions & 14 deletions .github/workflows/testing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ jobs:
strategy:
matrix:
php:
- '5.6'
- '7.0'
- '7.1'
- '7.2'
- '7.3'
- '7.4'
- '8.0'
- '8.1'
- '8.2'
- '8.3'
Expand All @@ -38,13 +31,6 @@ jobs:
strategy:
matrix:
php:
- '5.6'
- '7.0'
- '7.1'
- '7.2'
- '7.3'
- '7.4'
- '8.0'
- '8.1'
- '8.2'
- '8.3'
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ Please refer to the [`darsyn/ip`][ip] project for complete documentation.

## Compatibility

This library has extensive test coverage using PHPUnit on PHP versions: `5.6`,
`7.0`, `7.1`, `7.2`, `7.3`, `7.4`, `8.0`, `8.1`, `8.2`, and `8.3`.
This library has extensive test coverage using PHPUnit on PHP versions: `8.1`, `8.2`, and `8.3`.

Static analysis is performed with PHPStan at `max` level on PHP `8.3`, using
core, bleeding edge, and deprecation rules.

The Doctrine features are compatible with Doctrine DBAL `^2.3 || ^3.0`. Types
The Doctrine features are compatible with Doctrine DBAL `^4`. Types
are provided for `Ipv4`, `IPv6`, and `Multi` IP types from the parent
[`darsyn/ip`][ip] project.

Expand Down
7 changes: 2 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
}
},
"require": {
"php": ">=5.6",
"php": ">=8.1",
"ext-pdo": "*",
"darsyn/ip": "^5.0",
"doctrine/dbal": "^2.3 || ^3"
"doctrine/dbal": "^4"
},
"autoload-dev": {
"psr-4": {
Expand All @@ -33,9 +33,6 @@
"phpstan/phpstan-deprecation-rules": "^1.2",
"phpunit/phpunit": "*"
},
"conflict": {
"doctrine/dbal": ">=4"
},
"prefer-stable": true,
"config": {
"sort-packages": true
Expand Down
16 changes: 9 additions & 7 deletions src/AbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
use Darsyn\IP\Exception\IpException;
use Darsyn\IP\IpInterface;
use Darsyn\IP\Util\MbString;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Exception\ValueNotConvertible;
use Doctrine\DBAL\Types\Type;

/**
Expand Down Expand Up @@ -38,7 +40,7 @@ abstract protected function createIpObject($ip);
* {@inheritdoc}
* @return string
*/
public function getSQLDeclaration(array $column, AbstractPlatform $platform)
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
return $platform->getBinaryTypeDeclarationSQL(['length' => static::IP_LENGTH]);
}
Expand All @@ -48,7 +50,7 @@ public function getSQLDeclaration(array $column, AbstractPlatform $platform)
* @throws \Doctrine\DBAL\Types\ConversionException
* @return \Darsyn\IP\IpInterface|null
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
public function convertToPHPValue($value, AbstractPlatform $platform): mixed
{
/** @var string|resource|\Darsyn\IP\IpInterface|null $value */
// PostgreSQL will return the binary data as a resource instead of a string (like MySQL).
Expand All @@ -71,7 +73,7 @@ public function convertToPHPValue($value, AbstractPlatform $platform)
try {
return $this->createIpObject($value);
} catch (IpException $e) {
throw ConversionException::conversionFailed($value, static::NAME);
throw ValueNotConvertible::new($value, static::NAME, null, $e);
}
}

Expand All @@ -80,7 +82,7 @@ public function convertToPHPValue($value, AbstractPlatform $platform)
* @throws \Doctrine\DBAL\Types\ConversionException
* @return string|null
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
public function convertToDatabaseValue($value, AbstractPlatform $platform): mixed
{
if (empty($value)) {
return null;
Expand Down Expand Up @@ -133,11 +135,11 @@ public function getName()

/**
* {@inheritdoc}
* @return int
* @return ParameterType
*/
public function getBindingType()
public function getBindingType(): ParameterType
{
return \PDO::PARAM_LOB;
return ParameterType::LARGE_OBJECT;
}

/**
Expand Down
17 changes: 3 additions & 14 deletions tests/IPv4TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use Darsyn\IP\Doctrine\IPv4Type;
use Darsyn\IP\Version\IPv4 as IP;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Types\Type;
use PDO;
use PHPUnit\Framework\Attributes as PHPUnit;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -165,19 +165,8 @@ public function testGetBinaryTypeDeclarationSQL()
* @return void
*/
#[PHPUnit\Test]
public function testBindingTypeIsAValidPDOTypeConstant()
public function testBindingTypeIsALargeObject()
{
// 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);
$this->assertEquals(ParameterType::LARGE_OBJECT, $this->type->getBindingType());
}
}
17 changes: 3 additions & 14 deletions tests/IPv6TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use Darsyn\IP\Doctrine\IPv6Type;
use Darsyn\IP\Version\IPv6 as IP;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Types\Type;
use PDO;
use PHPUnit\Framework\Attributes as PHPUnit;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -165,19 +165,8 @@ public function testGetBinaryTypeDeclarationSQL()
* @return void
*/
#[PHPUnit\Test]
public function testBindingTypeIsAValidPDOTypeConstant()
public function testBindingTypeIsALargeObject()
{
// 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);
$this->assertEquals(ParameterType::LARGE_OBJECT, $this->type->getBindingType());
}
}
17 changes: 3 additions & 14 deletions tests/MultiTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use Darsyn\IP\Doctrine\MultiType;
use Darsyn\IP\Version\Multi as IP;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Types\Type;
use PDO;
use PHPUnit\Framework\Attributes as PHPUnit;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -165,19 +165,8 @@ public function testGetBinaryTypeDeclarationSQL()
* @return void
*/
#[PHPUnit\Test]
public function testBindingTypeIsAValidPDOTypeConstant()
public function testBindingTypeIsALargeObject()
{
// 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);
$this->assertEquals(ParameterType::LARGE_OBJECT, $this->type->getBindingType());
}
}
7 changes: 4 additions & 3 deletions tests/TestPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace Darsyn\IP\Tests\Doctrine;

use Doctrine\DBAL\Platforms\SqlitePlatform;

class TestPlatform extends SqlitePlatform
use Doctrine\DBAL\Platforms\SQLitePlatform;

class TestPlatform extends SQLitePlatform
{
public function getBinaryTypeDeclarationSQL(array $column)
public function getBinaryTypeDeclarationSQL(array $column): string
{
return 'DUMMYBINARY()';
}
Expand Down

0 comments on commit 05f8697

Please sign in to comment.