Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
l-vo authored and nicolas-grekas committed Oct 23, 2019
1 parent 2abffee commit 562288a
Show file tree
Hide file tree
Showing 2 changed files with 159 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@

namespace Doctrine\Tests\ORM\Functional\SchemaTool;

use Doctrine\DBAL\Configuration;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\Tests\OrmFunctionalTestCase;
use Doctrine\Tests\Models;

class MySqlSchemaToolTest extends OrmFunctionalTestCase
{
protected function setUp() {
protected function setUp()
{
parent::setUp();

if ($this->_em->getConnection()->getDatabasePlatform()->getName() !== 'mysql') {
$this->markTestSkipped('The ' . __CLASS__ .' requires the use of mysql.');
}
}

protected function tearDown()
{
$this->_em->getConnection()->exec("DROP TABLE IF EXISTS entity_to_remove");

parent::tearDown();
}

public function testGetCreateSchemaSql()
{
$classes = [
Expand Down Expand Up @@ -89,6 +99,71 @@ public function testGetCreateSchemaSql4()
$this->assertEquals(0, count($sql));
}

public function testUpdateSchemaSql()
{
$classes = [
$this->_em->getClassMetadata(MyEntityToRemove::class),
];
$tool = new SchemaTool($this->_em);
$sqls = $tool->getUpdateSchemaSql($classes);
$this->assertCount(1, $sqls);
$this->assertEquals('CREATE TABLE entity_to_remove (id INT AUTO_INCREMENT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB', $sqls[0]);

$this->_em->getConnection()->exec($sqls[0]);
$sqls = $tool->getUpdateSchemaSql($classes);
$this->assertCount(0, $sqls);

$classes[] = $this->_em->getClassMetadata(Models\Generic\BooleanModel::class);
$sqls = $tool->getUpdateSchemaSql($classes);
$this->assertEquals('CREATE TABLE boolean_model (id INT AUTO_INCREMENT NOT NULL, booleanField TINYINT(1) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB', $sqls[0]);
}

public function provideUpdateSchemaSqlWithSchemaAssetFilter() : array
{
return [
['/^(?!entity_to_r)/', null],
[null, function ($assetName) : bool {
return $assetName !== 'entity_to_remove';
}]
];
}

/**
* @dataProvider provideUpdateSchemaSqlWithSchemaAssetFilter
*/
public function testUpdateSchemaSqlWithSchemaAssetFilter(?string $filterRegex, ?callable $filterCallback)
{
if ($filterRegex && !method_exists(Configuration::class, 'setFilterSchemaAssetsExpression')) {
$this->markTestSkipped(sprintf("Test require %s::setFilterSchemaAssetsExpression method", Configuration::class));
}

if ($filterCallback && !method_exists(Configuration::class, 'setSchemaAssetsFilter')) {
$this->markTestSkipped(sprintf("Test require %s::setSchemaAssetsFilter method", Configuration::class));
}

$classes = [
$this->_em->getClassMetadata(MyEntityToRemove::class)
];

$tool = new SchemaTool($this->_em);
$tool->createSchema($classes);

$config = $this->_em->getConnection()->getConfiguration();
if ($filterRegex) {
$config->setFilterSchemaAssetsExpression($filterRegex);
} else {
$config->setSchemaAssetsFilter($filterCallback);
}

$sqls = $tool->getUpdateSchemaSql($classes);
$this->assertCount(0, $sqls);

if ($filterRegex) {
$this->assertEquals($filterRegex, $config->getFilterSchemaAssetsExpression());
} else {
$this->assertSame($filterCallback, $config->getSchemaAssetsFilter());
}
}
}

/**
Expand All @@ -101,3 +176,16 @@ class MysqlSchemaNamespacedEntity
public $id;
}

/**
* @Entity
* @Table(name="entity_to_remove")
*/
class MyEntityToRemove
{
/**
* @Id @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
public $id;
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\Tests\Models;
use Doctrine\Tests\OrmFunctionalTestCase;
use Doctrine\DBAL\Configuration;

class PostgreSqlSchemaToolTest extends OrmFunctionalTestCase
{
Expand All @@ -17,6 +18,14 @@ protected function setUp()
}
}

protected function tearDown()
{
$this->_em->getConnection()->exec("DROP TABLE IF EXISTS pg_entity_to_remove");
$this->_em->getConnection()->exec("DROP SEQUENCE IF EXISTS pg_entity_to_remove_id_seq");

parent::tearDown();
}

public function testPostgresMetadataSequenceIncrementedBy10()
{
$address = $this->_em->getClassMetadata(Models\CMS\CmsAddress::class);
Expand Down Expand Up @@ -133,6 +142,54 @@ public function testUpdateSchemaWithPostgreSQLSchema()

$this->assertCount(0, $sql, implode("\n", $sql));
}

public function provideUpdateSchemaSqlWithSchemaAssetFilter() : array
{
return [
['/^(?!pg_entity_to_r)/', null],
[null, function ($assetName) : bool {
return $assetName !== 'pg_entity_to_remove';
}]
];
}

/**
* @dataProvider provideUpdateSchemaSqlWithSchemaAssetFilter
*/
public function testUpdateSchemaSqlWithSchemaAssetFilter(?string $filterRegex, ?callable $filterCallback)
{
if ($filterRegex && !method_exists(Configuration::class, 'setFilterSchemaAssetsExpression')) {
$this->markTestSkipped(sprintf("Test require %s::setFilterSchemaAssetsExpression method", Configuration::class));
}

if ($filterCallback && !method_exists(Configuration::class, 'setSchemaAssetsFilter')) {
$this->markTestSkipped(sprintf("Test require %s::setSchemaAssetsFilter method", Configuration::class));
}

$classes = [
$this->_em->getClassMetadata(PgMyEntityToRemove::class)
];

$tool = new SchemaTool($this->_em);
$tool->createSchema($classes);

$config = $this->_em->getConnection()->getConfiguration();
if ($filterRegex) {
$config->setFilterSchemaAssetsExpression($filterRegex);
} else {
$config->setSchemaAssetsFilter($filterCallback);
}

$sqls = $tool->getUpdateSchemaSql($classes);
$sqls = array_filter($sqls, function($sql) { return (strpos($sql, "pg_entity_to_remove") !== false); });
$this->assertCount(0, $sqls);

if ($filterRegex) {
$this->assertEquals($filterRegex, $config->getFilterSchemaAssetsExpression());
} else {
$this->assertSame($filterCallback, $config->getSchemaAssetsFilter());
}
}
}

/**
Expand Down Expand Up @@ -201,3 +258,16 @@ class DDC1657Avatar
*/
private $pk;
}

/**
* @Entity
* @Table(name="pg_entity_to_remove")
*/
class PgMyEntityToRemove
{
/**
* @Id @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
public $id;
}

0 comments on commit 562288a

Please sign in to comment.