Skip to content

Commit

Permalink
Add more testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateu Aguiló Bosch committed Jun 17, 2015
1 parent 21599c3 commit 31fd712
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
25 changes: 21 additions & 4 deletions src/AutoloaderBootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,8 @@ public function __construct(\Composer\Autoload\ClassLoader $loader, $seed = 'com
* Register the autoloader if it is not registered.
*/
public function register() {
if ($functions = spl_autoload_functions()) {
if (array_search(static::AUTOLOAD_FUNCTION, $functions)) {
return;
}
if ($this::checkLoadedAutoloader()) {
return;
}
// Parse the composer.json.
$composer_config = json_decode(file_get_contents(static::COMPOSER_CONFIGURATION_NAME));
Expand Down Expand Up @@ -114,4 +112,23 @@ protected function registerPsr($composer_config) {
Loader::registerPsr($this->loader);
}

/**
* Checks if the autoloader has been added.
*
* @return bool
*/
public static function checkLoadedAutoloader() {
$autoloader_str = ltrim(static::AUTOLOAD_FUNCTION, '\\');
$autoloader = explode('::', $autoloader_str);
foreach (spl_autoload_functions() as $callable) {
if (
($callable[0] == $autoloader[0] || $callable[0] == $autoloader[0]) &&
$callable[1] == $autoloader[1]
) {
return TRUE;
}
}
return FALSE;
}

}
11 changes: 8 additions & 3 deletions tests/src/AutoloaderBootstrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,21 @@ public function test___construct() {
/**
* Tests the ::register() method.
*
* @covers ::register()
* @covers ::load()
* @covers ::registerDrupalPaths()
* @covers ::checkLoadedAutoloader()
*/
public function test_register() {
$loader = m::mock('\Composer\Autoload\ClassLoader');
$autoloader = new AutoloaderBootstrap($loader, 'data/docroot/sites/all/modules/testmodule/composer.json');
$autoloader->register();
$functions = spl_autoload_functions();
$expected = [['Drupal\Composer\ClassLoader\Loader', 'autoload']];
$this->assertTrue(in_array($expected, $functions));

$this->assertTrue($autoloader::checkLoadedAutoloader());
// Make sure that calling to register a second time does not fail.
$autoloader->register();
$this->assertTrue($autoloader::checkLoadedAutoloader());
}


}

0 comments on commit 31fd712

Please sign in to comment.