这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ package: tools/box
sed -e 's/Application('"'"'dev/Application('"'"'$(TOOLBOX_VERSION)/g' bin/toolbox.php > build/phar/bin/toolbox.php

cd build/phar && \
composer config platform.php 8.0.0 && \
composer config platform.php 8.0.2 && \
composer update --no-dev -o -a

tools/box compile
Expand All @@ -84,7 +84,7 @@ package-devkit: tools/box
sed -e 's/\(Application(.*\)'"'"'dev/\1'"'"'$(TOOLBOX_VERSION)/g' bin/devkit.php > build/devkit-phar/bin/devkit.php

cd build/devkit-phar && \
composer config platform.php 8.0.0 && \
composer config platform.php 8.0.2 && \
composer update --no-dev -o -a

tools/box compile -c box-devkit.json.dist
Expand Down
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
"description": "Helps to discover and install tools",
"type": "project",
"require": {
"php": "~8.0.0 || ~8.1.0 || ~8.2.0",
"symfony/console": "^4.4 || ^5.4 || ^6.1",
"psr/container": "^1.0"
"php": "~8.0.2 || ~8.1.0 || ~8.2.0",
"symfony/console": "^5.4.31 || ^6.3",
"psr/container": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"zalas/phpunit-globals": "^2.1",
"phpspec/prophecy-phpunit": "^2.0",
"infection/infection": "^0.26"
},
"autoload": {
Expand Down
4 changes: 2 additions & 2 deletions src/Cli/ServiceContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function set(string $id, /*object */$service): void
/**
* {@inheritdoc}
*/
public function get($id)
public function get(string $id)
{
if (isset($this->runtimeServices[$id])) {
return $this->runtimeServices[$id];
Expand All @@ -68,7 +68,7 @@ public function get($id)
/**
* {@inheritdoc}
*/
public function has($id)
public function has(string $id): bool
{
return isset($this->services[$id]) || isset($this->runtimeServices[$id]);
}
Expand Down
29 changes: 15 additions & 14 deletions tests/Cli/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

namespace Zalas\Toolbox\Tests\Cli;

use PHPUnit\Framework\MockObject\Stub;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Symfony\Component\Console\Application as CliApplication;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
Expand All @@ -17,8 +15,6 @@

class ApplicationTest extends TestCase
{
use ProphecyTrait;

private const VERSION = 'test';

/**
Expand All @@ -27,14 +23,14 @@ class ApplicationTest extends TestCase
private $app;

/**
* @var ServiceContainer|ObjectProphecy
* @var ServiceContainer|Stub
*/
private $container;

protected function setUp(): void
{
$this->container = $this->prophesize(ServiceContainer::class);
$this->app = new Application(self::VERSION, $this->container->reveal());
$this->container = $this->createStub(ServiceContainer::class);
$this->app = new Application(self::VERSION, $this->container);
}

public function test_it_is_a_cli_application()
Expand Down Expand Up @@ -113,7 +109,7 @@ public function test_it_allows_to_override_tools_location()
*/
public function test_it_runs_the_command_in_dry_run_mode()
{
$output = $this->prophesize(OutputInterface::class);
$output = $this->givenOutputThatExpectsMessageWritten('composer global bin phpstan require');

$app = new Application(self::VERSION, new ServiceContainer());
$app->doRun(
Expand All @@ -123,12 +119,17 @@ public function test_it_runs_the_command_in_dry_run_mode()
'--tools' => [__DIR__.'/../resources/tools.json'],
'--no-interaction' => true,
]),
$output->reveal()
$output
);
}

public function givenOutputThatExpectsMessageWritten(string $message): OutputInterface
{
$output = $this->createMock(OutputInterface::class);
$output->expects(self::once())
->method('writeln')
->with(self::stringContains($message));

$output->writeln(Argument::allOf(
Argument::type('string'),
Argument::containingString('composer global bin phpstan require')
))->shouldHaveBeenCalled();
return $output;
}
}
45 changes: 20 additions & 25 deletions tests/Cli/Command/InstallCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,51 @@

namespace Zalas\Toolbox\Tests\Cli\Command;

use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use PHPUnit\Framework\MockObject\Stub;
use Zalas\Toolbox\Cli\Command\InstallCommand;
use Zalas\Toolbox\Runner\Runner;
use Zalas\Toolbox\Tool\Command;
use Zalas\Toolbox\Tool\Command\ShCommand;
use Zalas\Toolbox\Tool\Filter;
use Zalas\Toolbox\UseCase\InstallTools;

class InstallCommandTest extends ToolboxCommandTestCase
{
use ProphecyTrait;

protected const CLI_COMMAND_NAME = InstallCommand::NAME;

/**
* @var Runner|ObjectProphecy
* @var Runner|Stub
*/
private $runner;

/**
* @var InstallTools|ObjectProphecy
* @var InstallTools|Stub
*/
private $useCase;

protected function setUp(): void
{
$this->runner = $this->prophesize(Runner::class);
$this->useCase = $this->prophesize(InstallTools::class);
$this->runner = $this->createStub(Runner::class);
$this->useCase = $this->createStub(InstallTools::class);

parent::setUp();
}

public function test_it_runs_the_install_tools_use_case()
{
$command = $this->createCommand();
$this->useCase->__invoke(Argument::type(Filter::class))->willReturn($command);
$this->runner->run($command)->willReturn(0);
$this->useCase->method('__invoke')->willReturn($command);
$this->runner->method('run')->with($command)->willReturn(0);

$tester = $this->executeCliCommand();

$this->runner->run($command)->shouldHaveBeenCalled();

$this->assertSame(0, $tester->getStatusCode());
}

public function test_it_returns_the_status_code_of_the_run()
{
$this->useCase->__invoke(Argument::type(Filter::class))->willReturn($this->createCommand());
$this->runner->run(Argument::any())->willReturn(1);
$this->useCase->method('__invoke')->willReturn($this->createCommand());
$this->runner->method('run')->willReturn(1);

$tester = $this->executeCliCommand();

Expand All @@ -60,12 +55,15 @@ public function test_it_returns_the_status_code_of_the_run()

public function test_it_filters_by_tags()
{
$this->useCase->__invoke(Argument::type(Filter::class))->willReturn($this->createCommand());
$this->runner->run(Argument::any())->willReturn(0);
$this->useCase
->method('__invoke')
->with(new Filter(['foo'], ['bar']))
->willReturn($this->createCommand());
$this->runner->method('run')->willReturn(0);

$this->executeCliCommand(['--exclude-tag' => ['foo'], '--tag' => ['bar']]);
$tester = $this->executeCliCommand(['--exclude-tag' => ['foo'], '--tag' => ['bar']]);

$this->useCase->__invoke(new Filter(['foo'], ['bar']))->shouldBeCalled();
$this->assertSame(0, $tester->getStatusCode());
}

public function test_it_defines_dry_run_option()
Expand Down Expand Up @@ -118,16 +116,13 @@ public function test_it_takes_the_tag_option_default_from_environment_if_present
protected function getContainerTestDoubles(): array
{
return [
Runner::class => $this->runner->reveal(),
InstallTools::class => $this->useCase->reveal(),
Runner::class => $this->runner,
InstallTools::class => $this->useCase,
];
}

private function createCommand(): Command
{
$command = $this->prophesize(Command::class);
$command->__toString()->willReturn('echo "foo"');

return $command->reveal();
return new ShCommand('echo "foo"');
}
}
42 changes: 22 additions & 20 deletions tests/Cli/Command/ListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,34 @@

namespace Zalas\Toolbox\Tests\Cli\Command;

use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use PHPUnit\Framework\MockObject\Stub;
use Zalas\Toolbox\Cli\Command\ListCommand;
use Zalas\Toolbox\Tool\Collection;
use Zalas\Toolbox\Tool\Command\ShCommand;
use Zalas\Toolbox\Tool\Command\TestCommand;
use Zalas\Toolbox\Tool\Filter;
use Zalas\Toolbox\Tool\Tool;
use Zalas\Toolbox\UseCase\ListTools;

class ListCommandTest extends ToolboxCommandTestCase
{
use ProphecyTrait;

protected const CLI_COMMAND_NAME = ListCommand::NAME;

/**
* @var ListTools|ObjectProphecy
* @var ListTools|Stub
*/
private $useCase;

protected function setUp(): void
{
$this->useCase = $this->prophesize(ListTools::class);
$this->useCase = $this->createStub(ListTools::class);

parent::setUp();
}

public function test_it_runs_the_list_tools_use_case()
{
$this->useCase->__invoke(Argument::type(Filter::class))->willReturn(Collection::create([
$this->useCase->method('__invoke')->willReturn(Collection::create([
$this->createTool('Behat', 'Tests business expectations', 'http://behat.org'),
]));

Expand All @@ -44,13 +42,15 @@ public function test_it_runs_the_list_tools_use_case()

public function test_it_filters_by_tags()
{
$this->useCase->__invoke(Argument::type(Filter::class))->willReturn(Collection::create([
$this->createTool('Behat', 'Tests business expectations', 'http://behat.org'),
]));
$this->useCase->method('__invoke')
->with(new Filter(['foo'], ['bar']))
->willReturn(Collection::create([
$this->createTool('Behat', 'Tests business expectations', 'http://behat.org'),
]));

$this->executeCliCommand(['--exclude-tag' => ['foo'], '--tag' => ['bar']]);
$tester = $this->executeCliCommand(['--exclude-tag' => ['foo'], '--tag' => ['bar']]);

$this->useCase->__invoke(new Filter(['foo'], ['bar']))->shouldHaveBeenCalled();
$this->assertSame(0, $tester->getStatusCode());
}

public function test_it_defines_exclude_tag_option()
Expand Down Expand Up @@ -83,17 +83,19 @@ public function test_it_takes_the_tag_option_default_from_environment_if_present
protected function getContainerTestDoubles(): array
{
return [
ListTools::class => $this->useCase->reveal(),
ListTools::class => $this->useCase,
];
}

private function createTool(string $name, string $summary, string $website): Tool
{
$tool = $this->prophesize(Tool::class);
$tool->name()->willReturn($name);
$tool->summary()->willReturn($summary);
$tool->website()->willReturn($website);

return $tool->reveal();
return new Tool(
$name,
$summary,
$website,
[],
new ShCommand('any command'),
new TestCommand('any test command', 'any')
);
}
}
Loading