1

As described here it is possible to create custom Types.

I've done it, but now I'd like to test the class but unfortunately this is not possible.

Infact, a custom type extends the class Doctrine\DBAL\Types\Type that seems that cannot be instantiated.

Infact, its constructor is built this way:

/**
 * Prevents instantiation and forces use of the factory method.
 */
final private function __construct()
{
}

As told in the comment, it is required the use of the factory method to instantiate the class, but, what this factory method is? Where can I find it?

Aerendir
  • 6,152
  • 9
  • 55
  • 108
  • you could take a look at the DBAL test suite, as example see this test https://github.com/doctrine/dbal/blob/master/tests/Doctrine/Tests/DBAL/Types/DecimalTest.php – Matteo Oct 06 '16 at 15:51
  • Mmm... It seems `Doctrine\Tests\DBAL\Mocks\MockPlatform;` is unfoundable! -.-' – Aerendir Oct 06 '16 at 18:46
  • They mention this [here](https://github.com/ramsey/uuid/issues/16#issuecomment-24575462) but I'm not sure how they broke it. – mickadoo Oct 12 '16 at 10:12

1 Answers1

2

Through a combination of not replacing any methods in a mock and some tips from an issue on github I was able to make a unit test for a doctrine type. I guess this works for simpler types where the platform isn't relevant. For more complex type behavior you could substitute the platform with a different mock.

/**
 * @test
 */
public function willCastValueToInt()
{
    $typeBuilder = $this
        ->getMockBuilder(IntegerType::class)
        ->disableOriginalConstructor()
        ->setMethods(null);

    $type = $typeBuilder->getMock();
    $platform = $this->getMockForAbstractClass(AbstractPlatform::class);

    $result = $type->convertToPHPValue('3', $platform);

    $this->assertSame(3, $result);
}
Community
  • 1
  • 1
mickadoo
  • 3,337
  • 1
  • 25
  • 38