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..80ddada --- /dev/null +++ b/Tests/IntegrationTest/IntegrationTest.php @@ -0,0 +1,68 @@ +createTest(array('foo' => 'bar')); + + /** @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()); + + static::assertEquals($test->getAttrs(), $retrievedTest->getAttrs()); + + } + + /** + * @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(); + + } + +}