Skip to content

Commit

Permalink
Updates phpro#580 to php7.0, removes de-duplication of --tasks option…
Browse files Browse the repository at this point in the history
… elements
  • Loading branch information
Pascal Landau committed Jan 4, 2019
1 parent b381bcb commit 6b16628
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 62 deletions.
17 changes: 15 additions & 2 deletions spec/Collection/TasksCollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,20 @@ function it_can_filter_by_task_names(TaskInterface $task1, TaskInterface $task2)
$task2->getName()->willReturn('task2');
$tasks = ['task1'];

$result = $this->filterByTaskName($tasks);
$result = $this->filterByTaskNames($tasks);
$result->shouldBeAnInstanceOf(TasksCollection::class);
$result->count()->shouldBe(1);
$tasks = $result->toArray();
$tasks[0]->shouldBe($task1);
}

function it_can_filter_by_duplicate_task_names(TaskInterface $task1, TaskInterface $task2)
{
$task1->getName()->willReturn('task1');
$task2->getName()->willReturn('task2');
$tasks = ['task1', 'task1'];

$result = $this->filterByTaskNames($tasks);
$result->shouldBeAnInstanceOf(TasksCollection::class);
$result->count()->shouldBe(1);
$tasks = $result->toArray();
Expand All @@ -81,7 +94,7 @@ function it_can_filter_by_empty_task_names(TaskInterface $task1, TaskInterface $
$task2->getName()->willReturn('task2');
$tasks = [];

$result = $this->filterByTaskName($tasks);
$result = $this->filterByTaskNames($tasks);
$result->shouldBeAnInstanceOf(TasksCollection::class);
$result->count()->shouldBe(2);
$tasks = $result->toArray();
Expand Down
6 changes: 3 additions & 3 deletions spec/Runner/TaskRunnerContextSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TaskRunnerContextSpec extends ObjectBehavior
{
function let(ContextInterface $context, TestSuiteInterface $testSuite)
{
$this->beConstructedWith($context, $testSuite);
$this->beConstructedWith($context, [], $testSuite);
}

function it_is_initializable()
Expand All @@ -32,7 +32,7 @@ function it_has_a_test_suite(TestSuiteInterface $testSuite)

function it_has_no_test_suite(ContextInterface $context)
{
$this->beConstructedWith($context);
$this->beConstructedWith($context, []);
$this->hasTestSuite()->shouldBe(false);
$this->getTestSuite()->shouldBe(null);
}
Expand All @@ -46,7 +46,7 @@ function it_has_no_tasks()
function it_has_tasks(ContextInterface $context)
{
$tasks = ["task_1"];
$this->beConstructedWith($context, null, $tasks);
$this->beConstructedWith($context, $tasks);
$this->hasTasks()->shouldBe(true);
$this->getTasks()->shouldBe($tasks);
}
Expand Down
15 changes: 7 additions & 8 deletions src/Collection/TasksCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,15 @@ public function filterByTestSuite(TestSuiteInterface $testSuite = null): self

/**
* @param string[] $tasks
*
* @return TasksCollection
*/
public function filterByTaskName($tasks)
public function filterByTaskNames($tasks): self
{
if (empty($tasks)) {
return new self($this->toArray());
}

return $this->filter(function (TaskInterface $task) use ($tasks) {
if (empty($tasks)) {
return true;
}
return in_array($task->getName(), $tasks);
return \in_array($task->getName(), $tasks, true);
});
}

Expand All @@ -54,7 +53,7 @@ public function filterByTaskName($tasks)
*/
public function sortByPriority(GrumPHP $grumPHP): self
{
$priorityQueue = new SplPriorityQueue();
$priorityQueue = new SplPriorityQueue();
$stableSortIndex = PHP_INT_MAX;
foreach ($this->getIterator() as $task) {
$metadata = $grumPHP->getTaskMetadata($task->getName());
Expand Down
10 changes: 5 additions & 5 deletions src/Console/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ public function execute(InputInterface $input, OutputInterface $output)
$files = $this->getRegisteredFiles();
$testSuites = $this->grumPHP->getTestSuites();

$tasks = $this->parseCommaSeparatedOption($input->getOption("tasks"));
$tasks = $this->parseCommaSeparatedOption($input->getOption("tasks") ?? "");

$context = new TaskRunnerContext(
new RunContext($files),
(bool) $input->getOption('testsuite') ? $testSuites->getRequired($input->getOption('testsuite')) : null,
$tasks
$tasks,
(bool) $input->getOption('testsuite') ? $testSuites->getRequired($input->getOption('testsuite')) : null
);

return $this->taskRunner()->run($output, $context);
Expand Down Expand Up @@ -101,7 +101,7 @@ protected function paths(): PathsHelper
* @param string $value
* @return string[]
*/
protected function parseCommaSeparatedOption($value)
protected function parseCommaSeparatedOption(string $value)
{
$stringValues = explode(",", $value);
$parsedValues = [];
Expand All @@ -110,7 +110,7 @@ protected function parseCommaSeparatedOption($value)
if (empty($v)) {
continue;
}
$parsedValues[$v] = $v;
$parsedValues[] = $v;
}
return $parsedValues;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Runner/TaskRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function run(TaskRunnerContext $runnerContext): TaskResultCollection
$tasks = $this->tasks
->filterByContext($runnerContext->getTaskContext())
->filterByTestSuite($runnerContext->getTestSuite())
->filterByTaskName($runnerContext->getTasks())
->filterByTaskNames($runnerContext->getTasks())
->sortByPriority($this->grumPHP);
$taskResults = new TaskResultCollection();

Expand Down
17 changes: 7 additions & 10 deletions src/Runner/TaskRunnerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,27 @@ class TaskRunnerContext
private $skipSuccessOutput = false;

/**
* @var null|TestSuiteInterface
* @var string[]
*/
private $testSuite = null;
private $tasks = null;

/**
* @var string[]
* @var null|TestSuiteInterface
*/
private $tasks = null;
private $testSuite = null;

/**
* TaskRunnerContext constructor.
*
* @param ContextInterface $taskContext
* @param string[] $tasks
* @param TestSuiteInterface $testSuite
* @param string[]|null $tasks
*/
public function __construct(ContextInterface $taskContext, TestSuiteInterface $testSuite = null, $tasks = null)
public function __construct(ContextInterface $taskContext, array $tasks, TestSuiteInterface $testSuite = null)
{
$this->taskContext = $taskContext;
$this->testSuite = $testSuite;
if ($tasks === null) {
$tasks = [];
}
$this->tasks = $tasks;
$this->testSuite = $testSuite;
}

/**
Expand Down
51 changes: 18 additions & 33 deletions test/Console/Command/RunCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,32 @@

namespace GrumPHPTest\Console\Command;

use GrumPHP\Configuration\GrumPHP;
use GrumPHP\Console\Command\RunCommand;
use GrumPHP\Locator\RegisteredFiles;
use PHPUnit\Framework\TestCase;

class RunCommandTest extends TestCase
{
/**
* @test
* @param null $valueString
* @param null $expected
* @param string $valueString
* @param array $expected
* @dataProvider parses_comma_separated_options_dataProvider
* @throws \ReflectionException
*/
function parses_comma_separated_options($valueString = null, $expected = null)
function parses_comma_separated_options(string $valueString, array $expected)
{
/**
* @var GrumPHP $grumPhp
*/
$grumPhp = $this->createMock(GrumPHP::class);
/**
* @var RegisteredFiles $registeredFiles
*/
$registeredFiles = $this->createMock(RegisteredFiles::class);
$command = new class extends RunCommand{
public function __construct()
{
}

$command = new RunCommand($grumPhp, $registeredFiles);
$method = new \ReflectionMethod($command, "parseCommaSeparatedOption");
$method->setAccessible(true);
public function parseCommaSeparatedOption($str)
{
return parent::parseCommaSeparatedOption(... func_get_args());
}
};

$actual = $method->invoke($command, $valueString);
$actual = $command->parseCommaSeparatedOption($valueString);
$actual = array_values($actual);

$this->assertEquals($expected, $actual);
}
Expand All @@ -43,28 +39,17 @@ public function parses_comma_separated_options_dataProvider()
"default" => [
"valueString" => "foo,bar",
"expected" => [
"foo" => "foo",
"bar" => "bar"
"foo",
"bar"
],
],
"trims values" => [
"valueString" => "foo , bar",
"expected" => [
"foo" => "foo",
"bar" => "bar"
"foo",
"bar"
],
],
"deduplicates values" => [
"valueString" => "foo,bar,bar",
"expected" => [
"foo" => "foo",
"bar" => "bar"
],
],
"null" => [
"valueString" => null,
"expected" => [],
],
"empty" => [
"valueString" => "",
"expected" => [],
Expand Down

0 comments on commit 6b16628

Please sign in to comment.