Skip to content

Commit 2d15078

Browse files
author
Davert
committed
phpunit command added, namespaces added to test names generator
1 parent b2012cb commit 2d15078

79 files changed

Lines changed: 278 additions & 12922 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

codecept

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use Symfony\Component\Console\Application,
3333
$app->add(new Codeception\Command\GenerateCept('generate:cept'));
3434
$app->add(new Codeception\Command\GenerateCest('generate:cest'));
3535
$app->add(new Codeception\Command\GenerateTest('generate:test'));
36+
$app->add(new Codeception\Command\GeneratePhpUnit('generate:phpunit'));
3637
$app->add(new Codeception\Command\GenerateSuite('generate:suite'));
3738
$app->add(new Codeception\Command\GenerateScenarios('generate:scenarios'));
3839
$app->run();

src/Codeception/Command/Base.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,47 @@ class Base extends \Symfony\Component\Console\Command\Command
88

99
protected function buildPath($basePath, $testName)
1010
{
11-
$testName = str_replace('/',DIRECTORY_SEPARATOR, $testName);
12-
$dir = pathinfo($testName, PATHINFO_DIRNAME);
13-
14-
15-
$path = $basePath.$dir;
11+
$testName = str_replace(array('/','\\'),array(DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR), $testName);
12+
$path = $basePath.$testName;
13+
$path = pathinfo($path, PATHINFO_DIRNAME).DIRECTORY_SEPARATOR;
1614
if (!file_exists($path)) {
1715
// Second argument should be mode. Well, umask() doesn't seem to return any if not set. Config may fix this.
1816
mkdir($path, 0775, true); // Third parameter commands to create directories recursively
1917
}
2018
return $path;
2119
}
2220

21+
protected function getNamespaceString($class)
22+
{
23+
$namespaces = $this->getNamespaces($class);
24+
$ns = "";
25+
if (count($namespaces)) {
26+
$ns = "namespace ".implode('\\',$namespaces).";\n";
27+
}
28+
return $ns;
29+
}
30+
2331
protected function getNamespaces($class)
2432
{
25-
$namespaces = explode('\\', $class);
33+
$namespaces = $this->breakParts($class);
2634
array_pop($namespaces);
2735
return $namespaces;
2836
}
2937

3038
protected function getClassName($class)
3139
{
32-
$namespaces = explode('\\', $class);
40+
$namespaces = $this->breakParts($class);
3341
return array_pop($namespaces);
3442
}
3543

