@@ -22,33 +22,53 @@ class PublicCodeTest extends \PHPUnit\Framework\TestCase
22
22
'$this ' , 'void ' , 'string ' , 'int ' , 'bool ' , 'boolean ' , 'integer ' , 'null '
23
23
];
24
24
25
+ /**
26
+ * @var string[]|null
27
+ */
28
+ private $ blockWhitelist ;
29
+
30
+ /**
31
+ * Return whitelist class names
32
+ *
33
+ * @return string[]
34
+ */
35
+ private function getWhitelist (): array
36
+ {
37
+ if ($ this ->blockWhitelist === null ) {
38
+ $ whiteListFiles = str_replace (
39
+ '\\' ,
40
+ '/ ' ,
41
+ realpath (__DIR__ ) . '/_files/whitelist/public_code*.txt '
42
+ );
43
+ $ whiteListItems = [];
44
+ foreach (glob ($ whiteListFiles ) as $ fileName ) {
45
+ $ whiteListItems = array_merge (
46
+ $ whiteListItems ,
47
+ file ($ fileName , FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES )
48
+ );
49
+ }
50
+ $ this ->blockWhitelist = $ whiteListItems ;
51
+ }
52
+ return $ this ->blockWhitelist ;
53
+ }
54
+
25
55
/**
26
56
* Since blocks can be referenced from templates, they should be stable not to break theme customizations.
27
57
* So all blocks should be @api annotated. This test checks that all blocks declared in layout files are public
28
58
*
29
59
* @param $layoutFile
60
+ * @throws \ReflectionException
30
61
* @dataProvider layoutFilesDataProvider
31
62
*/
32
63
public function testAllBlocksReferencedInLayoutArePublic ($ layoutFile )
33
64
{
34
- // A block can be whitelisted and thus not be required to be public
35
- $ whiteListFiles = str_replace ('\\' , '/ ' , realpath (__DIR__ ))
36
- . '/_files/whitelist/public_code*.txt ' ;
37
- $ whiteListBlocks = [];
38
- foreach (glob ($ whiteListFiles ) as $ fileName ) {
39
- $ whiteListBlocks = array_merge (
40
- $ whiteListBlocks ,
41
- file ($ fileName , FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES )
42
- );
43
- }
44
-
45
65
$ nonPublishedBlocks = [];
46
66
$ xml = simplexml_load_file ($ layoutFile );
47
67
$ elements = $ xml ->xpath ('//block | //referenceBlock ' ) ?: [];
48
68
/** @var $node \SimpleXMLElement */
49
69
foreach ($ elements as $ node ) {
50
70
$ class = (string ) $ node ['class ' ];
51
- if ($ class && \class_exists ($ class ) && !in_array ($ class , $ whiteListBlocks )) {
71
+ if ($ class && \class_exists ($ class ) && !in_array ($ class , $ this -> getWhitelist () )) {
52
72
$ reflection = (new \ReflectionClass ($ class ));
53
73
if (strpos ($ reflection ->getDocComment (), '@api ' ) === false ) {
54
74
$ nonPublishedBlocks [] = $ class ;
@@ -67,6 +87,7 @@ public function testAllBlocksReferencedInLayoutArePublic($layoutFile)
67
87
* Find all layout update files in magento modules and themes.
68
88
*
69
89
* @return array
90
+ * @throws \Exception
70
91
*/
71
92
public function layoutFilesDataProvider ()
72
93
{
@@ -79,8 +100,8 @@ public function layoutFilesDataProvider()
79
100
* This test walks through all public PHP types and makes sure that all their method arguments
80
101
* and return values are public types.
81
102
*
82
- *
83
103
* @param string $class
104
+ * @throws \ReflectionException
84
105
* @dataProvider publicPHPTypesDataProvider
85
106
*/
86
107
public function testAllPHPClassesReferencedFromPublicClassesArePublic ($ class )
@@ -121,6 +142,7 @@ public function testAllPHPClassesReferencedFromPublicClassesArePublic($class)
121
142
/**
122
143
* Retrieve list of all interfaces and classes in Magento codebase that are marked with @api annotation.
123
144
* @return array
145
+ * @throws \Exception
124
146
*/
125
147
public function publicPHPTypesDataProvider ()
126
148
{
@@ -130,7 +152,9 @@ public function publicPHPTypesDataProvider()
130
152
$ fileContents = \file_get_contents ($ file );
131
153
if (strpos ($ fileContents , '@api ' ) !== false ) {
132
154
foreach ($ this ->getDeclaredClassesAndInterfaces ($ file ) as $ class ) {
133
- if (class_exists ($ class ->getName ()) || interface_exists ($ class ->getName ())) {
155
+ if (!in_array ($ class ->getName (), $ this ->getWhitelist ())
156
+ && (class_exists ($ class ->getName ()) || interface_exists ($ class ->getName ()))
157
+ ) {
134
158
$ result [$ class ->getName ()] = [$ class ->getName ()];
135
159
}
136
160
}
0 commit comments