Skip to content

Commit

Permalink
Merge pull request #40 from veewee/feature-taskcontext
Browse files Browse the repository at this point in the history
Refactoring for running the tasks in different contexts
  • Loading branch information
veewee committed Sep 18, 2015
2 parents ceacccb + a9aa1ee commit 73a045f
Show file tree
Hide file tree
Showing 25 changed files with 575 additions and 170 deletions.
40 changes: 40 additions & 0 deletions spec/GrumPHP/Collection/TasksCollectionSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace spec\GrumPHP\Collection;

use GrumPHP\Task\Context\ContextInterface;
use GrumPHP\Task\TaskInterface;
use phpDocumentor\Reflection\DocBlock\Context;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class TasksCollectionSpec extends ObjectBehavior
{
public function let(TaskInterface $task1, TaskInterface $task2)
{
$this->beConstructedWith(array($task1, $task2));
}

function it_is_initializable()
{
$this->shouldHaveType('GrumPHP\Collection\TasksCollection');
}

function it_is_an_array_collection()
{
$this->shouldHaveType('Doctrine\Common\Collections\ArrayCollection');
}

function it_should_filter_by_context(TaskInterface $task1, TaskInterface $task2, ContextInterface $context)
{
$task1->canRunInContext($context)->willReturn(true);
$task2->canRunInContext($context)->willReturn(false);

$result = $this->filterByContext($context);
$result->shouldBeAnInstanceOf('GrumPHP\Collection\TasksCollection');
$result->count()->shouldBe(1);
$tasks = $result->toArray();
$tasks[0]->shouldBe($task1);
}

}
38 changes: 38 additions & 0 deletions spec/GrumPHP/Console/Helper/TaskRunnerHelperSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace spec\GrumPHP\Console\Helper;

use GrumPHP\Configuration\GrumPHP;
use GrumPHP\Console\Helper\PathsHelper;
use GrumPHP\Console\Helper\TaskRunnerHelper;
use GrumPHP\Exception\FailureException;
use GrumPHP\Runner\TaskRunner;
use GrumPHP\Task\Context\ContextInterface;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class TaskRunnerHelperSpec extends ObjectBehavior
{
function let(TaskRunner $taskRunner, HelperSet $helperSet, PathsHelper $pathsHelper)
{
$this->beConstructedWith($taskRunner);

$helperSet->get(PathsHelper::HELPER_NAME)->willreturn($pathsHelper);
$this->setHelperSet($helperSet);
}

function it_should_return_error_code_during_exceptions(OutputInterface $output, TaskRunner $taskRunner, ContextInterface $context)
{
$taskRunner->run($context)->willThrow(new FailureException());
$this->run($output, $context)->shouldReturn(TaskRunnerHelper::CODE_ERROR);
}

function it_should_return_success_code_during_exceptions(OutputInterface $output, TaskRunner $taskRunner, ContextInterface $context)
{
$taskRunner->run($context)->shouldBeCalled();
$this->run($output, $context)->shouldReturn(TaskRunnerHelper::CODE_SUCCESS);
}
}
56 changes: 25 additions & 31 deletions spec/GrumPHP/Runner/TaskRunnerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@
namespace spec\GrumPHP\Runner;

