66use Magento \Framework \Component \ComponentRegistrar ;
77use Magento \Framework \Filesystem ;
88use Magento \Framework \Filesystem \Directory \WriteInterface ;
9+ use RuntimeException ;
910use Symfony \Component \Console \Output \OutputInterface ;
11+ use Yireo \TestGenerator \Generator \IntegrationTest \AbstractTestGenerator ;
1012use Yireo \TestGenerator \Generator \IntegrationTest \GenericTestGenerator ;
1113use Yireo \TestGenerator \Generator \IntegrationTest \ModuleTestGenerator ;
14+ use Yireo \TestGenerator \Generator \IntegrationTest \TestGeneratorInterface ;
15+ use Yireo \TestGenerator \Model \ClassStub ;
1216use Yireo \TestGenerator \Model \ClassStubFactory ;
1317use Yireo \TestGenerator \Utilities \ClassCollector ;
1418
@@ -21,13 +25,17 @@ public function __construct(
2125 private DirectoryList $ directoryList ,
2226 private Filesystem $ filesystem ,
2327 private ClassCollector $ classCollector ,
24- private ClassStubFactory $ classStubFactory
28+ private ClassStubFactory $ classStubFactory ,
29+ private array $ testGenerators = [],
2530 ) {
2631 }
2732
28- public function generate (string $ moduleName , OutputInterface $ output , bool $ overrideExisting )
29- {
30- $ modulePath = $ this ->componentRegistrar ->getPath (ComponentRegistrar::MODULE , $ moduleName );
33+ public function generateAll (
34+ string $ moduleName ,
35+ OutputInterface $ output ,
36+ bool $ overrideExisting
37+ ) {
38+ $ modulePath = $ this ->getModulePath ($ moduleName );
3139 $ testPath = $ modulePath .'/Test/Integration/ ' ;
3240 if (false === $ this ->getWriter ()->isExist ($ testPath )) {
3341 $ this ->getWriter ()->create ($ testPath );
@@ -37,25 +45,43 @@ public function generate(string $moduleName, OutputInterface $output, bool $over
3745
3846 $ testFile = $ testPath .'ModuleTest.php ' ;
3947 if (true === $ overrideExisting || false === $ this ->getWriter ()->isExist ($ testFile )) {
40- $ testContents = $ this ->moduleTestGenerator ->generate ($ moduleName , $ classNamePrefix );
48+ $ phpGenerator = $ this ->moduleTestGenerator ->generate ($ moduleName , $ classNamePrefix );
49+ $ testContents = $ phpGenerator ->output ();
4150 $ output ->writeln ('Generating module test ' );
4251 $ this ->getWriter ()->writeFile ($ testFile , $ testContents );
4352 }
4453
4554 $ classNames = $ this ->classCollector ->collect ($ modulePath );
4655 foreach ($ classNames as $ className ) {
56+ $ this ->generateTest ($ className , $ modulePath , $ output , $ overrideExisting );
57+ }
58+ }
4759
48- $ classStub = $ this ->classStubFactory ->create ($ moduleName , $ className );
49- $ testClassStub = $ this ->classStubFactory ->createTest ($ classStub );
50- $ testFile = $ modulePath .'/ ' .$ testClassStub ->getRelativePath ();
60+ public function generateTest (
61+ string $ moduleName ,
62+ string $ className ,
63+ OutputInterface $ output ,
64+ bool $ overrideExisting
65+ ): void {
66+ $ modulePath = $ this ->getModulePath ($ moduleName );
5167
52- if (true === $ overrideExisting || false === $ this ->getWriter ()->isExist ($ testFile )) {
53- $ testContents = $ this ->genericTestGenerator ->generate ($ classStub , $ testClassStub );
54- $ output ->writeln ('Generating test for ' .$ className );
55- $ output ->writeln ('Writing file ' .$ testFile , OutputInterface::VERBOSITY_VERBOSE );
56- $ this ->getWriter ()->writeFile ($ testFile , $ testContents );
57- }
68+ $ classStub = $ this ->classStubFactory ->create ($ moduleName , $ className );
69+ $ testClassStub = $ this ->classStubFactory ->createTest ($ classStub );
70+ $ testFile = $ modulePath .'/ ' .$ testClassStub ->getRelativePath ();
71+
72+ if (false === $ overrideExisting && $ this ->getWriter ()->isExist ($ testFile )) {
73+ $ output ->writeln ('Test for ' .$ className .' already exists ' );
74+
75+ return ;
5876 }
77+
78+ $ testGenerator = $ this ->getTestGenerator ($ classStub );
79+ $ phpGenerator = $ testGenerator ->generate ($ classStub , $ testClassStub );
80+ $ testContents = $ phpGenerator ->output ();
81+
82+ $ output ->writeln ('Generating test for ' .$ className );
83+ $ output ->writeln ('Writing file ' .$ testFile , OutputInterface::VERBOSITY_VERBOSE );
84+ $ this ->getWriter ()->writeFile ($ testFile , $ testContents );
5985 }
6086
6187 private function getClassNamePrefix (string $ moduleName ): string
@@ -69,4 +95,29 @@ private function getWriter(): WriteInterface
6995 {
7096 return $ this ->filesystem ->getDirectoryWrite ($ this ->directoryList ::ROOT );
7197 }
98+
99+ private function getModulePath (string $ moduleName ): string
100+ {
101+ $ modulePath = $ this ->componentRegistrar ->getPath (ComponentRegistrar::MODULE , $ moduleName );
102+ if (empty ($ modulePath )) {
103+ throw new RuntimeException ('No path found for module " ' .$ moduleName .'" ' );
104+ }
105+
106+ return $ modulePath ;
107+ }
108+
109+ private function getTestGenerator (ClassStub $ classStub ): TestGeneratorInterface
110+ {
111+ foreach ($ this ->testGenerators as $ testGenerator ) {
112+ if (false === $ testGenerator instanceof TestGeneratorInterface) {
113+ continue ;
114+ }
115+
116+ if ($ testGenerator ->apply ($ classStub )) {
117+ return $ testGenerator ;
118+ }
119+ }
120+
121+ return $ this ->genericTestGenerator ;
122+ }
72123}
0 commit comments