Skip to content

Commit

Permalink
Fix Deprecations: Upgrade to PHPUnit Attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
zanbaldwin committed Mar 28, 2024
1 parent 6554c25 commit 762dde4
Show file tree
Hide file tree
Showing 14 changed files with 411 additions and 174 deletions.
81 changes: 28 additions & 53 deletions tests/Doctrine/IPv4TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Darsyn\IP\Version\IPv4 as IP;
use Doctrine\DBAL\Types\Type;
use PDO;
use PHPUnit\Framework\Attributes as PHPUnit;
use PHPUnit\Framework\TestCase;

class IPv4TypeTest extends TestCase
Expand All @@ -17,43 +18,28 @@ class IPv4TypeTest extends TestCase
private $type;

/** @beforeClass */
#[PHPUnit\BeforeClass]
public static function setUpBeforeClassWithoutReturnDeclaration()
{
if (class_exists(Type::class)) {
Type::addType('ipv4', IPv4Type::class);
}
}

private function getPlatformMock()
{
// We have to use MySQL as the platform here, because the AbstractPlatform does not support BINARY types.
$mockBuilder = $this->getMockBuilder('Doctrine\DBAL\Platforms\MySqlPlatform');
$mockedMethods = ['getBinaryTypeDeclarationSQL'];
// MockBuilder::setMethods() was deprecated in favour of MockBuilder::onlyMethods() in PHPUnit v7.5.x
method_exists($mockBuilder, 'onlyMethods')
? $mockBuilder->onlyMethods($mockedMethods)
: $mockBuilder->setMethods($mockedMethods);
return $mockBuilder->getMockForAbstractClass();
}

/** @before */
#[PHPUnit\Before]
protected function setUpWithoutReturnDeclaration()
{
if (!class_exists('Doctrine\DBAL\Types\Type')) {
$this->markTestSkipped('Skipping test that requires "doctrine/dbal".');
}

$this->platform = $this->getPlatformMock();
$this->platform
->expects($this->any())
->method('getBinaryTypeDeclarationSQL')
->will($this->returnValue('DUMMYBINARY()'));
$this->platform = new TestPlatform;
$this->type = Type::getType('ipv4');
}

/**
* @test
*/
/** @test */
#[PHPUnit\Test]
public function testIpConvertsToDatabaseValue()
{
$ip = IP::factory('12.34.56.78');
Expand All @@ -64,26 +50,23 @@ public function testIpConvertsToDatabaseValue()
$this->assertEquals($expected, $actual);
}

/**
* @test
*/
/** @test */
#[PHPUnit\Test]
public function testInvalidIpConversionForDatabaseValue()
{
$this->expectException(\Doctrine\DBAL\Types\ConversionException::class);
$this->type->convertToDatabaseValue('abcdefg', $this->platform);
}

/**
* @test
*/
/** @test */
#[PHPUnit\Test]
public function testNullConversionForDatabaseValue()
{
$this->assertNull($this->type->convertToDatabaseValue(null, $this->platform));
}

/**
* @test
*/
/** @test */
#[PHPUnit\Test]
public function testIpConvertsToPHPValue()
{
$ip = IP::factory('12.34.56.78');
Expand All @@ -93,9 +76,8 @@ public function testIpConvertsToPHPValue()
$this->assertEquals('12.34.56.78', $dbIp->getDotAddress());
}

/**
* @test
*/
/** @test */
#[PHPUnit\Test]
public function testIpObjectConvertsToPHPValue()
{
$ip = IP::factory('12.34.56.78');
Expand All @@ -105,9 +87,8 @@ public function testIpObjectConvertsToPHPValue()
$this->assertSame($ip, $dbIp);
}

/**
* @test
*/
/** @test */
#[PHPUnit\Test]
public function testStreamConvertsToPHPValue()
{
$ip = IP::factory('12.34.56.78');
Expand All @@ -120,42 +101,37 @@ public function testStreamConvertsToPHPValue()
$this->assertEquals('12.34.56.78', $dbIp->getDotAddress());
}

/**
* @test
*/
/** @test */
#[PHPUnit\Test]
public function testInvalidIpConversionForPHPValue()
{
$this->expectException(\Doctrine\DBAL\Types\ConversionException::class);
$this->type->convertToPHPValue('abcdefg', $this->platform);
}

/**
* @test
*/
/** @test */
#[PHPUnit\Test]
public function testNullConversionForPHPValue()
{
$this->assertNull($this->type->convertToPHPValue(null, $this->platform));
}

/**
* @test
*/
/** @test */
#[PHPUnit\Test]
public function testGetName()
{
$this->assertEquals('ip', $this->type->getName());
}

/**
* @test
*/
/** @test */
#[PHPUnit\Test]
public function testGetBinaryTypeDeclarationSQL()
{
$this->assertEquals('DUMMYBINARY()', $this->type->getSqlDeclaration(['length' => 4], $this->platform));
}

/**
* @test
*/
/** @test */
#[PHPUnit\Test]
public function testBindingTypeIsAValidPDOTypeConstant()
{
// Get all constants of the PDO class.
Expand All @@ -172,9 +148,8 @@ public function testBindingTypeIsAValidPDOTypeConstant()
$this->assertContains($this->type->getBindingType(), $paramConstants);
}

