|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Modulus\Console\Commands; |
| 4 | + |
| 5 | +use Modulus\Console\ModulusCLI; |
| 6 | +use AtlantisPHP\Console\Command; |
| 7 | +use Modulus\Scaffolding\Template; |
| 8 | +use Symfony\Component\Console\Input\InputInterface; |
| 9 | +use Symfony\Component\Console\Output\OutputInterface; |
| 10 | + |
| 11 | +class CraftTest extends Command |
| 12 | +{ |
| 13 | + /** |
| 14 | + * The name and signature of the console command. |
| 15 | + * |
| 16 | + * @var string |
| 17 | + */ |
| 18 | + protected $signature = 'craft:test {name} {unit=false}'; |
| 19 | + |
| 20 | + /** |
| 21 | + * The full command description. |
| 22 | + * |
| 23 | + * @var string |
| 24 | + */ |
| 25 | + protected $help = 'This command allows you to create a Application event'; |
| 26 | + |
| 27 | + /** |
| 28 | + * The descriptions of the console commands. |
| 29 | + * |
| 30 | + * @var array |
| 31 | + */ |
| 32 | + protected $descriptions = [ |
| 33 | + 'craft:test' => 'Create a new test class', |
| 34 | + 'name' => 'The name of the class', |
| 35 | + 'unit' => 'Create a unit test' |
| 36 | + ]; |
| 37 | + |
| 38 | + /** |
| 39 | + * @param InputInterface $input |
| 40 | + * @param OutputInterface $output |
| 41 | + * |
| 42 | + * @return void |
| 43 | + */ |
| 44 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 45 | + { |
| 46 | + $isUnit = strtolower($input->getOption('unit')) == 'true' ? true : false; |
| 47 | + |
| 48 | + $name = $input->getArgument('name'); |
| 49 | + |
| 50 | + if ($this->add($name, $isUnit)) { |
| 51 | + return $output->writeln('<info>Test "' . $name . '" has been successfully created.</info>'); |
| 52 | + } |
| 53 | + |
| 54 | + return $output->writeln('Test "' . $name . '" already exists.'); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Add asset |
| 59 | + * |
| 60 | + * @param string $name |
| 61 | + * @param boolean $isUnit |
| 62 | + * @return boolean |
| 63 | + */ |
| 64 | + private function add(string $name, bool $isUnit) : bool |
| 65 | + { |
| 66 | + $tests = ModulusCLI::$appdir . 'tests' . DIRECTORY_SEPARATOR . ($isUnit ? 'Unit' : 'Feature'); |
| 67 | + $test = $tests . DIRECTORY_SEPARATOR . $name . '.php'; |
| 68 | + $namespace = ''; |
| 69 | + |
| 70 | + if (substr_count($name, '/') > 0) { |
| 71 | + ModulusCLI::_dir(substr($test, 0, strrpos($test, DIRECTORY_SEPARATOR))); |
| 72 | + $namespace = substr($name, 0, strrpos($name, DIRECTORY_SEPARATOR)); |
| 73 | + $name = str_replace($namespace . DIRECTORY_SEPARATOR, '', $name); |
| 74 | + |
| 75 | + $namespace = '\\' . str_replace('/', '\\', $namespace); |
| 76 | + } |
| 77 | + |
| 78 | + ModulusCLI::_dir($tests); |
| 79 | + |
| 80 | + $template = ($isUnit ? 'unit_test_template' : 'feature_test_template'); |
| 81 | + |
| 82 | + $content = Template::asset($template); |
| 83 | + $content = str_replace('{test_name}', $name, $content); |
| 84 | + $content = str_replace('{namespace}', $namespace, $content); |
| 85 | + |
| 86 | + if (file_exists($test)) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + else { |
| 90 | + file_put_contents($test, $content); |
| 91 | + return true; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments