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 24, 2019
1 parent 2abffee commit ae645f0
Show file tree
Hide file tree
Showing 3 changed files with 343 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,35 @@

namespace Doctrine\Tests\ORM\Functional\SchemaTool;

use Doctrine\DBAL\Configuration;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\Tests\OrmFunctionalTestCase;
use Doctrine\Tests\Models;
use function array_filter;
use function current;
use function method_exists;
use function sprintf;
use function strpos;

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');
$this->_em->getConnection()->exec('DROP TABLE IF EXISTS other_entity_to_remove');

parent::tearDown();
}

public function testGetCreateSchemaSql()
{
$classes = [
Expand Down Expand Up @@ -89,6 +105,89 @@ 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);
$sqls = $this->filterSqls($sqls, ['entity_to_remove']);
$this->assertCount(1, $sqls);
$this->assertContains('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);

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

$classes[] = $this->_em->getClassMetadata(MyOtherEntityToRemove::class);
$sqls = $tool->getUpdateSchemaSql($classes);
$sqls = $this->filterSqls($sqls, ['entity_to_remove', 'other_entity_to_remove']);
$this->assertCount(1, $sqls);
$this->assertContains('CREATE TABLE other_entity_to_remove (id INT AUTO_INCREMENT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB', $sqls);
}

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

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

if ($filterRegex && ! method_exists(Configuration::class, 'setFilterSchemaAssetsExpression')) {
$this->markTestSkipped(sprintf('Test require %s::setFilterSchemaAssetsExpression 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);
$sqls = $this->filterSqls($sqls, ['entity_to_remove']);
$this->assertCount(0, $sqls);

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

private function filterSqls(array $sqls, array $needles) : array
{
return array_filter($sqls, static function ($sql) use ($needles) {
foreach ($needles as $needle) {
if (strpos($sql, $needle) !== false) {
return true;
}
}
return false;
});
}
}

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

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

/**
* @Entity
* @Table(name="other_entity_to_remove")
*/
class MyOtherEntityToRemove
{
/**
* @Id @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
public $id;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

namespace Doctrine\Tests\ORM\Functional\SchemaTool;

use Doctrine\DBAL\Configuration;
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\Tests\Models;
use Doctrine\Tests\OrmFunctionalTestCase;
use function array_filter;
use function method_exists;
use function sprintf;
use function strpos;

class PostgreSqlSchemaToolTest extends OrmFunctionalTestCase
{
Expand All @@ -17,6 +22,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 +146,57 @@ public function testUpdateSchemaWithPostgreSQLSchema()

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

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

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

if ($filterRegex && ! method_exists(Configuration::class, 'setFilterSchemaAssetsExpression')) {
$this->markTestSkipped(sprintf('Test require %s::setFilterSchemaAssetsExpression 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, static 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 +265,16 @@ class DDC1657Avatar
*/
private $pk;
}

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

0 comments on commit ae645f0

Please sign in to comment.