Skip to content

Commit

Permalink
Merge pull request #6 from boldtrn/add_tests
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
boldtrn committed Oct 22, 2015
2 parents c8b191e + eb8722a commit df76656
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 23 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
86 changes: 66 additions & 20 deletions Tests/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

}
30 changes: 27 additions & 3 deletions Tests/Entities/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Doctrine\ORM\Mapping\Id;

/**
* @Entity
* @Entity()
*/
class Test
{
Expand All @@ -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;
}

}
68 changes: 68 additions & 0 deletions Tests/IntegrationTest/IntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Created by PhpStorm.
* User: robin
* Date: 14.04.15
* Time: 14:11
*/

namespace Boldtrn\JsonbBundle\Tests\IntegrationTest;

use Boldtrn\JsonbBundle\Tests\BaseTest;
use Boldtrn\JsonbBundle\Tests\Entities\Test;

class IntegrationTest extends BaseTest
{


public function testSimpleInsertSelect()
{

$test = $this->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();

}

}

0 comments on commit df76656

Please sign in to comment.