Skip to content

Commit

Permalink
Merge pull request #6521 from GawainLynch/tests/extension-coverage
Browse files Browse the repository at this point in the history
[3.3][Tests] Extension coverage
  • Loading branch information
bobdenotter committed Mar 31, 2017
2 parents eb58bc8 + a18439a commit 6b5510c
Show file tree
Hide file tree
Showing 9 changed files with 435 additions and 113 deletions.
15 changes: 7 additions & 8 deletions tests/phpunit/unit/Extension/AbstractExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,24 @@ public function testContainer()
public function testBaseDirectory()
{
$app = $this->getApp();
$webDir = $app['filesystem']->getDir('extensions://');
$dir = new Directory();
$dir->setPath(__DIR__);
$baseDir = $app['filesystem']->getDir('extensions://foo/bar');
$ext = new BasicExtension();
$ext->setWebDirectory($webDir);

$this->assertInstanceOf(AbstractExtension::class, $ext->setBaseDirectory($dir));
$this->assertInstanceOf(AbstractExtension::class, $ext->setBaseDirectory($baseDir));
$this->assertInstanceOf(Directory::class, $ext->getBaseDirectory());
$this->assertSame(__DIR__, $ext->getBaseDirectory()->getPath());
$this->assertSame('extensions://foo/bar', $ext->getBaseDirectory()->getFullPath());
}

public function testRelativeUrl()
public function testWebDirectory()
{
$app = $this->getApp();
$webDir = new Directory($app['filesystem']->getFilesystem('extensions'));
$webDir = $app['filesystem']->getDir('web://extensions/foo/bar');
$ext = new BasicExtension();
$ext->setWebDirectory($webDir);

$this->assertInstanceOf(AbstractExtension::class, $ext->setWebDirectory($webDir));
$this->assertInstanceOf(Directory::class, $ext->getWebDirectory());
$this->assertSame('web://extensions/foo/bar', $ext->getWebDirectory()->getFullPath());
}

public function testGetId()
Expand Down
149 changes: 50 additions & 99 deletions tests/phpunit/unit/Extension/ConfigTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Bolt\Tests\Extension;

