Licitator 1.0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

181 lines
6.5 KiB

<?php
namespace Prometeo\CommandsBundle\Commands;
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
use Symfony\Bundle\FrameworkBundle\Test\WebTestAssertionsTrait;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Component\BrowserKit\History;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\CssSelector\CssSelectorConverter;
use Symfony\Component\Panther\PantherTestCaseTrait;
use Symfony\Component\Routing\Router;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\DependencyInjection\Container;
/**
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
* @author Ryan Weaver <weaverryan@gmail.com>
*/
class MakeFunctionalTest extends AbstractMaker
{
/**
* @var Container
*/
public $container;
/**
* Constructor
*
* @param Container $container
*/
public function __construct(Container $container)
{
$this->container = $container;
}
public static function getCommandName(): string
{
return 'prometeo:make:functional-test';
}
public function configureCommand(Command $command, InputConfiguration $inputConf)
{
$command
->setDescription('Creates a new functional test class')
->addArgument('name', InputArgument::OPTIONAL, 'The name of the functional test class (e.g. <fg=yellow>DefaultControllerTest</>)')
->addArgument('classtype', InputArgument::OPTIONAL, 'Type of class to test (e.g. <fg=yellow>Admin</>)')
->addArgument('username', InputArgument::OPTIONAL, 'Username for tests')
->addArgument('password', InputArgument::OPTIONAL, 'Password for test')
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeFunctionalTest.txt'))
;
}
/**
* @param $filter
* @return array
* @throws \Exception
*/
public function getRoutes($filter)
{
/** @var Router $router */
$router = $this->container->get('router');
$allRoutes = $router->getRouteCollection();
$routes = $allRoutes->all();
$filtered_routes=[];
$controllerAdmin='';
/** @var $params \Symfony\Component\Routing\Route */
foreach ($routes as $route => $params)
{
$defaults = $params->getDefaults();
if (isset($defaults['_controller']))
{
$controllerAction = explode(':', $defaults['_controller']);
$controller = $controllerAction[0];
//Filtramos los controller de los admin
if(strpos($controller, 'App\Controller\Admin\\'.$filter)!==false){
$controllerAdmin=$controller;
if (!isset($filtered_routes[$controller])) {
$filtered_routes[$controller]['routes'] = array();
$filtered_routes[$controller]['urls'] = array();
}
$filtered_routes[$controller]['routes'][]= $route;
$filtered_routes[$controller]['urls'][]=strtok( $router->generate($route, array('id'=>1, 'position'=>1, 'childId'=>1)),'?');
}
}
}
return [
'routes' => $filtered_routes[$controllerAdmin]
];
}
/**
* @param \Symfony\Component\Routing\Route $route
* @throws \Exception
*/
private function convertController(\Symfony\Component\Routing\Route $route)
{
$nameParser = $this->container->get('controller_name_converter');
if ($route->hasDefault('_controller')) {
try {
$route->setDefault('_controller', $nameParser->build($route->getDefault('_controller')));
} catch (\InvalidArgumentException $e) {
}
}
}
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
{
$originClass=$input->getArgument('name');
$username=$input->getArgument('username');
$password=$input->getArgument('password');
$testClassNameDetails = $generator->createClassNameDetails(
$input->getArgument('name'),
'Tests\\Admin',
'Test'
);
if($input->getArgument('classtype')) {
switch ($input->getArgument('classtype')) {
case 'Admin':
$endclass = explode('\\', $originClass);
$originClass = end($endclass);
$routes=$this->getRoutes($originClass);
$generator->generateClass(
$testClassNameDetails->getFullName(),
__DIR__ . '/../Resources/skeleton/test/Functional.tpl.php',
[
'username'=>$username,
'password'=>$password,
'routes'=>$routes,
'originClass' => $originClass,
'web_assertions_are_available' => trait_exists(WebTestAssertionsTrait::class),
'panther_is_available' => trait_exists(PantherTestCaseTrait::class),
]
);
break;
}
}else{
$generator->generateClass(
$testClassNameDetails->getFullName(),
__DIR__.'/../Resources/skeleton/test/Functional.tpl.php',
[
'originClass'=>$originClass,
'web_assertions_are_available' => trait_exists(WebTestAssertionsTrait::class),
'panther_is_available' => trait_exists(PantherTestCaseTrait::class),
]
);
}
$generator->writeChanges();
$this->writeSuccessMessage($io);
$io->text([
'Next: Open your new test class and start customizing it.',
'Find the documentation at <fg=yellow>https://symfony.com/doc/current/testing.html#functional-tests</>',
]);
}
public function configureDependencies(DependencyBuilder $dependencies)
{
$dependencies->addClassDependency(
History::class,
'browser-kit',
true,
true
);
$dependencies->addClassDependency(
CssSelectorConverter::class,
'css-selector',
true,
true
);
}
}