Skip to content

Commit

Permalink
fixed #5 on conflicting separator rules for a new and old version of …
Browse files Browse the repository at this point in the history
…a framework, the first file was included twice when both versions have been tried to load

 - added: isFileAlreadyIncluded check
 - added: UnitTest for this case
  • Loading branch information
Mike Gladysch committed Apr 18, 2016
1 parent 3fe36ad commit 2b7decc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/glady/Behind/ClassLoader/ClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ private function tryToLoadClassByRule(array $state, $className, array $rule)

$state[self::LOAD_STATE_FILE_NAME] = $fileName;

if ($fileName !== null && $this->fileExists($fileName)) {
if ($fileName !== null && !$this->isFileAlreadyIncluded($fileName) && $this->fileExists($fileName)) {
$this->fire(self::ON_BEFORE_REQUIRE, $state);

if (!$this->classExists($className) && !$this->isFileAlreadyIncluded($fileName)) {
Expand Down
17 changes: 17 additions & 0 deletions src/glady/Behind/ClassLoader/test/ClassLoaderBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,23 @@ protected function thenClassShouldBeDeclared($className)
{
$this->assertTrue(in_array($className, $this->classLoader->loadedClasses));
}


protected function thenNoFileIsIncludedTwice()
{
$files = array();
foreach ($this->classLoader->_eventsFired[ClassLoader::ON_BEFORE_REQUIRE] as $event) {
$file = $event[ClassLoader::LOAD_STATE_FILE_NAME];

$files[$file] = isset($files[$file]) ? $files[$file] + 1 : 1;
}

$files = array_filter($files, function ($count) {
return $count > 1;
});

$this->assertEquals(array(), $files);
}
}


Expand Down
16 changes: 16 additions & 0 deletions src/glady/Behind/ClassLoader/test/ClassLoaderBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,20 @@ public function testWrongClassNameAddRuleAndLoadAgain()
$this->thenClassShouldBeDeclared('TestFolder_TestClass');
$this->thenClassShouldBeDeclared('TestFolder_TestClassInvalid');
}


public function testIssue5()
{
$this->givenIHaveAClassLoader();
$this->givenIHaveAPhpFile_ThatContainsClasses('/OldVersion/Framework/Package/Feature.php', array('Framework_Package_Feature'));
$this->givenIHaveAPhpFile_ThatContainsClasses('/NewVersion/Framework/Package/Feature.php', array('Framework\\Package\\Feature'));

$this->givenIHaveASeparatorRuleWith_AndFixedNamespace_OnDirectory_AsSeparatorOnDirectory('_', 'Framework', '/OldVersion/Framework', null);
$this->givenIHaveASeparatorRuleWith_AndFixedNamespace_OnDirectory_AsSeparatorOnDirectory('\\', 'Framework', '/NewVersion/Framework', null);

$this->whenITryToLoadExistingClass('Framework_Package_Feature');
$this->whenITryToLoadExistingClass('Framework\\Package\\Feature');

$this->thenNoFileIsIncludedTwice();
}
}

0 comments on commit 2b7decc

Please sign in to comment.