use GrumPHP\Collection\FilesCollection;
use GrumPHP\Task\Context\ContextInterface;
use GrumPHP\Task\TaskInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class TaskRunnerSpec extends ObjectBehavior
{

protected $files;

public function let()
public function let(TaskInterface $task1, TaskInterface $task2, ContextInterface $context)
{
$this->files = new FilesCollection();
$task1->canRunInContext($context)->willReturn(true);
$task2->canRunInContext($context)->willReturn(true);

$this->addTask($task1);
$this->addTask($task2);
}

function it_is_initializable()
Expand All @@ -24,49 +27,40 @@ function it_is_initializable()

function it_holds_tasks(TaskInterface $task1, TaskInterface $task2)
{
$this->addTask($task1);
$this->addTask($task2);

$this->getTasks()->shouldEqual(array($task1, $task2));
$this->getTasks()->toArray()->shouldEqual(array($task1, $task2));
}

function it_does_not_add_the_same_task_twice(TaskInterface $task1, TaskInterface $task2)
{
$this->addTask($task1);
$this->addTask($task1);
$this->addTask($task2);

$this->getTasks()->shouldEqual(array($task1, $task2));
$this->getTasks()->toArray()->shouldEqual(array($task1, $task2));
}

function it_runs_all_tasks(TaskInterface $task1, TaskInterface $task2)
function it_runs_all_tasks(TaskInterface $task1, TaskInterface $task2, ContextInterface $context)
{
$this->addTask($task1);
$this->addTask($task2);

$task1->run($this->files)->shouldBeCalled();
$task2->run($this->files)->shouldBeCalled();
$task1->run($context)->shouldBeCalled();
$task2->run($context)->shouldBeCalled();

$this->run($this->files);
$this->run($context);
}

function it_throws_exception_if_task_fails(TaskInterface $task1)
function it_throws_exception_if_task_fails(TaskInterface $task1, TaskInterface $task2, ContextInterface $context)
{
$this->addTask($task1);

$task1->run($this->files)->willThrow('GrumPHP\Exception\RuntimeException');
$task1->run($context)->willThrow('GrumPHP\Exception\RuntimeException');
$task2->run($context)->shouldBeCalled();

$this->shouldThrow('GrumPHP\Exception\FailureException')->duringRun($this->files);
$this->shouldThrow('GrumPHP\Exception\FailureException')->duringRun($context);
}

function it_runs_subsequent_tasks_if_one_fails(TaskInterface $task1, TaskInterface $task2)
{
$this->addTask($task1);
$this->addTask($task2);

$task1->run($this->files)->willThrow('GrumPHP\Exception\RuntimeException');
$task2->run($this->files)->shouldBeCalled();
function it_runs_subsequent_tasks_if_one_fails(
TaskInterface $task1,
TaskInterface $task2,
ContextInterface $context
) {
$task1->run($context)->willThrow('GrumPHP\Exception\RuntimeException');
$task2->run($context)->shouldBeCalled();

$this->shouldThrow('GrumPHP\Exception\FailureException')->duringRun($this->files);
$this->shouldThrow('GrumPHP\Exception\FailureException')->duringRun($context);
}
}
34 changes: 22 additions & 12 deletions spec/GrumPHP/Task/BehatSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use GrumPHP\Collection\FilesCollection;
use GrumPHP\Configuration\GrumPHP;
use GrumPHP\Locator\LocatorInterface;
use GrumPHP\Task\Context\ContextInterface;
use GrumPHP\Task\Context\GitPreCommitContext;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Symfony\Component\Process\Process;
Expand Down Expand Up @@ -34,42 +36,50 @@ function it_uses_its_external_command_locator_to_find_correct_command(LocatorInt
$this->getCommandLocation();
}

function it_does_not_do_anything_if_there_are_no_files(ProcessBuilder $processBuilder)
function it_should_run_in_git_pre_commit_context(GitPreCommitContext $context)
{
$this->canRunInContext($context)->shouldReturn(true);
}

function it_does_not_do_anything_if_there_are_no_files(ProcessBuilder $processBuilder, ContextInterface $context)
{
$processBuilder->add(Argument::any())->shouldNotBeCalled();
$processBuilder->setArguments(Argument::any())->shouldNotBeCalled();
$processBuilder->getProcess()->shouldNotBeCalled();

$files = new FilesCollection();
$this->run($files)->shouldBeNull();
$context->getFiles()->willReturn(new FilesCollection());
$this->run($context)->shouldBeNull();
}

function it_runs_the_suite(ProcessBuilder $processBuilder, Process $process)
function it_runs_the_suite(ProcessBuilder $processBuilder, Process $process, ContextInterface $context)
{
$processBuilder->setArguments(Argument::type('array'))->shouldBeCalled();
$processBuilder->getProcess()->willReturn($process);

$process->run()->shouldBeCalled();
$process->isSuccessful()->willReturn(true);

$files = new FilesCollection(array(
$context->getFiles()->willReturn(new FilesCollection(array(
new SplFileInfo('test.php')
));
$this->run($files);
)));
$this->run($context);
}

function it_throws_exception_if_the_process_fails(ProcessBuilder $processBuilder, Process $process)
{
function it_throws_exception_if_the_process_fails(
ProcessBuilder $processBuilder,
Process $process,
ContextInterface $context
) {
$processBuilder->setArguments(Argument::type('array'))->shouldBeCalled();
$processBuilder->getProcess()->willReturn($process);

$process->run()->shouldBeCalled();
$process->isSuccessful()->willReturn(false);
$process->getOutput()->shouldBeCalled();

$files = new FilesCollection(array(
$context->getFiles()->willReturn(new FilesCollection(array(
new SplFileInfo('test.php')
));
$this->shouldThrow('GrumPHP\Exception\RuntimeException')->duringRun($files);
)));
$this->shouldThrow('GrumPHP\Exception\RuntimeException')->duringRun($context);
}
}
51 changes: 34 additions & 17 deletions spec/GrumPHP/Task/BlacklistSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use GrumPHP\Collection\FilesCollection;
use GrumPHP\Configuration\GrumPHP;
use GrumPHP\Locator\LocatorInterface;
use GrumPHP\Task\Context\ContextInterface;
use GrumPHP\Task\Context\GitPreCommitContext;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Symfony\Component\Process\Process;
Expand Down Expand Up @@ -35,31 +37,41 @@ function it_uses_its_external_command_locator_to_find_correct_command(LocatorInt
$this->getCommandLocation();
}

function it_does_not_do_anything_if_there_are_no_files(ProcessBuilder $processBuilder)
function it_should_run_in_git_pre_commit_context(GitPreCommitContext $context)
{
$this->canRunInContext($context)->shouldReturn(true);
}

function it_does_not_do_anything_if_there_are_no_files(ProcessBuilder $processBuilder, ContextInterface $context)
{
$processBuilder->add(Argument::any())->shouldNotBeCalled();
$processBuilder->setArguments(Argument::any())->shouldNotBeCalled();
$processBuilder->getProcess()->shouldNotBeCalled();

$files = new FilesCollection();
$this->run($files)->shouldBeNull();
$context->getFiles()->willReturn(new FilesCollection());
$this->run($context)->shouldBeNull();
}

function it_does_not_do_anything_if_there_are_no_keywords(ProcessBuilder $processBuilder)
function it_does_not_do_anything_if_there_are_no_keywords(ProcessBuilder $processBuilder, ContextInterface $context)
{
$processBuilder->add(Argument::any())->shouldNotBeCalled();
$processBuilder->setArguments(Argument::any())->shouldNotBeCalled();
$processBuilder->getProcess()->shouldNotBeCalled();

$files = new FilesCollection(array(
$context->getFiles()->willReturn(new FilesCollection(array(
new SplFileInfo('file1.php'),
));
)));

$this->run($files)->shouldBeNull();
$this->run($context)->shouldBeNull();
}

function it_runs_the_suite(GrumPHP $grumPHP, LocatorInterface $externalCommandLocator, ProcessBuilder $processBuilder, Process $process)
{
function it_runs_the_suite(
GrumPHP $grumPHP,
LocatorInterface $externalCommandLocator,
ProcessBuilder $processBuilder,
Process $process,
ContextInterface $context
) {
$this->beConstructedWith($grumPHP, array('keywords'=>array('var_dump(', 'die(')), $externalCommandLocator, $processBuilder);

$processBuilder->setArguments(Argument::type('array'))->shouldBeCalled();
Expand All @@ -73,14 +85,19 @@ function it_runs_the_suite(GrumPHP $grumPHP, LocatorInterface $externalCommandLo
// Assume that blacklisted keywords was not found by `git grep` process
$process->isSuccessful()->willReturn(false);

$files = new FilesCollection(array(
$context->getFiles()->willReturn(new FilesCollection(array(
new SplFileInfo('file1.php'),
));
$this->run($files);
)));
$this->run($context);
}

function it_throws_exception_if_the_process_is_successfull(GrumPHP $grumPHP, LocatorInterface $externalCommandLocator, ProcessBuilder $processBuilder, Process $process)
{
function it_throws_exception_if_the_process_is_successfull(
GrumPHP $grumPHP,
LocatorInterface $externalCommandLocator,
ProcessBuilder $processBuilder,
Process $process,
ContextInterface $context
) {
$this->beConstructedWith($grumPHP, array('keywords'=>array('var_dump(')), $externalCommandLocator, $processBuilder);

$processBuilder->setArguments(Argument::type('array'))->shouldBeCalled();
Expand All @@ -92,9 +109,9 @@ function it_throws_exception_if_the_process_is_successfull(GrumPHP $grumPHP, Loc
$process->isSuccessful()->willReturn(true);
$process->getOutput()->shouldBeCalled();

$files = new FilesCollection(array(
$context->getFiles()->willReturn(new FilesCollection(array(
new SplFileInfo('file1.php'),
));
$this->shouldThrow('GrumPHP\Exception\RuntimeException')->duringRun($files);
)));
$this->shouldThrow('GrumPHP\Exception\RuntimeException')->duringRun($context);
}
}
30 changes: 30 additions & 0 deletions spec/GrumPHP/Task/Context/GitPreCommitContextSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace spec\GrumPHP\Task\Context;

use GrumPHP\Collection\FilesCollection;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class GitPreCommitContextSpec extends ObjectBehavior
{
function let(FilesCollection $files)
{
$this->beConstructedWith($files);
}

function it_is_initializable()
{
$this->shouldHaveType('GrumPHP\Task\Context\GitPreCommitContext');
}

function it_should_be_a_task_context()
{
$this->shouldImplement('GrumPHP\Task\Context\ContextInterface');
}

function it_should_have_files(FilesCollection $files)
{
$this->getFiles()->shouldBe($files);
}
}
Loading

0 comments on commit 73a045f

Please sign in to comment.