use Bolt\Filesystem\Adapter\Memory;
use Bolt\Filesystem\Filesystem;
use Bolt\Filesystem\Handler\DirectoryInterface;
use Bolt\Filesystem\Handler\YamlFile;
use Bolt\Tests\BoltUnitTest;
use Bolt\Tests\Extension\Mock\ConfigExtension;
Expand All @@ -14,132 +17,77 @@
*/
class ConfigTraitTest extends BoltUnitTest
{
/** @var DirectoryInterface */
private $extDir;
/** @var DirectoryInterface */
private $extConfigDir;

protected function setUp()
{
parent::setUp();
$this->resetDirs();

$app = $this->getApp();
$filesystem = $app['filesystem'];
$filesystem->createDir('extensions://local/bolt/config/config');
$filesystem->createDir('config://extensions');
$file = new YamlFile();
$filesystem->getFile('extensions://local/bolt/config/config/config.yml.dist', $file);
$this->extDir = (new Filesystem(new Memory()))->getDir('/');
$this->extConfigDir = (new Filesystem(new Memory()))->getDir('/');

/** @var YamlFile $file */
$file = $this->extDir->getFile('config/config.yml.dist', new YamlFile());
$file->dump(['blame' => 'drop bear']);
}

protected function tearDown()
protected function makeApp()
{
parent::tearDown();
$this->resetDirs();
}
$app = parent::makeApp();

private function resetDirs()
{
$app = $this->getApp();
$filesystem = $app['filesystem'];
if ($filesystem->has('extensions://local/bolt/config/config')) {
$filesystem->deleteDir('extensions://local/bolt/config/config');
}
if ($filesystem->has('config://extensions')) {
$filesystem->deleteDir('config://extensions');
}
$app['filesystem']->mountFilesystem('extensions_config', $this->extConfigDir->getFilesystem());

return $app;
}

public function testDefaultConfigNoOverride()
{
$app = $this->getApp();

$ext = new NormalExtension();
$ext->setContainer($app);
$refObj = new \ReflectionObject($ext);
$method = $refObj->getMethod('getDefaultConfig');
$method->setAccessible(true);

$this->assertSame([], $method->invoke($ext));
$result = $this->invoke($ext, 'getDefaultConfig');

$this->assertSame([], $result);
}

public function testDefaultConfig()
{
$app = $this->getApp();
$ext = $this->createExt();

$ext = new ConfigExtension();
$ext->setContainer($app);
$refObj = new \ReflectionObject($ext);
$method = $refObj->getMethod('getDefaultConfig');
$method->setAccessible(true);
$result = $this->invoke($ext, 'getDefaultConfig');

$this->assertSame(['blame' => 'koala'], $method->invoke($ext));
$this->assertSame(['blame' => 'koala'], $result);
}

public function testInstallConfigFile()
{
$app = $this->getApp();
$ext = new ConfigExtension();
$baseDir = $app['filesystem']->getDir('extensions://');
$baseDir->setPath('local/bolt/config');
$ext->setBaseDirectory($baseDir);
$webDir = $app['filesystem']->getDir('extensions://');
$ext->setWebDirectory($webDir);

$refObj = new \ReflectionObject($ext);
$method = $refObj->getMethod('getConfig');
$method->setAccessible(true);

$ext->setContainer($app);
$ext = $this->createExt();

$conf = $method->invoke($ext);
$conf = $this->invoke($ext, 'getConfig');
$this->assertSame(['blame' => 'drop bear'], $conf);

// Second call should match
$conf = $method->invoke($ext);
$conf = $this->invoke($ext, 'getConfig');
$this->assertSame(['blame' => 'drop bear'], $conf);
}

public function testInstallConfigFileFailure()
{
$app = $this->getApp();
$ext = new ConfigExtension();
$baseDir = $app['filesystem']->getDir('extensions://');
$baseDir->setPath('local/bolt/config');
$ext->setBaseDirectory($baseDir);
$webDir = $app['filesystem']->getDir('extensions://');
$ext->setWebDirectory($webDir);

$refObj = new \ReflectionObject($ext);
$method = $refObj->getMethod('getConfig');
$method->setAccessible(true);
$this->extDir->getFile('config/config.yml.dist')->delete();

$ext->setContainer($app);
$ext->register($app);
$filesystem = $app['filesystem'];
$filesystem->delete('extensions://local/bolt/config/config/config.yml.dist');
$conf = $this->invoke($this->createExt(), 'getConfig');

$conf = $method->invoke($ext);
$this->assertSame(['blame' => 'koala'], $conf);
}

public function testConfigFileLocalOverRide()
{
$app = $this->getApp();
$ext = new ConfigExtension();
$baseDir = $app['filesystem']->getDir('extensions://');
$baseDir->setPath('local/bolt/config');
$ext->setBaseDirectory($baseDir);
$webDir = $app['filesystem']->getDir('extensions://');
$ext->setWebDirectory($webDir);

$refObj = new \ReflectionObject($ext);
$method = $refObj->getMethod('getConfig');
$method->setAccessible(true);

$ext->setContainer($app);
$filesystem = $app['filesystem'];
$file = new YamlFile();
$filesystem->getFile('config://extensions/config.bolt_local.yml', $file);
/** @var YamlFile $file */
$file = $this->extConfigDir->getFile('config.bolt_local.yml');
$file->dump(['blame' => 'gnomes']);

$conf = $method->invoke($ext);
$conf = $this->invoke($this->createExt(), 'getConfig');

$this->assertSame(['blame' => 'gnomes'], $conf);
}

Expand All @@ -149,24 +97,27 @@ public function testConfigFileLocalOverRide()
*/
public function testConfigFileInvalidYaml()
{
$app = $this->getApp();
$filesystem = $app['filesystem'];
$file = new YamlFile();
$filesystem->getFile('config://extensions/config.bolt.yml', $file);
$file->put("\tever so slightly invalid yaml");
$this->extConfigDir->getFile('config.bolt.yml')->put("\tever so slightly invalid yaml");

$this->invoke($this->createExt(), 'getConfig');
}

protected function createExt()
{
$ext = new ConfigExtension();
$dir = $app['filesystem']->getDir('extensions://');
$dir->setPath('local/bolt/config');
$ext->setBaseDirectory($dir);
$ext->setContainer($this->getApp());
$ext->setBaseDirectory($this->extDir);
$ext->register($this->getApp());

$refObj = new \ReflectionObject($ext);
$method = $refObj->getMethod('getConfig');
$method->setAccessible(true);
return $ext;
}

$ext->setContainer($app);
protected function invoke($object, $method, array $args = [])
{
$refObj = new \ReflectionObject($object);
$method = $refObj->getMethod($method);
$method->setAccessible(true);

$conf = $method->invoke($ext);
$this->assertSame(['blame' => 'gnomes'], $conf);
return $method->invokeArgs($object, $args);
}
}
39 changes: 39 additions & 0 deletions tests/phpunit/unit/Extension/DatabaseSchemaTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Bolt\Tests\Extension;

