Skip to content

Commit

Permalink
Updated Unit Tests
Browse files Browse the repository at this point in the history
Changes:
- Replaced ‘cache/void-adapter’ with ‘tedivm/stash’;
- Cleaned up unit test bootstrap;
- Added ContainerProvider, ContainerIntegrationTrait;
- Refactored all unit tests to use the ContainerIntegrationTrait;
- Removed dependencies on ‘admin/authentication’ and ‘admin/authorizer’, to be handled by concrete implements;
  • Loading branch information
mcaskill committed Feb 24, 2017
1 parent 9e5b691 commit a7e859d
Show file tree
Hide file tree
Showing 16 changed files with 493 additions and 193 deletions.
11 changes: 10 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}
],
"minimum-stability": "dev",
"prefer-stable":true,
"prefer-stable": true,
"extra": {
"branch-alias": {
"dev-master": "0.1.x-dev"
Expand All @@ -35,6 +35,7 @@
"phpunit/phpunit": "^4.8",
"squizlabs/php_codesniffer": "^2.4",
"satooshi/php-coveralls": "~1.0.1",
"tedivm/stash": "~0.14",
"pimple/pimple": "^3.0"
},
"suggest": {
Expand All @@ -45,8 +46,16 @@
"Charcoal\\": "src/Charcoal/"
}
},
"autoload-dev": {
"psr-4": {
"Charcoal\\Tests\\": "tests/Charcoal"
}
},
"scripts": {
"test": [
"@tests"
],
"tests": [
"@phplint",
"@phpcs",
"@phpunit"
Expand Down
1 change: 0 additions & 1 deletion src/Charcoal/Ui/AbstractUiItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public function __construct(array $data = null)
public function setDependencies(Container $container)
{
$this->setTranslator($container['translator']);
$this->setAuthDependencies($container);
}

/**
Expand Down
28 changes: 23 additions & 5 deletions tests/Charcoal/Ui/AbstractUiItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,38 @@

namespace Charcoal\Tests\Ui;

use Pimple\Container;
use ReflectionClass;

class AbstractUiTest extends \PHPUnit_Framework_TestCase
// From 'charcoal-ui'
use Charcoal\Ui\AbstractUiItem;

/**
*
*/
class AbstractUiItemTest extends \PHPUnit_Framework_TestCase
{
use \Charcoal\Tests\Ui\ContainerIntegrationTrait;

public $obj;

public function setUp()
{
$container = new Container;
$container['translator'] = $GLOBALS['translator'];
$container = $this->getContainer();

$this->obj = $this->getMockForAbstractClass('\Charcoal\Ui\AbstractUiItem', [[
$this->obj = $this->getMockForAbstractClass(AbstractUiItem::class, [[
'container' => $container
]]);

$setAuthDependencies = self::getMethod($this->obj, 'setAuthDependencies');
$setAuthDependencies->invoke($this->obj, $container);
}

public static function getMethod($obj, $name)
{
$class = new ReflectionClass($obj);
$method = $class->getMethod($name);
$method->setAccessible(true);
return $method;
}

public function testSetType()
Expand Down
45 changes: 45 additions & 0 deletions tests/Charcoal/Ui/ContainerIntegrationTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Charcoal\Tests\Ui;

// From Pimple
use Pimple\Container;

// From 'charcoal-core/tests'
use Charcoal\Tests\Ui\ContainerProvider;

/**
* Integrates Charcoal's service container into PHPUnit.
*
* Ensures Charcoal framework is set-up for each test.
*/
trait ContainerIntegrationTrait
{
/**
* @var Container
*/
private $container;

/**
* @see ContainerProvider
* @return Container
*/
private function getContainer()
{
if ($this->container === null) {
$provider = new ContainerProvider();
$container = new Container();

$provider->registerBaseServices($container);
$provider->registerAuthServices($container);
$provider->registerTranslator($container);
$provider->registerMetadataLoader($container);
$provider->registerSourceFactory($container);
$provider->registerPropertyFactory($container);

$this->container = $container;
}

return $this->container;
}
}
267 changes: 267 additions & 0 deletions tests/Charcoal/Ui/ContainerProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
<?php

namespace Charcoal\Tests\Ui;

use PDO;

// From PSR-3
use Psr\Log\NullLogger;

// From 'cache/void-adapter' (PSR-6)
use Cache\Adapter\Void\VoidCachePool;

// From 'tedivm/stash' (PSR-6)
use Stash\Pool;
use Stash\Driver\Ephemeral;

// From 'zendframework/zend-permissions-acl'
use Zend\Permissions\Acl\Acl;

// From Pimple
use Pimple\Container;

// From 'charcoal-factory'
use Charcoal\Factory\GenericFactory as Factory;

// From 'charcoal-core'
use Charcoal\Model\Service\MetadataLoader;
use Charcoal\Loader\CollectionLoader;
use Charcoal\Source\DatabaseSource;

// From 'charcoal-user'
use Charcoal\User\Authenticator;
use Charcoal\User\Authorizer;

// From 'charcoal-translator'
use Charcoal\Translator\LocalesManager;
use Charcoal\Translator\Translator;

/**
* Service Container for Unit Tests
*/
class ContainerProvider
{
/**
* Register the unit tests required services.
*
* @param Container $container A DI container.
* @return void
*/
public function registerBaseServices(Container $container)
{
$this->registerSource($container);
$this->registerLogger($container);
$this->registerCache($container);
}

/**
* Register the admin services.
*
* @param Container $container A DI container.
* @return void
*/
public function registerAuthServices(Container $container)
{
$this->registerAuthenticator($container);
$this->registerAuthorizer($container);
}

/**
* Setup the application's data source interface.
*
* Note: Uses SQLite to create a database in memory.
*
* @param Container $container A DI container.
* @return void
*/
public function registerSource(Container $container)
{
$container['database'] = function (Container $container) {
$pdo = new PDO('sqlite::memory:');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
};
}

/**
* Setup the application's logging interface.
*
* @param Container $container A DI container.
* @return void
*/
public function registerLogger(Container $container)
{
$container['logger'] = function (Container $container) {
return new NullLogger();
};
}

/**
* Setup the application's caching interface.
*
* @param Container $container A DI container.
* @return void
*/
public function registerCache(Container $container)
{
$container['cache'] = function ($container) {
return new Pool(new Ephemeral());
};
}

/**
* Setup the application's translator service.
*
* @param Container $container A DI container.
* @return void
*/
public function registerTranslator(Container $container)
{
$container['language/manager'] = function (Container $container) {
return new LocalesManager([
'locales' => [
'en' => [ 'locale' => 'en-US' ]
]
]);
};

$container['translator'] = function (Container $container) {
return new Translator([
'manager' => $container['language/manager']
]);
};
}

/**
* Setup the framework's metadata loader interface.
*
* @param Container $container A DI container.
* @return void
*/
public function registerMetadataLoader(Container $container)
{
$container['metadata/loader'] = function (Container $container) {
return new MetadataLoader([
'cache' => $container['cache'],
'logger' => $container['logger'],
'base_path' => realpath(__DIR__.'/../../../'),
'paths' => [
'metadata',
'vendor/locomotivemtl/charcoal-property/metadata'
]
]);
};
}

/**
* Setup the framework's data source factory.
*
* @param Container $container A DI container.
* @return void
*/
public function registerSourceFactory(Container $container)
{
$container['source/factory'] = function ($container) {
return new Factory([
'map' => [
'database' => DatabaseSource::class
],
'arguments' => [[
'logger' => $container['logger'],
'cache' => $container['cache'],
'pdo' => $container['database']
]]
]);
};
}

/**
* Setup the framework's model factory.
*
* @param Container $container A DI container.
* @return void
*/
public function registerModelFactory(Container $container)
{
$container['model/factory'] = function ($container) {
return new Factory([
'arguments' => [[
'container' => $container,
'logger' => $container['logger'],
'metadata_loader' => $container['metadata/loader'],
'source_factory' => $container['source/factory'],
'property_factory' => $container['property/factory']
]]
]);
};
}

/**
* Setup the framework's property factory.
*
* @param Container $container A DI container.
* @return void
*/
public function registerPropertyFactory(Container $container)
{
$container['property/factory'] = function (Container $container) {
return new Factory([
'resolver_options' => [
'prefix' => '\\Charcoal\\Property\\',
'suffix' => 'Property'
],
'arguments' => [[
'container' => $container,
'database' => $container['database'],
'logger' => $container['logger'],
'translator' => $container['translator']
]]
]);
};
}

/**
* Setup the framework's collection loader interface.
*
* @param Container $container A DI container.
* @return void
*/
public function registerModelCollectionLoader(Container $container)
{
$container['model/collection/loader'] = function (Container $container) {
return new CollectionLoader([
'logger' => $container['logger'],
'cache' => $container['cache']
]);
};
}

public function registerAuthenticator(Container $container)
{
$this->registerLogger($container);
$this->registerModelFactory($container);

$container['authenticator'] = function (Container $container) {
return new Authenticator([
'logger' => $container['logger'],
'user_type' => 'charcoal/user',
'user_factory' => $container['model/factory'],
'token_type' => 'charcoal/user/auth-token',
'token_factory' => $container['model/factory']
]);
};
}

public function registerAuthorizer(Container $container)
{
$this->registerLogger($container);

$container['authorizer'] = function (Container $container) {
return new Authorizer([
'logger' => $container['logger'],
'acl' => new Acl(),
'resource' => 'test'
]);
};
}
}
Loading

0 comments on commit a7e859d

Please sign in to comment.