From 6b1662836fe373c900650d9da3b459948bfade65 Mon Sep 17 00:00:00 2001 From: Pascal Landau Date: Fri, 4 Jan 2019 10:02:38 +0100 Subject: [PATCH] Updates #580 to php7.0, removes de-duplication of --tasks option elements --- spec/Collection/TasksCollectionSpec.php | 17 ++++++++- spec/Runner/TaskRunnerContextSpec.php | 6 +-- src/Collection/TasksCollection.php | 15 ++++---- src/Console/Command/RunCommand.php | 10 ++--- src/Runner/TaskRunner.php | 2 +- src/Runner/TaskRunnerContext.php | 17 ++++----- test/Console/Command/RunCommandTest.php | 51 +++++++++---------------- 7 files changed, 56 insertions(+), 62 deletions(-) diff --git a/spec/Collection/TasksCollectionSpec.php b/spec/Collection/TasksCollectionSpec.php index 6edcabbec..db1974e6f 100644 --- a/spec/Collection/TasksCollectionSpec.php +++ b/spec/Collection/TasksCollectionSpec.php @@ -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(); @@ -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(); diff --git a/spec/Runner/TaskRunnerContextSpec.php b/spec/Runner/TaskRunnerContextSpec.php index 53024a401..94eb94c46 100644 --- a/spec/Runner/TaskRunnerContextSpec.php +++ b/spec/Runner/TaskRunnerContextSpec.php @@ -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() @@ -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); } @@ -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); } diff --git a/src/Collection/TasksCollection.php b/src/Collection/TasksCollection.php index e1ad8e9d0..1676a6bf6 100644 --- a/src/Collection/TasksCollection.php +++ b/src/Collection/TasksCollection.php @@ -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); }); } @@ -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()); diff --git a/src/Console/Command/RunCommand.php b/src/Console/Command/RunCommand.php index cd6cb9191..43639ba42 100644 --- a/src/Console/Command/RunCommand.php +++ b/src/Console/Command/RunCommand.php @@ -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); @@ -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 = []; @@ -110,7 +110,7 @@ protected function parseCommaSeparatedOption($value) if (empty($v)) { continue; } - $parsedValues[$v] = $v; + $parsedValues[] = $v; } return $parsedValues; } diff --git a/src/Runner/TaskRunner.php b/src/Runner/TaskRunner.php index bdde516f4..99c773e11 100644 --- a/src/Runner/TaskRunner.php +++ b/src/Runner/TaskRunner.php @@ -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(); diff --git a/src/Runner/TaskRunnerContext.php b/src/Runner/TaskRunnerContext.php index 0b96d1722..9586f4a91 100644 --- a/src/Runner/TaskRunnerContext.php +++ b/src/Runner/TaskRunnerContext.php @@ -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; } /** diff --git a/test/Console/Command/RunCommandTest.php b/test/Console/Command/RunCommandTest.php index 1f9ad4d03..5b4d62778 100644 --- a/test/Console/Command/RunCommandTest.php +++ b/test/Console/Command/RunCommandTest.php @@ -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); } @@ -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" => [],