Skip to content

Commit ab8a3f1

Browse files
committed
Add BaseGeneratorTets
1 parent 5e1f76c commit ab8a3f1

File tree

5 files changed

+177
-23
lines changed

5 files changed

+177
-23
lines changed

src/Test/BaseGeneratorTest.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace DrupalCodeGenerator\Test;
4+
5+
use DrupalCodeGenerator\Application;
6+
use DrupalCodeGenerator\Helper\Renderer;
7+
use DrupalCodeGenerator\Tests\QuestionHelper;
8+
use DrupalCodeGenerator\Twig\TwigEnvironment;
9+
use DrupalCodeGenerator\Utils;
10+
use PHPUnit\Framework\TestCase;
11+
use Symfony\Component\Console\Command\Command;
12+
use Symfony\Component\Console\Tester\CommandTester;
13+
use Symfony\Component\Filesystem\Filesystem;
14+
use Twig\Loader\FilesystemLoader;
15+
16+
/**
17+
* Base class for generator tests.
18+
*/
19+
abstract class BaseGeneratorTest extends TestCase {
20+
21+
protected $display;
22+
protected $fixtureDir;
23+
private $directory;
24+
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function setUp(): void {
29+
$this->directory = \sys_get_temp_dir() . '/dcg_sandbox';
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function tearDown(): void {
36+
(new Filesystem())->remove($this->directory);
37+
}
38+
39+
/**
40+
* Executes the command.
41+
*
42+
* @param \Symfony\Component\Console\Command\Command $command
43+
* A command to execute.
44+
* @param array $input
45+
* An array of strings representing each input passed to the command input
46+
* stream.
47+
*
48+
* @return int
49+
* The command exit code
50+
*/
51+
protected function execute(Command $command, array $input): int {
52+
$this->createApplication()->add($command);
53+
54+
$command_tester = new CommandTester($command);
55+
$result = $command_tester
56+
->setInputs(\array_values($input))
57+
->execute(['--destination' => $this->directory, '--working-dir' => $this->directory]);
58+
59+
$this->display = $command_tester->getDisplay();
60+
61+
return $result;
62+
}
63+
64+
/**
65+
* Asserts generat display.
66+
*/
67+
protected function assertDisplay(string $expected_display): void {
68+
$default_name = Utils::machine2human(\basename($this->directory), TRUE);
69+
$expected_display = \str_replace('%default_name%', $default_name, $expected_display);
70+
self::assertEquals($expected_display, $this->display);
71+
}
72+
73+
/**
74+
* Asserts generated file.
75+
*/
76+
protected function assertGeneratedFile(string $file, string $fixture): void {
77+
self::assertFileEquals($this->fixtureDir . '/' . $fixture, $this->directory . '/' . $file);
78+
}
79+
80+
/**
81+
* Creates DCG application.
82+
*/
83+
private function createApplication(): Application {
84+
$application = Application::create();
85+
86+
$helper_set = $application->getHelperSet();
87+
88+
// Replace default question helper to ease parsing output.
89+
$helper_set->set(new QuestionHelper());
90+
91+
// Replace default renderer to support 'strict_variables' in tests.
92+
$twig_environment = new TwigEnvironment(new FilesystemLoader(), ['strict_variables' => TRUE]);
93+
$helper_set->set(new Renderer($twig_environment));
94+
return $application;
95+
}
96+
97+
}

tests/dcg/Generator/BaseGeneratorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
namespace DrupalCodeGenerator\Tests\Generator;
44

5-
use DrupalCodeGenerator\GeneratorTester;
65
use DrupalCodeGenerator\Tests\BaseTestCase;
76

87
/**
98
* Base class for generators tests.
9+
*
10+
* @todo Replace with \DrupalCodeGenerator\Test\BaseGeneratorTest
1011
*/
1112
abstract class BaseGeneratorTest extends BaseTestCase {
1213

tests/dcg/Generator/ControllerTest.php

Lines changed: 75 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,85 @@
22

33
namespace DrupalCodeGenerator\Tests\Generator;
44

5+
use DrupalCodeGenerator\Command\Controller;
6+
use DrupalCodeGenerator\Test\BaseGeneratorTest;
7+
58
/**
69
* Test for controller command.
710
*/
811
final class ControllerTest extends BaseGeneratorTest {
912

10-
protected $class = 'Controller';
11-
12-
protected $interaction = [
13-
'Module name [%default_name%]:' => 'Foo',
14-
'Module machine name [foo]:' => 'foo',
15-
'Class [FooController]:' => 'FooController',
16-
'Would you like to inject dependencies? [No]:' => 'Yes',
17-
'<1> Type the service name or use arrows up/down. Press enter to continue:' => 'database',
18-
'<2> Type the service name or use arrows up/down. Press enter to continue:' => '',
19-
'Would you like to create a route for this controller? [Yes]:' => 'Yes',
20-
'Route name [foo.example]:' => 'example.bar',
21-
'Route path [/foo/example]:' => '/foo/example',
22-
'Route title [Example]:' => 'Bar',
23-
'Route permission [access content]:' => 'access content',
24-
];
25-
26-
protected $fixtures = [
27-
'foo.routing.yml' => '/_controller_routing.yml',
28-
'src/Controller/FooController.php' => '/_controller.php',
29-
];
13+
protected $fixtureDir = __DIR__;
14+
15+
/**
16+
* Test callback.
17+
*/
18+
public function testGenerator(): void {
19+
20+
$input = [
21+
'Foo',
22+
'foo',
23+
'FooController',
24+
'Yes',
25+
'database',
26+
'',
27+
'Yes',
28+
'example.bar',
29+
'/foo/example',
30+
'Bar',
31+
'access content',
32+
];
33+
$this->execute(new Controller(), $input);
34+
35+
$expected_display = <<< 'TXT'
36+
37+
Welcome to controller generator!
38+
––––––––––––––––––––––––––––––––––
39+
40+
Module name [%default_name%]:
41+
42+
43+
Module machine name [foo]:
44+
45+
46+
Class [FooController]:
47+
48+
49+
Would you like to inject dependencies? [No]:
50+
51+
52+
Type the service name or use arrows up/down. Press enter to continue:
53+
54+
55+
Type the service name or use arrows up/down. Press enter to continue:
56+
57+
58+
Would you like to create a route for this controller? [Yes]:
59+
60+
61+
Route name [foo.example]:
62+
63+
64+
Route path [/foo/example]:
65+
66+
67+
Route title [Example]:
68+
69+
70+
Route permission [access content]:
71+
72+
73+
The following directories and files have been created or updated:
74+
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
75+
• foo.routing.yml
76+
• src/Controller/FooController.php
77+
78+
79+
TXT;
80+
$this->assertDisplay($expected_display);
81+
82+
$this->assertGeneratedFile('src/Controller/FooController.php', '_controller.php');
83+
$this->assertGeneratedFile('foo.routing.yml', '_controller_routing.yml');
84+
}
3085

3186
}

src/GeneratorTester.php renamed to tests/dcg/Generator/GeneratorTester.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?php
22

3-
namespace DrupalCodeGenerator;
3+
namespace DrupalCodeGenerator\Tests\Generator;
44

5+
use DrupalCodeGenerator\Application;
56
use DrupalCodeGenerator\Helper\Renderer;
67
use DrupalCodeGenerator\Tests\QuestionHelper;
78
use DrupalCodeGenerator\Twig\TwigEnvironment;
9+
use DrupalCodeGenerator\Utils;
810
use Symfony\Component\Console\Command\Command;
911
use Symfony\Component\Console\Tester\CommandTester;
1012
use Twig\Loader\FilesystemLoader;

tests/dcg/Generator/PhpStormMetadataTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace DrupalCodeGenerator\Tests\Generator;
44

55
use DrupalCodeGenerator\Command\PhpStormMetadata;
6-
use DrupalCodeGenerator\GeneratorTester;
76
use DrupalCodeGenerator\Helper\DrupalContext;
87

98
/**

0 commit comments

Comments
 (0)