Skip to content

Commit

Permalink
Generate tests with use of class in original namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitexus committed May 10, 2020
1 parent d255d7e commit ae9d997
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 138 deletions.
Empty file.
6 changes: 4 additions & 2 deletions src/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,16 @@ public function getOutSourceFile()
* Generates the code and writes it to a source file.
*
* @param string $file
*
* @return int bytes wrote
*/
public function write($file = '')
{
if ($file == '') {
$file = $this->outSourceFile;
}

file_put_contents($file, $this->generate());
return file_put_contents($file, $this->generate());
}

/**
Expand All @@ -150,7 +152,7 @@ protected function parseFullyQualifiedClassName($className)
if (strpos($className, '\\') !== false) {
$tmp = explode('\\', $className);
$result['className'] = $tmp[count($tmp)-1];
$result['namespace'] = $this->arrayToName($tmp);
$result['origNamespace'] = $result['namespace'] = $this->arrayToName($tmp);
}

return $result;
Expand Down
2 changes: 1 addition & 1 deletion src/CLI/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Application extends AbstractApplication
{
public function __construct()
{
$version = new Version('2.0.1', dirname(dirname(__DIR__)));
$version = new Version('2.0.2', dirname(dirname(__DIR__)));
parent::__construct('phpunit-skelgen', $version->getVersion());

$this->add(new GenerateClassCommand);
Expand Down
32 changes: 16 additions & 16 deletions src/CLI/BaseCommand.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* phpunit-skeleton-generator
*
Expand Down Expand Up @@ -56,18 +57,17 @@
* @link http://github.com/sebastianbergmann/phpunit-skeleton-generator/tree
* @since Class available since Release 2.0.0
*/
abstract class BaseCommand extends Command
{
abstract class BaseCommand extends Command {

/**
* Configures the current command.
*/
protected function configure()
{
protected function configure() {
$this->addOption(
'bootstrap',
null,
InputOption::VALUE_REQUIRED,
'A "bootstrap" PHP file that is run at startup'
'bootstrap',
null,
InputOption::VALUE_REQUIRED,
'A "bootstrap" PHP file that is run at startup'
);
}

Expand All @@ -79,22 +79,22 @@ protected function configure()
*
* @return null|integer null or 0 if everything went fine, or an error code
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
protected function execute(InputInterface $input, OutputInterface $output) {
if ($input->getOption('bootstrap') && file_exists($input->getOption('bootstrap'))) {
include $input->getOption('bootstrap');
}

$generator = $this->getGenerator($input);
$generator->write();
$written = $generator->write();

$output->writeln(
sprintf(
'Wrote skeleton for "%s" to "%s".',
$generator->getOutClassName(),
$generator->getOutSourceFile()
)
sprintf(
'Wrote skeleton for "%s" to "%s".',
$generator->getOutClassName(),
$generator->getOutSourceFile()
)
);
return $written > 0 ? 0 : 1;
}

/**
Expand Down
62 changes: 31 additions & 31 deletions src/CLI/GenerateTestCommand.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* phpunit-skeleton-generator
*
Expand Down Expand Up @@ -57,35 +58,34 @@
* @link http://github.com/sebastianbergmann/phpunit-skeleton-generator/tree
* @since Class available since Release 2.0.0
*/
class GenerateTestCommand extends BaseCommand
{
class GenerateTestCommand extends BaseCommand {

/**
* Configures the current command.
*/
protected function configure()
{
protected function configure() {
$this->setName('generate-test')
->setDescription('Generate a test class based on a class')
->addArgument(
'class',
InputArgument::REQUIRED,
'The name of the class to generate a test class for'
)
->addArgument(
'class-source',
InputArgument::OPTIONAL,
'The source file that declared the class to generate a test class for'
)
->addArgument(
'test-class',
InputArgument::OPTIONAL,
'The name of the test class that is to be generated'
)
->addArgument(
'test-source',
InputArgument::OPTIONAL,
'The file to which the generated test code is to be written'
);
->setDescription('Generate a test class based on a class')
->addArgument(
'class',
InputArgument::REQUIRED,
'The name of the class to generate a test class for'
)
->addArgument(
'class-source',
InputArgument::OPTIONAL,
'The source file that declared the class to generate a test class for'
)
->addArgument(
'test-class',
InputArgument::OPTIONAL,
'The name of the test class that is to be generated'
)
->addArgument(
'test-source',
InputArgument::OPTIONAL,
'The file to which the generated test code is to be written'
);

parent::configure();
}
Expand All @@ -94,13 +94,13 @@ protected function configure()
* @param InputInterface $input An InputInterface instance
* @return AbstractGenerator
*/
protected function getGenerator(InputInterface $input)
{
protected function getGenerator(InputInterface $input) {
return new TestGenerator(
(string)$input->getArgument('class'),
(string)$input->getArgument('class-source'),
(string)$input->getArgument('test-class'),
(string)$input->getArgument('test-source')
(string) $input->getArgument('class'),
(string) $input->getArgument('class-source'),
(string) $input->getArgument('test-class'),
(string) $input->getArgument('test-source')
);
}

}
Loading

0 comments on commit ae9d997

Please sign in to comment.