1
0
mirror of https://github.com/danog/class-finder.git synced 2024-12-02 17:37:57 +01:00
class-finder/test/unit/Files/FilesEntryTest.php
2018-10-21 15:14:26 -05:00

95 lines
2.3 KiB
PHP

<?php
namespace HaydenPierce\ClassFinder\UnitTest\Files;
use HaydenPierce\ClassFinder\Files\FilesEntry;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
class FilesTest extends \PHPUnit_Framework_TestCase
{
private $file;
public function setUp()
{
/*
* A temporary file is used here due to the nature of the FilesEntry. Because FilesEntry must include a PHP file
* in a shell command, we would have to somehow include the vfs wrapper in that shell call. Since that would
* require a change to the class under test, it's probably just easier to use a temporary file.
*/
$this->file = tmpfile();
fwrite($this->file, <<<EOL
<?php
namespace Foo\Bar;
class Foo {}
class Bar {}
namespace Baz;
class Boo {}
EOL
);
}
/**
* @param $namespace
* @param $expected
* @dataProvider getClassesDataProvider
*/
public function testGetClasses($namespace, $expected)
{
$metaData = stream_get_meta_data($this->file);
$tmpFilename = $metaData['uri'];
$files = new FilesEntry($tmpFilename, $this->findPHP());
$classes = $files->getClasses($namespace);
$this->assertEquals($expected, $classes, 'FilesEntry should be able to determine the classes defined in a given file.');
}
public function getClassesDataProvider()
{
return array(
array(
'Foo\Bar',
array(
'Foo\Bar\Foo',
'Foo\Bar\Bar'
)
),
array(
'Baz',
array(
'Baz\Boo'
)
),
array(
'Stupid',
array()
)
);
}
private function findPHP()
{
if (defined("PHP_BINARY")) {
// PHP_BINARY was made available in PHP 5.4
$php = PHP_BINARY;
} else {
$isHostWindows = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
if ($isHostWindows) {
exec('where php', $output);
$php = $output[0];
} else {
exec('which php', $output);
$php = $output[0];
}
}
return $php;
}
}