Skip to content

Commit

Permalink
Refactoring and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem Henvald committed Mar 12, 2018
1 parent 6787a9f commit 9772b43
Show file tree
Hide file tree
Showing 15 changed files with 156 additions and 152 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ install:
script:
- ./vendor/bin/phpcs ./ -p --encoding=utf-8 --extensions=php --ignore="vendor|Tests" --standard=./vendor/escapestudios/symfony2-coding-standard/Symfony
- ./vendor/bin/phpunit -c phpunit.xml.dist --coverage-clover=coverage.xml
- ./vendor/bin/phpstan analyse -l 7 --no-progress -c phpstan.neon

after_success:
- bash <(curl -s https://codecov.io/bash)
Expand Down
2 changes: 1 addition & 1 deletion DBAL/Types/AbstractEnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function requiresSQLCommentHint(AbstractPlatform $platform): bool
/**
* {@inheritdoc}
*/
public function getName()
public function getName(): string
{
return $this->name ?: \array_search(\get_class($this), self::getTypesMap(), true);
}
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/FreshDoctrineEnumExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function load(array $configs, ContainerBuilder $container): void
$loader->load('services.yml');

if (\interface_exists(FormTypeGuesserInterface::class)) {
$loader->load('enum_guesser.yml');
$loader->load('form_type_guesser.yml');
}
}
}
File renamed without changes.
12 changes: 3 additions & 9 deletions Tests/DBAL/Types/AbstractEnumTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,9 @@ public function testConvertToDatabaseValue(): void
self::assertEquals('SF', $this->type->convertToDatabaseValue('SF', new MySqlPlatform()));
}

/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidArgumentExceptionInConvertToDatabaseValue(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->type->convertToDatabaseValue('YO', new MySqlPlatform());
}

Expand All @@ -128,11 +126,9 @@ public function testAssertValidChoice(): void
self::assertNull($this->type::assertValidChoice(BasketballPositionType::SMALL_FORWARD));
}

/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidArgumentExceptionInAssertValidChoice(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->type::assertValidChoice('YO');
}

Expand All @@ -141,11 +137,9 @@ public function testGetReadableValue(): void
self::assertEquals('Small Forward', $this->type::getReadableValue(BasketballPositionType::SMALL_FORWARD));
}

/**
* @expectedException \InvalidArgumentException
*/
public function testInvalidArgumentExceptionInGetReadableValue(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->type::getReadableValue('YO');
}

