From a908f610df6e5c0d2505af4f8449ec47bb267679 Mon Sep 17 00:00:00 2001 From: Robin Date: Thu, 22 Oct 2015 20:45:07 +0200 Subject: [PATCH 1/2] Added mini Integration Test --- .travis.yml | 3 + Tests/BaseTest.php | 86 +++++++++++++++++------ Tests/Entities/Test.php | 30 +++++++- Tests/IntegrationTest/IntegrationTest.php | 34 +++++++++ 4 files changed, 130 insertions(+), 23 deletions(-) create mode 100644 Tests/IntegrationTest/IntegrationTest.php diff --git a/.travis.yml b/.travis.yml index 6448861..3b14169 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,9 @@ language: php php: - 5.5 +addons: + postgresql: "9.4" + before_script: - psql -c 'create database jsonb_test;' -U postgres - composer install -n --no-interaction --prefer-source diff --git a/Tests/BaseTest.php b/Tests/BaseTest.php index d070168..7be1144 100644 --- a/Tests/BaseTest.php +++ b/Tests/BaseTest.php @@ -9,43 +9,89 @@ namespace Boldtrn\JsonbBundle\Tests; use Doctrine\Common\Cache\ArrayCache; +use \Doctrine\DBAL\Connection; +use Doctrine\DBAL\DriverManager; +use Doctrine\DBAL\Types\Type; use Doctrine\ORM\Configuration; +use Doctrine\ORM\EntityManager; +use Doctrine\ORM\Tools\SchemaTool; -abstract class BaseTest extends \PHPUnit_Framework_TestCase { +abstract class BaseTest extends \PHPUnit_Framework_TestCase +{ + + /** @var EntityManager */ + protected $entityManager; + + /** @var Connection */ + protected $connection; + + protected $dbParams = array( + 'driver' => 'pdo_pgsql', + 'host' => 'localhost', + 'port' => '5432', + 'dbname' => 'jsonb_test', + 'user' => 'postgres', + 'password' => 'secret', + ); + + protected $testEntityName = 'Boldtrn\JsonbBundle\Tests\Entities\Test'; - public $entityManager = null; protected function setUp() { + if (!class_exists('\Doctrine\ORM\Configuration')) { - $this->markTestSkipped('Doctrine is not available'); + static::markTestSkipped('Doctrine is not available'); } + $config = new Configuration(); $config->setMetadataCacheImpl(new ArrayCache()); $config->setQueryCacheImpl(new ArrayCache()); - $config->setProxyDir(__DIR__ . '/Proxies'); + $config->setProxyDir(__DIR__.'/Proxies'); $config->setProxyNamespace('Boldtrn\JsonbBundle\Tests\Proxies'); $config->setAutoGenerateProxyClasses(true); - $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver(__DIR__ . '/Entities')); + $config->setMetadataDriverImpl($config->newDefaultAnnotationDriver(__DIR__.'/Entities')); $config->addEntityNamespace('E', 'Boldtrn\JsonbBundle\Tests\Entities'); - $config->setCustomStringFunctions(array( - 'JSONB_AG' => 'Boldtrn\JsonbBundle\Query\JsonbAtGreater', - 'JSONB_HGG' => 'Boldtrn\JsonbBundle\Query\JsonbHashGreaterGreater', - 'JSONB_EX' => 'Boldtrn\JsonbBundle\Query\JsonbExistence', - )); - - $dbParams = array( - 'driver' => 'pdo_pgsql', - 'host' => 'localhost', - 'port' => '5432', - 'dbname' => 'jsonb_test', - 'user' => 'postgres', - 'password' => 'secret', + $config->setCustomStringFunctions( + array( + 'JSONB_AG' => 'Boldtrn\JsonbBundle\Query\JsonbAtGreater', + 'JSONB_HGG' => 'Boldtrn\JsonbBundle\Query\JsonbHashGreaterGreater', + 'JSONB_EX' => 'Boldtrn\JsonbBundle\Query\JsonbExistence', + ) ); - $this->entityManager = \Doctrine\ORM\EntityManager::create( - $dbParams, + + $this->entityManager = EntityManager::create( + $this->dbParams, $config ); + + $this->connection = $this->entityManager->getConnection(); + + $this->setUpDBALTypes(); + + $tool = new SchemaTool($this->entityManager); + $classes = $this->entityManager->getMetaDataFactory()->getAllMetaData(); + + + // Drop all classes and re-build them for each test case + $tool->dropSchema($classes); + $tool->createSchema($classes); + + } + + /** + * Configures DBAL types required in tests + */ + protected function setUpDBALTypes() + { + + if (Type::hasType('jsonb')) { + Type::overrideType('jsonb', 'Boldtrn\JsonbBundle\Types\JsonbArrayType'); + } else { + Type::addType('jsonb', 'Boldtrn\JsonbBundle\Types\JsonbArrayType'); + } + + $this->connection->getDatabasePlatform()->registerDoctrineTypeMapping('JSONB', 'jsonb'); } } \ No newline at end of file diff --git a/Tests/Entities/Test.php b/Tests/Entities/Test.php index d04aee4..86e20d2 100644 --- a/Tests/Entities/Test.php +++ b/Tests/Entities/Test.php @@ -8,7 +8,7 @@ use Doctrine\ORM\Mapping\Id; /** - * @Entity + * @Entity() */ class Test { @@ -18,11 +18,35 @@ class Test * @Column(type="string") * @GeneratedValue */ - public $id; + protected $id; /** * @Column(type="jsonb") */ - public $attrs; + protected $attrs = array(); + + /** + * @return mixed + */ + public function getId() + { + return $this->id; + } + + /** + * @return mixed + */ + public function getAttrs() + { + return $this->attrs; + } + + /** + * @param mixed $attrs + */ + public function setAttrs($attrs) + { + $this->attrs = $attrs; + } } \ No newline at end of file diff --git a/Tests/IntegrationTest/IntegrationTest.php b/Tests/IntegrationTest/IntegrationTest.php new file mode 100644 index 0000000..3140daa --- /dev/null +++ b/Tests/IntegrationTest/IntegrationTest.php @@ -0,0 +1,34 @@ +setAttrs(array('foo' => 'bar')); + + $this->entityManager->persist($test); + $this->entityManager->flush(); + + /** @var Test $retrievedTest */ + $retrievedTest = $this->entityManager->getRepository($this->testEntityName)->find($test->getId()); + + static::assertEquals($test->getAttrs(), $retrievedTest->getAttrs()); + + } + +} From 56c822f5e976cc0f85d03c4afbb6e10fd26d6c13 Mon Sep 17 00:00:00 2001 From: Robin Date: Thu, 22 Oct 2015 20:55:27 +0200 Subject: [PATCH 2/2] Added empty array Test --- Tests/IntegrationTest/IntegrationTest.php | 42 ++++++++++++++++++++--- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/Tests/IntegrationTest/IntegrationTest.php b/Tests/IntegrationTest/IntegrationTest.php index 3140daa..80ddada 100644 --- a/Tests/IntegrationTest/IntegrationTest.php +++ b/Tests/IntegrationTest/IntegrationTest.php @@ -18,11 +18,19 @@ class IntegrationTest extends BaseTest public function testSimpleInsertSelect() { - $test = new Test(); - $test->setAttrs(array('foo' => 'bar')); + $test = $this->createTest(array('foo' => 'bar')); - $this->entityManager->persist($test); - $this->entityManager->flush(); + /** @var Test $retrievedTest */ + $retrievedTest = $this->entityManager->getRepository($this->testEntityName)->find($test->getId()); + + static::assertEquals($test->getAttrs(), $retrievedTest->getAttrs()); + + } + + public function testEmptyArray() + { + + $test = $this->createTest(array()); /** @var Test $retrievedTest */ $retrievedTest = $this->entityManager->getRepository($this->testEntityName)->find($test->getId()); @@ -31,4 +39,30 @@ public function testSimpleInsertSelect() } + /** + * @param $attrs array the attributes of the jsonb array + * @return Test + */ + private function createTest($attrs) + { + $test = new Test(); + $test->setAttrs($attrs); + + $this->entityManager->persist($test); + $this->entityManager->flush(); + + return $test; + } + + private function clearTable() + { + + foreach ($this->entityManager->getRepository($this->testEntityName)->findAll() as $test) { + $this->entityManager->remove($test); + } + + $this->entityManager->flush(); + + } + }