Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reworked the mocks generated by Prophecy using PHPUnit #3609

Merged
merged 1 commit into from
Jun 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions tests/Doctrine/Tests/DBAL/Types/DateImmutableTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\DateImmutableType;
use Doctrine\DBAL\Types\Type;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use function get_class;

class DateImmutableTypeTest extends TestCase
{
/** @var AbstractPlatform|ObjectProphecy */
/** @var AbstractPlatform|MockObject */
Ocramius marked this conversation as resolved.
Show resolved Hide resolved
private $platform;

/** @var DateImmutableType */
Expand All @@ -24,7 +24,7 @@ class DateImmutableTypeTest extends TestCase
protected function setUp() : void
{
$this->type = Type::getType('date_immutable');
$this->platform = $this->prophesize(AbstractPlatform::class);
$this->platform = $this->createMock(AbstractPlatform::class);
Ocramius marked this conversation as resolved.
Show resolved Hide resolved
}

public function testFactoryCreatesCorrectType() : void
Expand All @@ -44,56 +44,65 @@ public function testReturnsBindingType() : void

public function testConvertsDateTimeImmutableInstanceToDatabaseValue() : void
{
$date = $this->prophesize(DateTimeImmutable::class);
$date = $this->createMock(DateTimeImmutable::class);

$this->platform->getDateFormatString()->willReturn('Y-m-d')->shouldBeCalled();
$date->format('Y-m-d')->willReturn('2016-01-01')->shouldBeCalled();
$this->platform->expects($this->once())
->method('getDateFormatString')
->willReturn('Y-m-d');
$date->expects($this->once())
->method('format')
->with('Y-m-d')
->willReturn('2016-01-01');

self::assertSame(
'2016-01-01',
$this->type->convertToDatabaseValue($date->reveal(), $this->platform->reveal())
$this->type->convertToDatabaseValue($date, $this->platform)
);
}

public function testConvertsNullToDatabaseValue() : void
{
self::assertNull($this->type->convertToDatabaseValue(null, $this->platform->reveal()));
self::assertNull($this->type->convertToDatabaseValue(null, $this->platform));
}

public function testDoesNotSupportMutableDateTimeToDatabaseValueConversion() : void
{
$this->expectException(ConversionException::class);

$this->type->convertToDatabaseValue(new DateTime(), $this->platform->reveal());
$this->type->convertToDatabaseValue(new DateTime(), $this->platform);
}

public function testConvertsDateTimeImmutableInstanceToPHPValue() : void
{
$date = new DateTimeImmutable();

self::assertSame($date, $this->type->convertToPHPValue($date, $this->platform->reveal()));
self::assertSame($date, $this->type->convertToPHPValue($date, $this->platform));
}

public function testConvertsNullToPHPValue() : void
{
self::assertNull($this->type->convertToPHPValue(null, $this->platform->reveal()));
self::assertNull($this->type->convertToPHPValue(null, $this->platform));
}

public function testConvertsDateStringToPHPValue() : void
{
$this->platform->getDateFormatString()->willReturn('Y-m-d')->shouldBeCalled();
$this->platform->expects($this->once())
->method('getDateFormatString')
->willReturn('Y-m-d');

$date = $this->type->convertToPHPValue('2016-01-01', $this->platform->reveal());
$date = $this->type->convertToPHPValue('2016-01-01', $this->platform);

self::assertInstanceOf(DateTimeImmutable::class, $date);
self::assertSame('2016-01-01', $date->format('Y-m-d'));
}

public function testResetTimeFractionsWhenConvertingToPHPValue() : void
{
$this->platform->getDateFormatString()->willReturn('Y-m-d');
$this->platform->expects($this->any())
->method('getDateFormatString')
->willReturn('Y-m-d');

$date = $this->type->convertToPHPValue('2016-01-01', $this->platform->reveal());
$date = $this->type->convertToPHPValue('2016-01-01', $this->platform);

self::assertSame('2016-01-01 00:00:00.000000', $date->format('Y-m-d H:i:s.u'));
}
Expand All @@ -102,11 +111,11 @@ public function testThrowsExceptionDuringConversionToPHPValueWithInvalidDateStri
{
$this->expectException(ConversionException::class);

$this->type->convertToPHPValue('invalid date string', $this->platform->reveal());
$this->type->convertToPHPValue('invalid date string', $this->platform);
}

public function testRequiresSQLCommentHint() : void
{
self::assertTrue($this->type->requiresSQLCommentHint($this->platform->reveal()));
self::assertTrue($this->type->requiresSQLCommentHint($this->platform));
}
}
47 changes: 30 additions & 17 deletions tests/Doctrine/Tests/DBAL/Types/DateTimeImmutableTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\DateTimeImmutableType;
use Doctrine\DBAL\Types\Type;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use function get_class;