Expand Down
42 changes: 13 additions & 29 deletions Tests/Form/EnumTypeGuesserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Fresh\DoctrineEnumBundle\Exception\EnumType\EnumTypeIsRegisteredButClassDoesNotExistException;
use Fresh\DoctrineEnumBundle\Form\EnumTypeGuesser;
use Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\BasketballPositionType;
use Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\InheritedType;
Expand All @@ -31,7 +32,7 @@ class EnumTypeGuesserTest extends TestCase
{
public function testNullResultWhenClassMetadataNotFound(): void
{
/** @var EnumTypeGuesser|MockObject */
/** @var EnumTypeGuesser|MockObject $enumTypeGuesser */
$enumTypeGuesser = $this
->getMockBuilder(EnumTypeGuesser::class)
->disableOriginalConstructor()
Expand All @@ -50,7 +51,7 @@ public function testNullResultWhenClassMetadataNotFound(): void

public function testNullResultWhenEnumTypeNotRegistered(): void
{
/** @var EnumTypeGuesser|MockObject */
/** @var EnumTypeGuesser|MockObject $enumTypeGuesser */
$enumTypeGuesser = $this
->getMockBuilder(EnumTypeGuesser::class)
->disableOriginalConstructor()
Expand Down Expand Up @@ -80,24 +81,17 @@ public function testNullResultWhenEnumTypeNotRegistered(): void
self::assertNull($enumTypeGuesser->guessType(\stdClass::class, 'position'));
}

/**
* @expectedException \Fresh\DoctrineEnumBundle\Exception\EnumType\EnumTypeIsRegisteredButClassDoesNotExistException
*/
public function testExceptionWhenClassDoesNotExist(): void
{
$managerRegistry = $this
->getMockBuilder(ManagerRegistry::class)
->disableOriginalConstructor()
->getMock()
;
$managerRegistry = $this->createMock(ManagerRegistry::class);

$registeredTypes = [
'stub' => [
'class' => '\Acme\Foo\Bar\Baz',
]
];

/** @var EnumTypeGuesser|MockObject */
/** @var EnumTypeGuesser|MockObject $enumTypeGuesser */
$enumTypeGuesser = $this
->getMockBuilder(EnumTypeGuesser::class)
->setConstructorArgs([$managerRegistry, $registeredTypes])
Expand All @@ -124,24 +118,22 @@ public function testExceptionWhenClassDoesNotExist(): void
->willReturn([$metadata])
;

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

self::assertNull($enumTypeGuesser->guessType(\stdClass::class, 'position'));
}

public function testNullResultWhenIsNotChildOfAbstractEnumType(): void
{
$managerRegistry = $this
->getMockBuilder(ManagerRegistry::class)
->disableOriginalConstructor()
->getMock()
;
$managerRegistry = $this->createMock(ManagerRegistry::class);

$registeredTypes = [
'NotAChildType' => [
'class' => NotAChildType::class,
]
];

/** @var EnumTypeGuesser|MockObject */
/** @var EnumTypeGuesser|MockObject $enumTypeGuesser */
$enumTypeGuesser = $this
->getMockBuilder(EnumTypeGuesser::class)
->setConstructorArgs([$managerRegistry, $registeredTypes])
Expand Down Expand Up @@ -173,19 +165,15 @@ public function testNullResultWhenIsNotChildOfAbstractEnumType(): void

public function testSuccessfulTypeGuessingWithAncestor(): void
{
$managerRegistry = $this
->getMockBuilder(ManagerRegistry::class)
->disableOriginalConstructor()
->getMock()
;
$managerRegistry = $this->createMock(ManagerRegistry::class);

$registeredTypes = [
'InheritedType' => [
'class' => InheritedType::class,
]
];

/** @var EnumTypeGuesser|MockObject */
/** @var EnumTypeGuesser|MockObject $enumTypeGuesser */
$enumTypeGuesser = $this
->getMockBuilder(EnumTypeGuesser::class)
->setConstructorArgs([$managerRegistry, $registeredTypes])
Expand Down Expand Up @@ -232,19 +220,15 @@ public function testSuccessfulTypeGuessingWithAncestor(): void

public function testSuccessfulTypeGuessing(): void
{
$managerRegistry = $this
->getMockBuilder(ManagerRegistry::class)
->disableOriginalConstructor()
->getMock()
;
$managerRegistry = $this->createMock(ManagerRegistry::class);

$registeredTypes = [
'BasketballPositionType' => [
'class' => BasketballPositionType::class,
]
];

/** @var EnumTypeGuesser|MockObject */
/** @var EnumTypeGuesser|MockObject $enumTypeGuesser */
$enumTypeGuesser = $this
->getMockBuilder(EnumTypeGuesser::class)
->setConstructorArgs([$managerRegistry, $registeredTypes])
Expand Down
104 changes: 50 additions & 54 deletions Tests/FreshDoctrineEnumBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* file that was distributed with this source code.
*/

namespace Fresh\DoctrineEnumBundle\Tests\Validator;
namespace Fresh\DoctrineEnumBundle\Tests;

use Doctrine\Bundle\DoctrineBundle\Registry;
use Doctrine\DBAL\Connection;
Expand All @@ -19,7 +19,7 @@
use Symfony\Component\DependencyInjection\Container;

/**
* FreshDoctrineEnumBundleTest.
* FreshDoctrineEnumBundleTest
*
* @author Artem Henvald <genvaldartem@gmail.com>
*/
Expand All @@ -33,21 +33,15 @@ class FreshDoctrineEnumBundleTest extends TestCase

protected function setUp(): void
{
$this->container = $this->getMockBuilder(Container::class)
->disableOriginalConstructor()
->setMethods(['get'])
->getMock();

$this->doctrine = $this->getMockBuilder(Registry::class)
->disableOriginalConstructor()
->setMethods(['getConnections'])
->getMock();

$this->container->expects($this->once())
->method('get')
->with('doctrine')
->willReturn($this->doctrine);

$this->doctrine = $this->createMock(Registry::class);

$this->container = $this->createMock(Container::class);
$this->container
->expects($this->once())
->method('get')
->with('doctrine')
->willReturn($this->doctrine)
;
}

protected function tearDown(): void
Expand All @@ -64,27 +58,27 @@ public function testEnumMappingRegistration(): void
$databasePlatformAbc = $this->getMockForAbstractClass(AbstractPlatform::class);
$databasePlatformDef = $this->getMockForAbstractClass(AbstractPlatform::class);

$connectionAbc = $this->getMockBuilder(Connection::class)
->disableOriginalConstructor()
->setMethods(['getDatabasePlatform'])
->getMock();
$connectionAbc = $this->createMock(Connection::class);

$connectionAbc->expects($this->once())
->method('getDatabasePlatform')
->willReturn($databasePlatformAbc);
$connectionAbc
->expects($this->once())
->method('getDatabasePlatform')
->willReturn($databasePlatformAbc)
;

$connectionDef = $this->getMockBuilder(Connection::class)
->disableOriginalConstructor()
->setMethods(['getDatabasePlatform'])
->getMock();
$connectionDef = $this->createMock(Connection::class);

$connectionDef->expects($this->once())
->method('getDatabasePlatform')
->willReturn($databasePlatformDef);
$connectionDef
->expects($this->once())
->method('getDatabasePlatform')
->willReturn($databasePlatformDef)
;

$this->doctrine->expects($this->once())
->method('getConnections')
->willReturn([$connectionAbc, $connectionDef]);
$this->doctrine
->expects($this->once())
->method('getConnections')
->willReturn([$connectionAbc, $connectionDef])
;

$bundle = new FreshDoctrineEnumBundle();
$bundle->setContainer($this->container);
Expand All @@ -102,18 +96,19 @@ public function testAlreadyRegisteredEnumMapping(): void
/** @var AbstractPlatform|MockObject $databasePlatformAbc */
$databasePlatformAbc = $this->getMockForAbstractClass(AbstractPlatform::class);

$connectionAbc = $this->getMockBuilder(Connection::class)
->disableOriginalConstructor()
->setMethods(['getDatabasePlatform'])
->getMock();
$connectionAbc = $this->createMock(Connection::class);

$connectionAbc->expects($this->once())
->method('getDatabasePlatform')
->willReturn($databasePlatformAbc);
$connectionAbc
->expects($this->once())
->method('getDatabasePlatform')
->willReturn($databasePlatformAbc)
;

$this->doctrine->expects($this->once())
->method('getConnections')
->willReturn([$connectionAbc]);
$this->doctrine
->expects($this->once())
->method('getConnections')
->willReturn([$connectionAbc])
;

$databasePlatformAbc->registerDoctrineTypeMapping('enum', 'string');

Expand All @@ -130,18 +125,19 @@ public function testEnumMappingReregistrationToString(): void
/** @var AbstractPlatform|MockObject $databasePlatformAbc */
$databasePlatformAbc = $this->getMockForAbstractClass(AbstractPlatform::class);

$connectionAbc = $this->getMockBuilder(Connection::class)
->disableOriginalConstructor()
->setMethods(['getDatabasePlatform'])
->getMock();
$connectionAbc = $this->createMock(Connection::class);

$connectionAbc->expects($this->once())
->method('getDatabasePlatform')
->willReturn($databasePlatformAbc);
$connectionAbc
->expects($this->once())
->method('getDatabasePlatform')
->willReturn($databasePlatformAbc)
;

$this->doctrine->expects($this->once())
->method('getConnections')
->willReturn([$connectionAbc]);
$this->doctrine
->expects($this->once())
->method('getConnections')
->willReturn([$connectionAbc])
;

$databasePlatformAbc->registerDoctrineTypeMapping('enum', 'boolean');

Expand Down
24 changes: 9 additions & 15 deletions Tests/Twig/Extension/EnumConstantExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

namespace Fresh\DoctrineEnumBundle\Tests\Twig\Extension;

use Fresh\DoctrineEnumBundle\Exception\Constant\ConstantIsFoundInFewRegisteredEnumTypesException;
use Fresh\DoctrineEnumBundle\Exception\Constant\ConstantIsNotFoundInAnyRegisteredEnumTypeException;
use Fresh\DoctrineEnumBundle\Exception\EnumType\EnumTypeIsNotRegisteredException;
use Fresh\DoctrineEnumBundle\Exception\EnumType\NoRegisteredEnumTypesException;
use Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\BasketballPositionType;
use Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\MapLocationType;
use Fresh\DoctrineEnumBundle\Twig\Extension\EnumConstantTwigExtension;
Expand Down Expand Up @@ -69,37 +73,27 @@ public function dataProviderForGetReadableEnumValueTest(): array
];
}

/**
* @expectedException \Fresh\DoctrineEnumBundle\Exception\EnumType\EnumTypeIsNotRegisteredException
*/
public function testEnumTypeIsNotRegisteredException(): void
{
$this->expectException(EnumTypeIsNotRegisteredException::class);
$this->enumConstantExtension->getEnumConstant('Pitcher', 'BaseballPositionType');
}

/**
* @expectedException \Fresh\DoctrineEnumBundle\Exception\Constant\ConstantIsFoundInFewRegisteredEnumTypesException
*/
public function testConstantIsFoundInFewRegisteredEnumTypesException(): void
{
$this->expectException(ConstantIsFoundInFewRegisteredEnumTypesException::class);
$this->enumConstantExtension->getEnumConstant('CENTER');
}

/**
* @expectedException \Fresh\DoctrineEnumBundle\Exception\Constant\ConstantIsNotFoundInAnyRegisteredEnumTypeException
*/
public function testConstantIsNotFoundInAnyRegisteredEnumTypeException(): void
{
$this->expectException(ConstantIsNotFoundInAnyRegisteredEnumTypeException::class);
$this->enumConstantExtension->getEnumConstant('Pitcher');
}

/**
* @expectedException \Fresh\DoctrineEnumBundle\Exception\EnumType\NoRegisteredEnumTypesException
*/
public function testNoRegisteredEnumTypesException(): void
{
// Create EnumConstantExtension without any registered ENUM type
$extension = new EnumConstantTwigExtension([]);
$extension->getEnumConstant(BasketballPositionType::POINT_GUARD, 'BasketballPositionType');
$this->expectException(NoRegisteredEnumTypesException::class);
(new EnumConstantTwigExtension([]))->getEnumConstant(BasketballPositionType::POINT_GUARD, 'BasketballPositionType');
}
}
Loading

0 comments on commit 9772b43

Please sign in to comment.