Skip to content

Commit

Permalink
more tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
mkorkmaz committed Nov 30, 2018
1 parent 628775a commit 7ed621f
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 22 deletions.
33 changes: 18 additions & 15 deletions src/Console/ApplicationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

use Symfony\Component\Console\Application;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Command\Command;
use Selami\Stdlib\Resolver;
use ReflectionClass;
use ReflectionException;
use InvalidArgumentException;
use Symfony\Component\Console\Command\Command;
use Selami\Console\Exception\DependencyNotFoundException;
use Selami\Stdlib\Exception\ClassOrMethodCouldNotBeFound;

class ApplicationFactory
{
Expand All @@ -28,7 +29,17 @@ public static function makeApplication(
'version' => $version
];
foreach ($commands as $command) {
$controllerConstructorArguments = Resolver::getParameterHints($command, '__construct');
try {
$controllerConstructorArguments = Resolver::getParameterHints($command, '__construct');
} catch (ClassOrMethodCouldNotBeFound $e) {
throw new DependencyNotFoundException(
sprintf(
'%s when calling command: %s',
$e->getMessage(),
$command
)
);
}
$arguments = [];
foreach ($controllerConstructorArguments as $argumentName => $argumentType) {
$arguments[] = self::getArgument(
Expand All @@ -38,11 +49,8 @@ public static function makeApplication(
$argumentType
);
}
try {
$reflectionClass = new ReflectionClass($command);
} catch (ReflectionException $exception) {
throw new InvalidArgumentException($exception->getMessage());
}
$reflectionClass = new ReflectionClass($command);

/**
* @var Command $autoWiredCommand
*/
Expand All @@ -62,18 +70,13 @@ private static function getArgument(
return $consoleApplicationArguments[$argumentName];
}
if ($argumentType === Resolver::ARRAY && $container->has($argumentName) === false) {
throw new InvalidArgumentException(
sprintf('Container does not have item for array: %s', $argumentName)
throw new DependencyNotFoundException(
sprintf('Container does not have an item for array: %s', $argumentName)
);
}
if ($argumentType === Resolver::ARRAY) {
return $container->get($argumentName);
}
if ($container->has($argumentType) === false) {
throw new InvalidArgumentException(
sprintf('Container does not have item for service: %s', $argumentType)
);
}
return $container->get($argumentType);
}
}
9 changes: 9 additions & 0 deletions src/Console/Exception/DependencyNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);

namespace Selami\Console\Exception;

class DependencyNotFoundException extends \InvalidArgumentException
{

}
6 changes: 4 additions & 2 deletions tests/resources/Command/OrdinaryCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ class OrdinaryCommand extends Command
* @var PrintService
*/
private $printService;
private $config;

public function __construct(PrintService $printService, string $name = null)
public function __construct(PrintService $printService, array $config, string $name = null)
{
$this->printService = $printService;
$this->config = $config;
parent::__construct($name);
}

Expand All @@ -37,6 +39,6 @@ protected function configure() : void
protected function execute(InputInterface $input, OutputInterface $output) : void
{
$name = $input->getArgument('name');
$output->write($this->printService->formatMessage($name));
$output->write($this->printService->formatMessage($name) . ' -'. $this->config['cache']);
}
}
44 changes: 44 additions & 0 deletions tests/resources/Command/OrdinaryCommandWithArrayDependency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);


namespace SelamiConsoleTest\Command;

use SelamiConsoleTest\Service\PrintService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;

class OrdinaryCommandWithArrayDependency extends Command
{
/**
* @var PrintService
*/
private $printService;
private $config;

public function __construct(PrintService $printService, array $args, string $name = null)
{
$this->printService = $printService;
$this->config = $config;
parent::__construct($name);
}

protected function configure() : void
{
$this
->setName('command:ordinary-with-array-dependency')
->setDescription('Show basic information about all mapped entities')
->setDefinition([
new InputArgument('name', InputArgument::REQUIRED),
]);
}


protected function execute(InputInterface $input, OutputInterface $output) : void
{
$name = $input->getArgument('name');
$output->write($this->printService->formatMessage($name) . ' -'. $this->config['cache']);
}
}
43 changes: 43 additions & 0 deletions tests/resources/Command/OrdinaryCommandWithServiceDependency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);


namespace SelamiConsoleTest\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;

class OrdinaryCommandWithServiceDependency extends Command
{
/**
* @var NonExistingPrintService
*/
private $printService;
private $config;

public function __construct(NonExistingPrintService $printService, string $name = null)
{
$this->printService = $printService;
$this->config = $config;
parent::__construct($name);
}

protected function configure() : void
{
$this
->setName('command:ordinary-with-service-dependency')
->setDescription('Show basic information about all mapped entities')
->setDefinition([
new InputArgument('name', InputArgument::REQUIRED),
]);
}


protected function execute(InputInterface $input, OutputInterface $output) : void
{
$name = $input->getArgument('name');
$output->write($this->printService->formatMessage($name) . ' -'. $this->config['cache']);
}
}
110 changes: 105 additions & 5 deletions tests/unit/ApplicationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@ class ApplicationFactoryTest extends \Codeception\Test\Unit