class DateTimeImmutableTypeTest extends TestCase
{
/** @var AbstractPlatform|ObjectProphecy */
/** @var AbstractPlatform|MockObject */
private $platform;

/** @var DateTimeImmutableType */
Expand All @@ -24,7 +24,7 @@ class DateTimeImmutableTypeTest extends TestCase
protected function setUp() : void
{
$this->type = Type::getType('datetime_immutable');
$this->platform = $this->prophesize(AbstractPlatform::class);
$this->platform = $this->getMockBuilder(AbstractPlatform::class)->getMock();
}

public function testFactoryCreatesCorrectType() : void
Expand All @@ -44,46 +44,53 @@ public function testReturnsBindingType() : void

public function testConvertsDateTimeImmutableInstanceToDatabaseValue() : void
{
$date = $this->prophesize(DateTimeImmutable::class);
$date = $this->getMockBuilder(DateTimeImmutable::class)->getMock();

$this->platform->getDateTimeFormatString()->willReturn('Y-m-d H:i:s')->shouldBeCalled();
$date->format('Y-m-d H:i:s')->willReturn('2016-01-01 15:58:59')->shouldBeCalled();
$this->platform->expects($this->once())
->method('getDateTimeFormatString')
->willReturn('Y-m-d H:i:s');
$date->expects($this->once())
->method('format')
->with('Y-m-d H:i:s')
->willReturn('2016-01-01 15:58:59');

self::assertSame(
'2016-01-01 15:58:59',
$this->type->convertToDatabaseValue($date->reveal(), $this->platform->reveal())
$this->type->convertToDatabaseValue($date, $this->platform)
);
}

public function testConvertsNullToDatabaseValue() : void
{
self::assertNull($this->type->convertToDatabaseValue(null, $this->platform->reveal()));
self::assertNull($this->type->convertToDatabaseValue(null, $this->platform));
}

public function testDoesNotSupportMutableDateTimeToDatabaseValueConversion() : void
{
$this->expectException(ConversionException::class);

$this->type->convertToDatabaseValue(new DateTime(), $this->platform->reveal());
$this->type->convertToDatabaseValue(new DateTime(), $this->platform);
}

public function testConvertsDateTimeImmutableInstanceToPHPValue() : void
{
$date = new DateTimeImmutable();

self::assertSame($date, $this->type->convertToPHPValue($date, $this->platform->reveal()));
self::assertSame($date, $this->type->convertToPHPValue($date, $this->platform));
}

public function testConvertsNullToPHPValue() : void
{
self::assertNull($this->type->convertToPHPValue(null, $this->platform->reveal()));
self::assertNull($this->type->convertToPHPValue(null, $this->platform));
}

public function testConvertsDateTimeStringToPHPValue() : void
{
$this->platform->getDateTimeFormatString()->willReturn('Y-m-d H:i:s')->shouldBeCalled();
$this->platform->expects($this->once())
->method('getDateTimeFormatString')
->willReturn('Y-m-d H:i:s');

$date = $this->type->convertToPHPValue('2016-01-01 15:58:59', $this->platform->reveal());
$date = $this->type->convertToPHPValue('2016-01-01 15:58:59', $this->platform);

self::assertInstanceOf(DateTimeImmutable::class, $date);
self::assertSame('2016-01-01 15:58:59', $date->format('Y-m-d H:i:s'));
Expand All @@ -94,22 +101,28 @@ public function testConvertsDateTimeStringToPHPValue() : void
*/
public function testConvertsDateTimeStringWithMicrosecondsToPHPValue() : void
{
$this->platform->getDateTimeFormatString()->willReturn('Y-m-d H:i:s');
$this->platform->expects($this->any())
->method('getDateTimeFormatString')
->willReturn('Y-m-d H:i:s');

$date = $this->type->convertToPHPValue('2016-01-01 15:58:59.123456', $this->platform->reveal());
$date = $this->type->convertToPHPValue('2016-01-01 15:58:59.123456', $this->platform);

self::assertSame('2016-01-01 15:58:59', $date->format('Y-m-d H:i:s'));
}

public function testThrowsExceptionDuringConversionToPHPValueWithInvalidDateTimeString() : void
{
$this->platform->expects($this->atLeastOnce())
->method('getDateTimeFormatString')
->willReturn('Y-m-d H:i:s');

$this->expectException(ConversionException::class);

$this->type->convertToPHPValue('invalid datetime string', $this->platform->reveal());
$this->type->convertToPHPValue('invalid datetime string', $this->platform);
}

public function testRequiresSQLCommentHint() : void
{
self::assertTrue($this->type->requiresSQLCommentHint($this->platform->reveal()));
self::assertTrue($this->type->requiresSQLCommentHint($this->platform));
}
}
41 changes: 26 additions & 15 deletions tests/Doctrine/Tests/DBAL/Types/DateTimeTzImmutableTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\DateTimeTzImmutableType;
use Doctrine\DBAL\Types\Type;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use function get_class;

class DateTimeTzImmutableTypeTest extends TestCase
{
/** @var AbstractPlatform|ObjectProphecy */
/** @var AbstractPlatform|MockObject */
private $platform;

/** @var DateTimeTzImmutableType */
Expand All @@ -24,7 +24,7 @@ class DateTimeTzImmutableTypeTest extends TestCase
protected function setUp() : void
{
$this->type = Type::getType('datetimetz_immutable');
$this->platform = $this->prophesize(AbstractPlatform::class);
$this->platform = $this->createMock(AbstractPlatform::class);
}

public function testFactoryCreatesCorrectType() : void
Expand All @@ -44,60 +44,71 @@ public function testReturnsBindingType() : void

public function testConvertsDateTimeImmutableInstanceToDatabaseValue() : void
{
$date = $this->prophesize(DateTimeImmutable::class);
$date = $this->createMock(DateTimeImmutable::class);

$this->platform->getDateTimeTzFormatString()->willReturn('Y-m-d H:i:s T')->shouldBeCalled();
$date->format('Y-m-d H:i:s T')->willReturn('2016-01-01 15:58:59 UTC')->shouldBeCalled();
$this->platform->expects($this->once())
->method('getDateTimeTzFormatString')
->willReturn('Y-m-d H:i:s T');
$date->expects($this->once())
->method('format')
->with('Y-m-d H:i:s T')
->willReturn('2016-01-01 15:58:59 UTC');

self::assertSame(
'2016-01-01 15:58:59 UTC',
$this->type->convertToDatabaseValue($date->reveal(), $this->platform->reveal())
$this->type->convertToDatabaseValue($date, $this->platform)
);
}

public function testConvertsNullToDatabaseValue() : void
{
self::assertNull($this->type->convertToDatabaseValue(null, $this->platform->reveal()));
self::assertNull($this->type->convertToDatabaseValue(null, $this->platform));
}

public function testDoesNotSupportMutableDateTimeToDatabaseValueConversion() : void
{
$this->expectException(ConversionException::class);

$this->type->convertToDatabaseValue(new DateTime(), $this->platform->reveal());
$this->type->convertToDatabaseValue(new DateTime(), $this->platform);
}

public function testConvertsDateTimeImmutableInstanceToPHPValue() : void
{
$date = new DateTimeImmutable();

self::assertSame($date, $this->type->convertToPHPValue($date, $this->platform->reveal()));
self::assertSame($date, $this->type->convertToPHPValue($date, $this->platform));
}

public function testConvertsNullToPHPValue() : void
{
self::assertNull($this->type->convertToPHPValue(null, $this->platform->reveal()));
self::assertNull($this->type->convertToPHPValue(null, $this->platform));
}

public function testConvertsDateTimeWithTimezoneStringToPHPValue() : void
{
$this->platform->getDateTimeTzFormatString()->willReturn('Y-m-d H:i:s T')->shouldBeCalled();
$this->platform->expects($this->once())
->method('getDateTimeTzFormatString')
->willReturn('Y-m-d H:i:s T');

$date = $this->type->convertToPHPValue('2016-01-01 15:58:59 UTC', $this->platform->reveal());
$date = $this->type->convertToPHPValue('2016-01-01 15:58:59 UTC', $this->platform);

self::assertInstanceOf(DateTimeImmutable::class, $date);
self::assertSame('2016-01-01 15:58:59 UTC', $date->format('Y-m-d H:i:s T'));
}

public function testThrowsExceptionDuringConversionToPHPValueWithInvalidDateTimeWithTimezoneString() : void
{
$this->platform->expects($this->atLeastOnce())
->method('getDateTimeTzFormatString')
->willReturn('Y-m-d H:i:s T');

$this->expectException(ConversionException::class);

$this->type->convertToPHPValue('invalid datetime with timezone string', $this->platform->reveal());
$this->type->convertToPHPValue('invalid datetime with timezone string', $this->platform);
}

public function testRequiresSQLCommentHint() : void
{
self::assertTrue($this->type->requiresSQLCommentHint($this->platform->reveal()));
self::assertTrue($this->type->requiresSQLCommentHint($this->platform));
}
}
Loading