Skip to content

Commit

Permalink
Merge 580f58f into d701084
Browse files Browse the repository at this point in the history
  • Loading branch information
laurisb committed May 30, 2014
2 parents d701084 + 580f58f commit 66a6228
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 35 deletions.
59 changes: 49 additions & 10 deletions src/ProjectFinder.php
Expand Up @@ -6,21 +6,60 @@

class ProjectFinder
{
public function find($taskfile = './Taskfile')
/**
* Directory to search for Taskfiles
*
* @var string
*/
private $cwd;

/**
* Taskfile basename variants, in priority order.
*
* @var array
*/
private $variants = ['Taskfile', 'taskfile', 'taskfile.php'];

public function __construct($cwd = null)
{
if (!file_exists($taskfile)) {
throw new \InvalidArgumentException("$taskfile not found");
}
$this->cwd = $cwd ?: getcwd();
}

public function getCwd()
{
return $this->cwd;
}

if (filesize($taskfile) === 0) {
throw new \LogicException("Taskfile is empty");
/**
* @return string|false
*/
public function findTaskfile()
{
foreach ($this->variants as $variant) {
$file = $this->getCwd() . DIRECTORY_SEPARATOR . $variant;

if (is_file($file)) {
return $file;
}
}

return false;
}

/**
* @return Task\Project
* @throws RuntimeException
*/
public function find()
{
if (!$taskfile = $this->findTaskfile()) {
throw new \RuntimeException("No Taskfile found");
}

$project = require $taskfile;

$cls = 'Task\Project';
if (!($project instanceof $cls)) {
throw new \LogicException("Taskfile must return a Project");

if (!($project instanceof Project)) {
throw new \UnexpectedValueException("Taskfile must return an instance of 'Task\Project'");
}

return $project;
Expand Down
5 changes: 0 additions & 5 deletions tests/fixtures/Taskfile

This file was deleted.

Empty file.
3 changes: 3 additions & 0 deletions tests/fixtures/invalid-taskfile/Taskfile
@@ -0,0 +1,3 @@
<?php

return "foo";
Empty file.
5 changes: 5 additions & 0 deletions tests/fixtures/valid-taskfile/Taskfile
@@ -0,0 +1,5 @@
<?php

require __DIR__.'/../../../vendor/autoload.php';

return new Task\Project('cli');
37 changes: 17 additions & 20 deletions tests/src/ProjectFinderTest.php
Expand Up @@ -6,37 +6,34 @@ class ProjectFinderTest extends \PHPUnit_Framework_TestCase
{
public function testFind()
{
$taskfile = __DIR__.'/../fixtures/Taskfile';
$project = (new ProjectFinder)->find($taskfile);
$this->assertEquals(require $taskfile, $project);
$dir = dirname(__DIR__) . '/fixtures/valid-taskfile';

$taskfile = $dir . '/Taskfile';

$expected = require $taskfile;

$actual = (new ProjectFinder($dir))->find();

$this->assertEquals($expected, $actual);
}

/**
* @expectedException InvalidArgumentException
* @expectedException RuntimeException
*/
public function testFindThrowsOnNoTaskFile()
{
(new ProjectFinder)->find('nope');
$dir = dirname(__DIR__) . '/fixtures/no-taskfile';

(new ProjectFinder($dir))->find();
}

/**
* @expectedException LogicException
*/
public function testFindThrowsOnEmptyTaskFile()
{
$taskfile = tempnam(sys_get_temp_dir(), 'taskfile');
(new ProjectFinder)->find($taskfile);
unlink($taskfile);
}

/**
* @expectedException LogicException
* @expectedException UnexpectedValueException
*/
public function testFindThrowsOnNotProject()
{
$taskfile = tempnam(sys_get_temp_dir(), 'taskfile');
file_put_contents($taskfile, '<?php return "foo";');
(new ProjectFinder)->find($taskfile);
unlink($taskfile);
$dir = dirname(__DIR__) . '/fixtures/invalid-taskfile';

(new ProjectFinder($dir))->find();
}
}

0 comments on commit 66a6228

Please sign in to comment.