protected function _before()
{
$container = new ServiceManager(
}

protected function successfulConsoleApp()
{
$container = new ServiceManager([
'factories' => [
SelamiConsoleTest\Service\PrintService::class => SelamiConsoleTest\Factory\PrintServiceFactory::class
]
]);
$container->setService(
'config',
[
'factories' => [
SelamiConsoleTest\Service\PrintService::class => SelamiConsoleTest\Factory\PrintServiceFactory::class
]
'cache' => '/tmp/uRt48sl'
]
);
$container->setService(
Expand All @@ -31,6 +39,51 @@ protected function _before()
$this->container = $container;
}

protected function failedConsoleApp() : void
{
$container = new ServiceManager([
'factories' => [
SelamiConsoleTest\Service\PrintService::class => SelamiConsoleTest\Factory\PrintServiceFactory::class
]
]);
$container->setService(
'config',
[
'cache' => '/tmp/uRt48sl'
]
);
$container->setService(
'commands',
[
SelamiConsoleTest\Command\OrdinaryCommand::class,
SelamiConsoleTest\Command\OrdinaryCommandWithArrayDependency::class,
]
);
$this->container = $container;
}
protected function failedForNonExistingServiceConsoleApp() : void
{
$container = new ServiceManager([
'factories' => [
SelamiConsoleTest\Service\PrintService::class => SelamiConsoleTest\Factory\PrintServiceFactory::class
]
]);
$container->setService(
'config',
[
'cache' => '/tmp/uRt48sl'
]
);
$container->setService(
'commands',
[
SelamiConsoleTest\Command\OrdinaryCommand::class,
SelamiConsoleTest\Command\OrdinaryCommandWithServiceDependency::class,
]
);
$this->container = $container;
}

protected function _after()
{
}
Expand All @@ -40,6 +93,7 @@ protected function _after()
*/
public function shouldRunCommandSuccessfully() : void
{
$this->successfulConsoleApp();
$input = new ArrayInput(array('command'=>'command:ordinary', 'name' => 'world'));
$cli = Console\ApplicationFactory::makeApplication($this->container, 'TestApp', '1.0.1');
$cli->setAutoExit(false);
Expand All @@ -48,6 +102,52 @@ public function shouldRunCommandSuccessfully() : void
$output = new BufferedOutput();
$cli->run($input, $output);
$return = $output->fetch();
$this->assertEquals('Hello world', $return);
$this->assertEquals('Hello world -/tmp/uRt48sl', $return);
}

/**
* @test
* @expectedException \Selami\Console\Exception\DependencyNotFoundException
*/
public function shouldFailNonExistingArrayDependency() : void
{
$this->failedConsoleApp();
$input = new ArrayInput(array('command'=>'command:ordinary-with-array-dependency', 'name' => 'world'));
$cli = Console\ApplicationFactory::makeApplication($this->container, 'TestApp', '1.0.1');
$cli->setAutoExit(false);
$this->assertEquals('TestApp', $cli->getName());
$this->assertEquals('1.0.1', $cli->getVersion());
$output = new BufferedOutput();
$cli->run($input, $output);
}
/**
* @test
* @expectedException \Selami\Console\Exception\DependencyNotFoundException
*/
public function shouldFailNonExistingServiceDependency() : void
{
$this->failedForNonExistingServiceConsoleApp();
$input = new ArrayInput(array('command'=>'command:ordinary-with-service-dependency', 'name' => 'world'));
$cli = Console\ApplicationFactory::makeApplication($this->container, 'TestApp', '1.0.1');
$cli->setAutoExit(false);
$this->assertEquals('TestApp', $cli->getName());
$this->assertEquals('1.0.1', $cli->getVersion());
$output = new BufferedOutput();
$cli->run($input, $output);
}
/**
* @testw
* @expectedExfception \Selami\Console\Exception\DependencyNotFoundException
*/
public function shouldFailNonExistingSCommandDependency() : void
{
$this->failedForNonExistingServiceConsoleApp();
$input = new ArrayInput(array('command'=>'command:ordinary-with-service-dependency', 'name' => 'world'));
$cli = Console\ApplicationFactory::makeApplication($this->container, 'TestApp', '1.0.1');
$cli->setAutoExit(false);
$this->assertEquals('TestApp', $cli->getName());
$this->assertEquals('1.0.1', $cli->getVersion());
$output = new BufferedOutput();
$cli->run($input, $output);
}
}

0 comments on commit 7ed621f

Please sign in to comment.