-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
16 changed files
with
493 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
]); | ||
}; | ||
} | ||
} |
Oops, something went wrong.