use Bolt\Storage\Database\Schema\Table\BaseTable;
use Bolt\Tests\BoltUnitTest;
use Bolt\Tests\Extension\Mock\DatabaseSchemaExtension;

/**
* Class to test Bolt\Extension\DatabaseSchemaTrait
*
* @author Gawain Lynch <gawain.lynch@gmail.com>
*/
class DatabaseSchemaTraitTest extends BoltUnitTest
{
public function testDatabaseSchemaExtension()
{
$app = $this->getApp();
$ext = new DatabaseSchemaExtension();
$ext->setContainer($app);
$ext->register($app);
$app->boot();
$this->addToAssertionCount(1);
}

public function testRegisterTable()
{
$app = $this->getApp();
$ext = new DatabaseSchemaExtension();
$ext->setContainer($app);
$ext->register($app);

$extensionTableNames = $app['schema.extension_tables']->keys();
$this->assertSame('round_table', reset($extensionTableNames));
$table = $app['schema.extension_tables']['round_table'];
$this->assertInstanceOf(BaseTable::class, $table);
$this->assertInstanceOf(Mock\ExtensionTable::class, $table);
}
}
6 changes: 2 additions & 4 deletions tests/phpunit/unit/Extension/MenuTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public function testEmptyMenus()
{
$app = $this->getApp();
$ext = new NormalExtension();
$baseDir = $app['filesystem']->getDir('extensions://');
$baseDir->setPath('local/bolt/menu');
$baseDir = $app['filesystem']->getDir('extensions://local/bolt/menu');
$ext->setBaseDirectory($baseDir);
$ext->setContainer($app);
$ext->register($app);
Expand All @@ -36,8 +35,7 @@ public function testLegacyMenuAdds()
$app = $this->getApp();

$ext = new MenuExtension();
$baseDir = $app['filesystem']->getDir('extensions://');
$baseDir->setPath('local/bolt/menu');
$baseDir = $app['filesystem']->getDir('extensions://local/bolt/menu');
$ext->setBaseDirectory($baseDir);
$ext->setContainer($app);
$ext->register($app);
Expand Down
35 changes: 35 additions & 0 deletions tests/phpunit/unit/Extension/Mock/DatabaseSchemaExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Bolt\Tests\Extension\Mock;

use Bolt\Extension\DatabaseSchemaTrait;
use Bolt\Extension\SimpleExtension;
use Silex\Application;

/**
* Mock extension that extends SimpleExtension for testing the DatabaseSchemaTrait.
*
* @author Gawain Lynch <gawain.lynch@gmail.com>
*/
class DatabaseSchemaExtension extends SimpleExtension
{
use DatabaseSchemaTrait;

/**
* {@inheritdoc}
*/
protected function registerServices(Application $app)
{
$this->extendDatabaseSchemaServices();
}

/**
* {@inheritdoc}
*/
protected function registerExtensionTables()
{
return [
'round_table' => ExtensionTable::class
];
}
}
34 changes: 34 additions & 0 deletions tests/phpunit/unit/Extension/Mock/ExtensionTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Bolt\Tests\Extension\Mock;

use Bolt\Storage\Database\Schema\Table\BaseTable;

class ExtensionTable extends BaseTable
{
/**
* {@inheritdoc}
*/
protected function addColumns()
{
$this->table->addColumn('id', 'integer', ['autoincrement' => true]);
$this->table->addColumn('name', 'string', ['length' => 42]);
$this->table->addColumn('updated', 'datetime', []);
}

/**
* {@inheritdoc}
*/
protected function addIndexes()
{
$this->table->addIndex(['name']);
}

/**
* {@inheritdoc}
*/
protected function setPrimaryKey()
{
$this->table->setPrimaryKey(['id']);
}
}
Loading

0 comments on commit 6b5510c

Please sign in to comment.