/**
* @test
*/
/** @test */
#[PHPUnit\Test]
public function testRequiresSQLCommentHint()
{
$this->assertTrue($this->type->requiresSQLCommentHint($this->platform));
Expand Down
81 changes: 28 additions & 53 deletions tests/Doctrine/IPv6TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Darsyn\IP\Version\IPv6 as IP;
use Doctrine\DBAL\Types\Type;
use PDO;
use PHPUnit\Framework\Attributes as PHPUnit;
use PHPUnit\Framework\TestCase;

class IPv6TypeTest extends TestCase
Expand All @@ -17,43 +18,28 @@ class IPv6TypeTest extends TestCase
private $type;

/** @beforeClass */
#[PHPUnit\BeforeClass]
public static function setUpBeforeClassWithoutReturnDeclaration()
{
if (class_exists(Type::class)) {
Type::addType('ipv6', IPv6Type::class);
}
}

private function getPlatformMock()
{
// We have to use MySQL as the platform here, because the AbstractPlatform does not support BINARY types.
$mockBuilder = $this->getMockBuilder('Doctrine\DBAL\Platforms\MySqlPlatform');
$mockedMethods = ['getBinaryTypeDeclarationSQL'];
// MockBuilder::setMethods() was deprecated in favour of MockBuilder::onlyMethods() in PHPUnit v7.5.x
method_exists($mockBuilder, 'onlyMethods')
? $mockBuilder->onlyMethods($mockedMethods)
: $mockBuilder->setMethods($mockedMethods);
return $mockBuilder->getMockForAbstractClass();
}

/** @before */
#[PHPUnit\Before]
protected function setUpWithoutReturnDeclaration()
{
if (!class_exists('Doctrine\DBAL\Types\Type')) {
$this->markTestSkipped('Skipping test that requires "doctrine/dbal".');
}

$this->platform = $this->getPlatformMock();
$this->platform
->expects($this->any())
->method('getBinaryTypeDeclarationSQL')
->will($this->returnValue('DUMMYBINARY()'));
$this->platform = new TestPlatform;
$this->type = Type::getType('ipv6');
}

/**
* @test
*/
/** @test */
#[PHPUnit\Test]
public function testIpConvertsToDatabaseValue()
{
$ip = IP::factory('::1');
Expand All @@ -64,26 +50,23 @@ public function testIpConvertsToDatabaseValue()
$this->assertEquals($expected, $actual);
}

/**
* @test
*/
/** @test */
#[PHPUnit\Test]
public function testInvalidIpConversionForDatabaseValue()
{
$this->expectException(\Doctrine\DBAL\Types\ConversionException::class);
$this->type->convertToDatabaseValue('abcdefg', $this->platform);
}

/**
* @test
*/
/** @test */
#[PHPUnit\Test]
public function testNullConversionForDatabaseValue()
{
$this->assertNull($this->type->convertToDatabaseValue(null, $this->platform));
}

/**
* @test
*/
/** @test */
#[PHPUnit\Test]
public function testIpConvertsToPHPValue()
{
$ip = IP::factory('::1');
Expand All @@ -93,9 +76,8 @@ public function testIpConvertsToPHPValue()
$this->assertEquals('::1', $dbIp->getCompactedAddress());
}

/**
* @test
*/
/** @test */
#[PHPUnit\Test]
public function testIpObjectConvertsToPHPValue()
{
$ip = IP::factory('::1');
Expand All @@ -105,9 +87,8 @@ public function testIpObjectConvertsToPHPValue()
$this->assertSame($ip, $dbIp);
}

/**
* @test
*/
/** @test */
#[PHPUnit\Test]
public function testStreamConvertsToPHPValue()
{
$ip = IP::factory('::1');
Expand All @@ -120,42 +101,37 @@ public function testStreamConvertsToPHPValue()
$this->assertEquals('::1', $dbIp->getCompactedAddress());
}

/**
* @test
*/
/** @test */
#[PHPUnit\Test]
public function testInvalidIpConversionForPHPValue()
{
$this->expectException(\Doctrine\DBAL\Types\ConversionException::class);
$this->type->convertToPHPValue('abcdefg', $this->platform);
}

/**
* @test
*/
/** @test */
#[PHPUnit\Test]
public function testNullConversionForPHPValue()
{
$this->assertNull($this->type->convertToPHPValue(null, $this->platform));
}

/**
* @test
*/
/** @test */
#[PHPUnit\Test]
public function testGetName()
{
$this->assertEquals('ip', $this->type->getName());
}

/**
* @test
*/
/** @test */
#[PHPUnit\Test]
public function testGetBinaryTypeDeclarationSQL()
{
$this->assertEquals('DUMMYBINARY()', $this->type->getSqlDeclaration(['length' => 16], $this->platform));
}

/**
* @test
*/
/** @test */
#[PHPUnit\Test]
public function testBindingTypeIsAValidPDOTypeConstant()
{
// Get all constants of the PDO class.
Expand All @@ -172,9 +148,8 @@ public function testBindingTypeIsAValidPDOTypeConstant()
$this->assertContains($this->type->getBindingType(), $paramConstants);
}

/**
* @test
*/
/** @test */
#[PHPUnit\Test]
public function testRequiresSQLCommentHint()
{
$this->assertTrue($this->type->requiresSQLCommentHint($this->platform));
Expand Down
Loading

0 comments on commit 762dde4

Please sign in to comment.