Skip to content

Commit f1409f2

Browse files
committed
Change class name fetch logic
1 parent 4334dc5 commit f1409f2

File tree

2 files changed

+70
-13
lines changed

2 files changed

+70
-13
lines changed

Code/Generator.php

+69-12
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
namespace Olmer\UnitTestsGenerator\Code;
66

7-
use Magento\Framework\Code\Generator\Io;
7+
use Magento\Framework\{
8+
Code\Generator\Io,
9+
Filesystem\Driver\File as Reader
10+
};
811

912
class Generator
1013
{
@@ -16,19 +19,26 @@ class Generator
1619
* @var \Magento\Framework\Code\Generator\Io
1720
*/
1821
private $ioObject;
22+
/**
23+
* @var Reader
24+
*/
25+
private $reader;
1926

2027
/**
2128
* Generator constructor.
2229
*
2330
* @param Generator\UnitTestFactory $testGenerator
2431
* @param Io $ioObject
32+
* @param Reader $reader
2533
*/
2634
public function __construct(
2735
Generator\UnitTestFactory $testGenerator,
28-
Io $ioObject
36+
Io $ioObject,
37+
Reader $reader
2938
) {
3039
$this->testGeneratorFactory = $testGenerator;
3140
$this->ioObject = $ioObject;
41+
$this->reader = $reader;
3242
}
3343

3444
/**
@@ -38,27 +48,74 @@ public function __construct(
3848
*/
3949
public function process(string $path)
4050
{
41-
$classes = \get_declared_classes();
42-
43-
require_once $path;
44-
45-
$diff = \array_diff(get_declared_classes(), $classes);
46-
$sourceClass = \end($diff) ?: '';
51+
$sourceClass = $this->getClassName($path);
52+
if (!\class_exists($sourceClass)) {
53+
return null;
54+
}
4755

4856
$resultClass = \explode('\\', trim($sourceClass, '\\'));
4957
\array_splice($resultClass, 2, 0, 'Test\\Unit');
5058
$resultClass = \implode('\\', $resultClass) . 'Test';
51-
52-
if (!\class_exists($sourceClass) || \class_exists($resultClass)) {
59+
if (\class_exists($resultClass)) {
5360
return null;
5461
}
55-
5662
$generator = $this->testGeneratorFactory->create([
5763
'sourceClassName' => $sourceClass,
5864
'resultClassName' => $resultClass,
59-
'ioObject' => $this->ioObject
65+
'ioObject' => $this->ioObject,
6066
]);
6167

6268
return $generator->generate();
6369
}
70+
71+
/**
72+
* @param string $path
73+
*
74+
* @return string
75+
* @throws \Magento\Framework\Exception\FileSystemException
76+
*/
77+
private function getClassName(string $path): string
78+
{
79+
if (\class_exists($path)) {
80+
return $path;
81+
}
82+
83+
$fileContents = $this->reader->fileGetContents($path);
84+
return $this->parseClassName($fileContents);
85+
}
86+
87+
/**
88+
* @param string $content
89+
*
90+
* @return string
91+
*/
92+
private function parseClassName(string $content): string
93+
{
94+
$class = $namespace = '';
95+
$i = 0;
96+
$tokens = \token_get_all($content);
97+
$tokensCount = \count($tokens);
98+
for (; $i < $tokensCount; $i++) {
99+
if ($tokens[$i][0] === T_NAMESPACE) {
100+
for ($j = $i + 1; $j < $tokensCount; $j++) {
101+
if ($tokens[$j][0] === T_STRING) {
102+
$namespace .= '\\' . $tokens[$j][1];
103+
} else {
104+
if ($tokens[$j] === '{' || $tokens[$j] === ';') {
105+
break;
106+
}
107+
}
108+
}
109+
}
110+
if ($tokens[$i][0] === T_CLASS) {
111+
for ($j = $i + 1; $j < $tokensCount; $j++) {
112+
if ($tokens[$j] === '{') {
113+
$class = '\\' . $tokens[$i + 2][1];
114+
break 2;
115+
}
116+
}
117+
}
118+
}
119+
return $namespace . $class;
120+
}
64121
}

Console/Command/Generate.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected function configure()
5858
$this->addArgument(
5959
self::ARGUMENT_PATH,
6060
InputArgument::REQUIRED,
61-
'Path to file to generate unit tests for'
61+
'Class name or path to file to generate unit tests for'
6262
);
6363
}
6464

0 commit comments

Comments
 (0)