4
4
5
5
namespace Olmer \UnitTestsGenerator \Code ;
6
6
7
- use Magento \Framework \Code \Generator \Io ;
7
+ use Magento \Framework \{
8
+ Code \Generator \Io ,
9
+ Filesystem \Driver \File as Reader
10
+ };
8
11
9
12
class Generator
10
13
{
@@ -16,19 +19,26 @@ class Generator
16
19
* @var \Magento\Framework\Code\Generator\Io
17
20
*/
18
21
private $ ioObject ;
22
+ /**
23
+ * @var Reader
24
+ */
25
+ private $ reader ;
19
26
20
27
/**
21
28
* Generator constructor.
22
29
*
23
30
* @param Generator\UnitTestFactory $testGenerator
24
31
* @param Io $ioObject
32
+ * @param Reader $reader
25
33
*/
26
34
public function __construct (
27
35
Generator \UnitTestFactory $ testGenerator ,
28
- Io $ ioObject
36
+ Io $ ioObject ,
37
+ Reader $ reader
29
38
) {
30
39
$ this ->testGeneratorFactory = $ testGenerator ;
31
40
$ this ->ioObject = $ ioObject ;
41
+ $ this ->reader = $ reader ;
32
42
}
33
43
34
44
/**
@@ -38,27 +48,74 @@ public function __construct(
38
48
*/
39
49
public function process (string $ path )
40
50
{
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
+ }
47
55
48
56
$ resultClass = \explode ('\\' , trim ($ sourceClass , '\\' ));
49
57
\array_splice ($ resultClass , 2 , 0 , 'Test \\Unit ' );
50
58
$ resultClass = \implode ('\\' , $ resultClass ) . 'Test ' ;
51
-
52
- if (!\class_exists ($ sourceClass ) || \class_exists ($ resultClass )) {
59
+ if (\class_exists ($ resultClass )) {
53
60
return null ;
54
61
}
55
-
56
62
$ generator = $ this ->testGeneratorFactory ->create ([
57
63
'sourceClassName ' => $ sourceClass ,
58
64
'resultClassName ' => $ resultClass ,
59
- 'ioObject ' => $ this ->ioObject
65
+ 'ioObject ' => $ this ->ioObject ,
60
66
]);
61
67
62
68
return $ generator ->generate ();
63
69
}
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
+ }
64
121
}
0 commit comments