44+
protected function breakParts($class)
45+
{
46+
$namespaces = explode('\\', $class);
47+
if (count($namespaces)) $namespaces[0] = ltrim($namespaces[0],'\\');
48+
if (!$namespaces[0]) array_shift($namespaces); // remove empty namespace caused of \\
49+
return $namespaces;
50+
}
51+
3652
protected function completeSuffix($filename, $suffix)
3753
{
3854
if (strpos(strrev($filename), strrev($suffix)) === 0) $filename .= '.php';

src/Codeception/Command/GenerateCest.php

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,36 @@
1111

1212
class GenerateCest extends Base
1313
{
14-
protected $template = "<?php\n%s %sCest\n{\n // the class name you want to test\n public %s = '';\n\n%s\n}\n";
15-
protected $methodTemplate = " // sample test\n public function %s(\\%s %s) {\n \n }";
14+
protected $template = <<<EOF
15+
<?php
16+
%suse Codeception\Util\Stub;
17+
18+
%s %sCest
19+
{
20+
protected $%s = '%s';
21+
22+
protected function _before()
23+
{
24+
}
25+
26+
protected function _after()
27+
{
28+
}
29+
30+
// tests
31+
%s
32+
33+
}
34+
EOF;
1635

17-
const SUFFIX = 'Cest';
36+
protected $methodTemplate = "public function %s(\\%s %s) {\n \n }";
1837

1938
protected function configure()
2039
{
2140
$this->setDefinition(array(
2241

2342
new \Symfony\Component\Console\Input\InputArgument('suite', InputArgument::REQUIRED, 'suite where tests will be put'),
24-
new \Symfony\Component\Console\Input\InputArgument('name', InputArgument::REQUIRED, 'test name'),
43+
new \Symfony\Component\Console\Input\InputArgument('class', InputArgument::REQUIRED, 'test name'),
2544
));
2645
parent::configure();
2746
}
@@ -33,43 +52,28 @@ public function getDescription() {
3352
public function execute(InputInterface $input, OutputInterface $output)
3453
{
3554
$suite = $input->getArgument('suite');
36-
$testName = $input->getArgument('name');
55+
$class = $input->getArgument('class');
3756

3857
$config = \Codeception\Configuration::config();
3958
$suiteconf = \Codeception\Configuration::suiteSettings($suite, $config);
4059

4160
$guy = $suiteconf['class_name'];
4261

43-
$classname = $this->getClassName($testName);
62+
$classname = $this->getClassName($class);
63+
$path = $this->buildPath($suiteconf['path'], $class);
64+
$ns = $this->getNamespaceString($class);
4465

45-
$path = $this->buildPath($suiteconf['path'], $testName);
46-
47-
$file = pathinfo($testName);
48-
$filename = $file['filename'];
49-
// filename already ends with Cest
50-
if (strpos($filename, $this::SUFFIX, strlen($filename) - strlen($this::SUFFIX)) === false) {
51-
$filename .= $this::SUFFIX;
52-
}
53-
$filename .= '.' . empty($file['extension']) ? '.php' : $file['extension'];
54-
55-
$filename = $path.DIRECTORY_SEPARATOR . $filename;
66+
$filename = $this->completeSuffix($classname, 'Cest');
67+
$filename = $path.DIRECTORY_SEPARATOR.$filename;
5668

5769
if (file_exists($filename)) {
5870
$output->writeln("<error>Test $filename already exists</error>");
5971
exit;
6072
}
6173

62-
// $methods = $reflected->getMethods(\ReflectionMethod::IS_PUBLIC);
63-
// foreach ($methods as $method) {
64-
// if ($method->getDeclaringClass()->name != $class) continue;
65-
// if ($method->isConstructor() or $method->isDestructor()) continue;
66-
//
67-
// $tests[] = sprintf($this->methodTemplate, $classname, $method->name,$method->name, $guy, '$I');
68-
// }
69-
7074
$tests = sprintf($this->methodTemplate, "shouldBe", $guy, '$I');
7175

72-
file_put_contents($filename, sprintf($this->template, 'class', $classname,'$class', $tests));
76+
file_put_contents($filename, sprintf($this->template, $ns, 'class', $classname, 'class', $class, $tests));
7377

7478
$output->writeln("<info>Cest was created in $filename</info>");
7579

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace Codeception\Command;
4+
5+
use Symfony\Component\Console\Application;
6+
use Symfony\Component\Console\Input\InputDefinition;
7+
use Symfony\Component\Console\Input\InputOption;
8+
use Symfony\Component\Console\Input\InputArgument;
9+
use Symfony\Component\Console\Input\InputInterface;
10+
use Symfony\Component\Console\Output\OutputInterface;
11+
12+
13+
class GeneratePhpUnit extends Base {
14+
15+
protected $template = <<<EOF
16+
<?php
17+
%s
18+
%s %sTest extends \PHPUnit_Framework_TestCase
19+
{
20+
protected function setUp()
21+
{
22+
}
23+
24+
protected function tearDown()
25+
{
26+
}
27+
28+
// tests
29+
public function testMe()
30+
{
31+
}
32+
33+
}
34+
EOF;
35+
36+
protected function configure()
37+
{
38+
$this->setDefinition(array(
39+
40+
new \Symfony\Component\Console\Input\InputArgument('suite', InputArgument::REQUIRED, 'suite where tests will be put'),
41+
new \Symfony\Component\Console\Input\InputArgument('class', InputArgument::REQUIRED, 'class name'),
42+
));
43+
parent::configure();
44+
}
45+
46+
public function getDescription() {
47+
return 'Generates empty PHPUnit test without Codeception additions';
48+
}
49+
50+
public function execute(InputInterface $input, OutputInterface $output)
51+
{
52+
$suite = $input->getArgument('suite');
53+
$class = $input->getArgument('class');
54+
55+
$config = \Codeception\Configuration::config();
56+
$suiteconf = \Codeception\Configuration::suiteSettings($suite, $config);
57+
58+
$classname = $this->getClassName($class);
59+
$path = $this->buildPath($suiteconf['path'], $class);
60+
$ns = $this->getNamespaceString($class);
61+
62+
$filename = $this->completeSuffix($classname, 'Test');
63+
$filename = $path.DIRECTORY_SEPARATOR.$filename;
64+
65+
if (file_exists($filename)) {
66+
$output->writeln("<error>Test $filename already exists</error>");
67+
exit;
68+
}
69+
70+
file_put_contents($filename, sprintf($this->template, $ns, 'class', $classname));
71+
72+
$output->writeln("<info>Test for $class was created in $filename</info>");
73+
}
74+
75+
}
76+

src/Codeception/Command/GenerateTest.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,28 @@ class GenerateTest extends Base
1313
{
1414
protected $template = <<<EOF
1515
<?php
16-
use Codeception\Util\Stub;
16+
%suse Codeception\Util\Stub;
1717
1818
%s %sTest extends \Codeception\TestCase\Test
1919
{
2020
/**
21-
* @var %s
21+
* @var \%s
2222
*/
2323
protected $%s;
2424
25-
// executed before each test
2625
protected function _before()
2726
{
2827
}
2928
30-
// executed after each test
3129
protected function _after()
3230
{
3331
}
3432
3533
// tests
34+
public function testMe()
35+
{
36+
37+
}
3638
3739
}
3840
EOF;
@@ -49,7 +51,7 @@ protected function configure()
4951
}
5052

5153
public function getDescription() {
52-
return 'Generates empty PHPUnit Test file in suite';
54+
return 'Generates empty unit test file in suite';
5355
}
5456

5557
public function execute(InputInterface $input, OutputInterface $output)
@@ -64,16 +66,17 @@ public function execute(InputInterface $input, OutputInterface $output)
6466

6567
$classname = $this->getClassName($class);
6668
$path = $this->buildPath($suiteconf['path'], $class);
69+
$ns = $this->getNamespaceString($class);
6770

6871
$filename = $this->completeSuffix($classname, 'Test');
69-
$filename = $path.DIRECTORY_SEPARATOR.$filename;
72+
$filename = $path.$filename;
7073

7174
if (file_exists($filename)) {
7275
$output->writeln("<error>Test $filename already exists</error>");
7376
exit;
7477
}
7578

76-
file_put_contents($filename, sprintf($this->template, 'class', $classname, $guy, lcfirst($guy), lcfirst($guy), $guy));
79+
file_put_contents($filename, sprintf($this->template, $ns, 'class', $classname, $guy, lcfirst($guy), lcfirst($guy), $guy));
7780

7881
$output->writeln("<info>Test for $class was created in $filename</info>");
7982

tests/cli/CliGuy.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,9 @@ public function seeFileFound($filename, $path = null) {
247247

248248

249249
/**
250+
* Executes a shell command
250251
*
252+
* @param $command
251253
* @see Cli::runShellCommmand()
252254
*
253255
* ! This method is generated. DO NOT EDIT. !
@@ -264,7 +266,9 @@ public function runShellCommmand($command) {
264266

265267

266268
/**
269+
* Checks that output from last executed command contains text
267270
*
271+
* @param $text
268272
* @see Cli::seeInShellOutput()
269273
*
270274
* ! This method is generated. DO NOT EDIT. !
@@ -281,6 +285,9 @@ public function seeInShellOutput($text) {
281285

282286

283287
/**
288+
* Checks that output from latest command doesn't contain text
289+
*
290+
* @param $text
284291
*
285292
* @see Cli::dontSeeInShellOutput()
286293
*

tests/cli/GeneratePhpUnitCept.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
$I = new CliGuy($scenario);
3+
$I->wantTo('generate sample Test');
4+
$I->amInPath('tests/data/sandbox');
5+
$I->executeCommand('generate:test dummy Dummy');
6+
$I->seeFileFound('DummyTest.php');
7+
$I->seeInThisFile('class DummyTest extends \PHPUnit_Framework_TestCase');
8+
$I->seeInThisFile('function setUp()');

tests/cli/GenerateTestCept.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
22
$I = new CliGuy($scenario);
3-
$I->wantTo('generate sample Cest');
3+
$I->wantTo('generate sample Test');
44
$I->amInPath('tests/data/sandbox');
55
$I->executeCommand('generate:test dummy Dummy');
66
$I->seeFileFound('DummyTest.php');
77
$I->seeInThisFile('class DummyTest extends \Codeception\TestCase\Test');
8-
$I->seeInThisFile('protected $dumbGuy');
8+
$I->seeInThisFile('protected $dumbGuy');
9+
$I->seeInThisFile("function _before(");

tests/coverage/CoverGuy.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,9 @@ public function seeFileFound($filename, $path = null) {
248248

249249

250250
/**
251+
* Executes a shell command
251252
*
253+
* @param $command
252254
* @see Cli::runShellCommmand()
253255
*
254256
* ! This method is generated. DO NOT EDIT. !
@@ -265,7 +267,9 @@ public function runShellCommmand($command) {
265267

266268

267269
/**
270+
* Checks that output from last executed command contains text
268271
*
272+
* @param $text
269273
* @see Cli::seeInShellOutput()
270274
*
271275
* ! This method is generated. DO NOT EDIT. !
@@ -282,6 +286,9 @@ public function seeInShellOutput($text) {
282286

283287

284288
/**
289+
* Checks that output from latest command doesn't contain text
290+
*
291+
* @param $text
285292
*
286293
* @see Cli::dontSeeInShellOutput()
287294
*

0 commit comments

Comments
 (0)