Skip to content

Commit

Permalink
FactoriesFinder ignores files without extension now.
Browse files Browse the repository at this point in the history
  • Loading branch information
HRcc committed Apr 22, 2015
1 parent 73f0933 commit d3cde43
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
37 changes: 29 additions & 8 deletions spec/Laracasts/TestDummy/FactoriesFinderSpec.php
Expand Up @@ -7,9 +7,13 @@

class FactoriesFinderSpec extends ObjectBehavior
{
private $stubDir;

function let()
{
$this->beConstructedWith(__DIR__.'/helpers');
$this->stubDir = __DIR__.'/helpers/';

$this->beConstructedWith($this->stubDir);
}

function it_is_initializable()
Expand All @@ -19,20 +23,37 @@ function it_is_initializable()

function it_hunts_down_the_fixtures_file_like_a_dog()
{
$this->find()->shouldBe([__DIR__.'/helpers/all.php']);
$this->find()->shouldBe([$this->stubDir.'all.php']);
}

function it_ignores_non_php_files()
{
$dir = __DIR__.'/helpers';
$notPhpFile = $dir .'/foo.txt';
$notPhpFile = $this->createFile('foo.txt');

// We'll create a file that should not be included...
file_put_contents($notPhpFile, '');

$this->find()->shouldBe([$dir.'/all.php']);
$this->find()->shouldBe([$this->stubDir.'all.php']);

unlink($notPhpFile);
}

function it_ignores_files_without_extension()
{
$fileWithoutExtension = $this->createFile('foo');

$this->find()->shouldBe([$this->stubDir.'all.php']);

unlink($fileWithoutExtension);
}

private function createFile($filename, $dir = null, $content = '')
{
if ($dir === null) {
$dir = $this->stubDir;
}

$file = $dir . $filename;

file_put_contents($file, $content);

return $file;
}
}
15 changes: 12 additions & 3 deletions src/FactoriesFinder.php
Expand Up @@ -35,9 +35,7 @@ public function find()
$files = [];

foreach ($this->getDirectoryIterator() as $file) {
$extension = pathinfo($file)['extension'];

if ($extension !== 'php') continue;
if ($this->getExtension($file) !== 'php') continue;

$files[] = $file->getPathname();
}
Expand All @@ -58,4 +56,15 @@ private function getDirectoryIterator()
return new RecursiveIteratorIterator($directoryIterator);
}

/**
* Get the extension of a file.
*
* @param $file
* @return string|null
*/
private function getExtension($file)
{
return isset(pathinfo($file)['extension']) ? pathinfo($file)['extension'] : null;
}

}

0 comments on commit d3cde43

Please sign in to comment.