Skip to content

Commit

Permalink
Merge pull request concretecms#5180 from KorvinSzanto/feature/even-fa…
Browse files Browse the repository at this point in the history
…ster-tests

Speed up tests even more
  • Loading branch information
aembler committed Mar 7, 2017
2 parents 5b8665b + bc66f62 commit bb65942
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 31 deletions.
52 changes: 33 additions & 19 deletions tests/tests/ConcreteDatabaseTestCase.php
Expand Up @@ -17,6 +17,9 @@ class ConcreteDatabaseTestCase extends PHPUnit_Extensions_Database_TestCase
/** @var bool[] Keys are tables that currently exist */
public static $existingTables = [];

/** @var bool[] Keys are entites that currently exist */
public static $existingEntites = [];

/** @var array[] Table data cache */
public static $tableData = [];

Expand Down Expand Up @@ -94,7 +97,6 @@ public static function tearDownAfterClass()
// Make sure tables are removed
$testCase = new Static();
$testCase->removeTables();
$testCase->removeMetadatas();

// Call parent teardown
parent::tearDownAfterClass();
Expand Down Expand Up @@ -138,9 +140,7 @@ protected function removeTables()

// Get all existing tables
$tables = $connection->query('show tables')->fetchAll();
$tables = array_map(function ($table) {
return array_shift($table);
}, $tables);
$tables = array_map('array_shift', $tables);

// Turn off foreign key checks
$connection->exec('SET FOREIGN_KEY_CHECKS = 0');
Expand All @@ -153,7 +153,9 @@ protected function removeTables()
// Reset foreign key checks on
$connection->exec('SET FOREIGN_KEY_CHECKS = 1');

// Clear exists cache
static::$existingTables = [];
static::$existingEntites = [];
}

/**
Expand Down Expand Up @@ -181,14 +183,22 @@ protected function extractTableData(array $tables)
$name = (string)$table['name'];

// If this table is being requested
if (isset(static::$existingTables[$name]) || in_array($name, $tables, false)) {
if (in_array($name, $tables, false)) {
$this->appendXML($partial, $table);

// Store the fact that this table should exist now
static::$existingTables[$name] = true;
// Remove the table from our list of tables
$tables = array_filter($tables, function($name) use ($table) {
return $name !== $table;
});

// Track that we actually have tables to e
// Track that we actually have tables to import
$importedTables[] = $name;

static::$existingTables[$name] = true;
}

if (!$tables) {
break;
}
}

Expand Down Expand Up @@ -244,22 +254,14 @@ protected function importMetadatas()
}
}

protected function removeMetadatas()
{
$sm = Application::make(DatabaseStructureManager::class);

if ($metadatas = $this->getMetadatas()) {
$sm->uninstallDatabaseFor($metadatas);
}
}

/**
* Gets the metadatas to import
* @return array
*/
protected function getMetadatas()
{
$metadatas = [];
$install = $this->metadatas;

// If there are metadatas to import
if ($this->metadatas && is_array($this->metadatas)) {
Expand All @@ -269,8 +271,21 @@ protected function getMetadatas()

// Loop through all metadata
foreach ($factory->getAllMetadata() as $meta) {
if (in_array($meta->getName(), $this->metadatas, false)) {
if (!isset(self::$existingEntites[$meta->getName()]) && in_array($meta->getName(), $install, false)) {
$metadatas[] = $meta;

// Remove this from the list of entities to install
$install = array_filter($install, function($name) use ($meta) {
return $name !== $meta->getName();
});

// Track that we've created this metadata
self::$existingEntites[$meta->getName()] = true;
}

// If no more entities to install, lets break
if (!$install) {
break;
}
}
}
Expand All @@ -281,7 +296,6 @@ protected function getMetadatas()
public function tearDown()
{
parent::tearDown();
;
\ORM::entityManager('core')->clear();
\CacheLocal::flush();
}
Expand Down
15 changes: 10 additions & 5 deletions tests/tests/Core/File/FileListTest.php
Expand Up @@ -42,9 +42,9 @@ public function __construct($name = null, array $data = array(), $dataName = '')
));
}

protected function setUp()
public static function setUpBeforeClass()
{
parent::setUp();
parent::setUpBeforeClass(); // TODO: Change the autogenerated stub

$files = \Core::make(Filesystem::class);
$files->create();
Expand All @@ -57,8 +57,9 @@ protected function setUp()
FileKey::add($number, array('akHandle' => 'width', 'akName' => 'Width'));
FileKey::add($number, array('akHandle' => 'height', 'akName' => 'Height'));

mkdir($this->getStorageDirectory());
$this->getStorageLocation();
$self = new static();
mkdir($self->getStorageDirectory());
$self->getStorageLocation();

$sample = dirname(__FILE__) . '/StorageLocation/fixtures/sample.txt';
$image = DIR_BASE . '/concrete/images/logo.png';
Expand All @@ -81,6 +82,11 @@ protected function setUp()
foreach ($files as $filename => $pointer) {
$fi->import($pointer, $filename);
}
}

protected function setUp()
{
parent::setUp();

$this->list = new \Concrete\Core\File\FileList();
$this->list->ignorePermissions();
Expand All @@ -96,7 +102,6 @@ protected function cleanup()
unlink(dirname(__FILE__) . '/test.invalid');
}

$this->truncateTables();
}

public function testGetPaginationObject()
Expand Down
14 changes: 7 additions & 7 deletions tests/tests/Core/File/ImporterTest.php
Expand Up @@ -237,12 +237,12 @@ public function testImporterMimeType()
$fi = new Importer();
$fo1 = $fi->import($sample, 'sample.txt');

$sample = dirname(__FILE__) . '/StorageLocation/fixtures/gummies.jpg';
$sample = dirname(__FILE__) . '/StorageLocation/fixtures/tiny.png';
$fi = new Importer();
$fo2 = $fi->import($sample, 'gummies.jpg');
$fo2 = $fi->import($sample, 'tiny.png');

$this->assertEquals('text/plain', $fo1->getMimeType());
$this->assertEquals('image/jpeg', $fo2->getMimeType());
$this->assertEquals('image/png', $fo2->getMimeType());
}

public function testFileDuplicate()
Expand All @@ -269,16 +269,16 @@ public function testFileAttributesDuplicate()
mkdir($this->getStorageDirectory());
$this->getStorageLocation();

$sample = dirname(__FILE__) . '/StorageLocation/fixtures/gummies.jpg';
$sample = dirname(__FILE__) . '/StorageLocation/fixtures/tiny.png';
$fi = new Importer();
$f = $fi->import($sample, 'gummies.jpg');
$f = $fi->import($sample, 'tiny.png');

$f2 = $f->duplicate();

$attributes = $f->getAttributes();
$attributesNew = $f2->getAttributes();
$this->assertEquals(2, count($attributes));
$this->assertEquals(2, count($attributesNew));
$this->assertCount(2, $attributes);
$this->assertCount(2, $attributesNew);
}

public function testFileVersionDuplicate()
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/tests/bootstrap.php
Expand Up @@ -71,6 +71,7 @@
$config->set('concrete.cache.blocks', false);
$config->set('concrete.cache.pages', false);
$config->set('concrete.cache.enabled', false);
$config->set('concrete.user.password.hash_cost_log2', 1);

/** @var Concrete\Core\Database\Connection\Connection $cn */
$cn = $cms->make('database')->connection('travisWithoutDB');
Expand Down

0 comments on commit bb65942

Please sign in to comment.