Skip to content

Commit cca101e

Browse files
committed
Merge remote-tracking branch 'karyna/php8-compatibility/add-test-data-for-tests-based-on-tokens' into platform-health
2 parents a9f0b47 + 76e3a7e commit cca101e

File tree

3 files changed

+144
-60
lines changed

3 files changed

+144
-60
lines changed

dev/tests/static/framework/tests/unit/testsuite/Magento/TestFramework/Integrity/Library/PhpParser/StaticCallsTest.php

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
*/
66
namespace Magento\TestFramework\Integrity\Library\PhpParser;
77

8+
use PHPUnit\Framework\TestCase;
9+
810
/**
11+
* Check static calls parsing
912
*/
10-
class StaticCallsTest extends \PHPUnit\Framework\TestCase
13+
class StaticCallsTest extends TestCase
1114
{
1215
/**
1316
* @var StaticCalls
@@ -32,54 +35,66 @@ protected function setUp(): void
3235
/**
3336
* Test get static call dependencies
3437
*
35-
* @test
38+
* @dataProvider tokensDataProvider
3639
*/
37-
public function testGetDependencies()
40+
public function testGetDependencies(array $tokens)
3841
{
39-
$tokens = [
40-
0 => [T_WHITESPACE, ' '],
41-
1 => [T_NS_SEPARATOR, '\\'],
42-
2 => [T_STRING, 'Object'],
43-
3 => [T_PAAMAYIM_NEKUDOTAYIM, '::'],
44-
];
45-
46-
$this->tokens->expects($this->any())->method('getPreviousToken')->willReturnCallback(
47-
42+
$this->tokens->method('getPreviousToken')
43+
->willReturnCallback(
4844
function ($k) use ($tokens) {
4945
return $tokens[$k - 1];
5046
}
47+
);
5148

52-
);
53-
54-
$this->tokens->expects($this->any())->method('getTokenCodeByKey')->willReturnCallback(
55-
49+
$this->tokens->method('getTokenCodeByKey')
50+
->willReturnCallback(
5651
function ($k) use ($tokens) {
5752
return $tokens[$k][0];
5853
}
54+
);
5955

60-
);
61-
62-
$this->tokens->expects($this->any())->method('getTokenValueByKey')->willReturnCallback(
63-
56+
$this->tokens->method('getTokenValueByKey')
57+
->willReturnCallback(
6458
function ($k) use ($tokens) {
6559
return $tokens[$k][1];
6660
}
61+
);
6762

68-
);
69-
70-
$throws = new StaticCalls($this->tokens);
63+
$staticCalls = new StaticCalls($this->tokens);
7164
foreach ($tokens as $k => $token) {
72-
$throws->parse($token, $k);
65+
$staticCalls->parse($token, $k);
7366
}
7467

7568
$uses = $this->getMockBuilder(
7669
\Magento\TestFramework\Integrity\Library\PhpParser\Uses::class
7770
)->disableOriginalConstructor()->getMock();
7871

79-
$uses->expects($this->once())->method('hasUses')->willReturn(true);
80-
81-
$uses->expects($this->once())->method('getClassNameWithNamespace')->willReturn('\Object');
72+
$this->assertEquals(['\Object'], $staticCalls->getDependencies($uses));
73+
}
8274

83-
$this->assertEquals(['\Object'], $throws->getDependencies($uses));
75+
/**
76+
* different tokens data for php 7 and php 8
77+
*
78+
* @return array
79+
*/
80+
public function tokensDataProvider(): array
81+
{
82+
return [
83+
'PHP 7' => [
84+
[
85+
0 => [T_WHITESPACE, ' '],
86+
1 => [T_NS_SEPARATOR, '\\'],
87+
2 => [T_STRING, 'Object'],
88+
3 => [T_PAAMAYIM_NEKUDOTAYIM, '::'],
89+
]
90+
],
91+
'PHP 8' => [
92+
[
93+
0 => [T_WHITESPACE, ' '],
94+
1 => [T_NAME_FULLY_QUALIFIED, '\\Object'],
95+
2 => [T_PAAMAYIM_NEKUDOTAYIM, '::'],
96+
]
97+
]
98+
];
8499
}
85100
}

dev/tests/static/framework/tests/unit/testsuite/Magento/TestFramework/Integrity/Library/PhpParser/ThrowsTest.php

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
*/
66
namespace Magento\TestFramework\Integrity\Library\PhpParser;
77

8+
use PHPUnit\Framework\TestCase;
9+
810
/**
11+
* Check throws parsing
912
*/
10-
class ThrowsTest extends \PHPUnit\Framework\TestCase
13+
class ThrowsTest extends TestCase
1114
{
1215
/**
1316
* @var Throws
@@ -32,35 +35,23 @@ protected function setUp(): void
3235
/**
3336
* Test get throws dependencies
3437
*
35-
* @test
38+
* @dataProvider tokensDataProvider
3639
*/
37-
public function testGetDependencies()
40+
public function testGetDependencies(array $tokens)
3841
{
39-
$tokens = [
40-
0 => [T_THROW, 'throw'],
41-
1 => [T_WHITESPACE, ' '],
42-
2 => [T_NEW, 'new'],
43-
3 => [T_WHITESPACE, ' '],
44-
4 => [T_NS_SEPARATOR, '\\'],
45-
5 => [T_STRING, 'Exception'],
46-
6 => '(',
47-
];
48-
49-
$this->tokens->expects($this->any())->method('getTokenCodeByKey')->willReturnCallback(
50-
42+
$this->tokens->method('getTokenCodeByKey')
43+
->willReturnCallback(
5144
function ($k) use ($tokens) {
5245
return $tokens[$k][0];
5346
}
47+
);
5448

55-
);
56-
57-
$this->tokens->expects($this->any())->method('getTokenValueByKey')->willReturnCallback(
58-
49+
$this->tokens->method('getTokenValueByKey')
50+
->willReturnCallback(
5951
function ($k) use ($tokens) {
6052
return $tokens[$k][1];
6153
}
62-
63-
);
54+
);
6455

6556
$throws = new Throws($this->tokens);
6657
foreach ($tokens as $k => $token) {
@@ -71,10 +62,38 @@ function ($k) use ($tokens) {
7162
\Magento\TestFramework\Integrity\Library\PhpParser\Uses::class
7263
)->disableOriginalConstructor()->getMock();
7364

74-
$uses->expects($this->once())->method('hasUses')->willReturn(true);
75-
76-
$uses->expects($this->once())->method('getClassNameWithNamespace')->willReturn('\Exception');
77-
7865
$this->assertEquals(['\Exception'], $throws->getDependencies($uses));
7966
}
67+
68+
/**
69+
* different tokens data for php 7 and php 8
70+
*
71+
* @return array
72+
*/
73+
public function tokensDataProvider(): array
74+
{
75+
return [
76+
'PHP 7' => [
77+
[
78+
0 => [T_THROW, 'throw'],
79+
1 => [T_WHITESPACE, ' '],
80+
2 => [T_NEW, 'new'],
81+
3 => [T_WHITESPACE, ' '],
82+
4 => [T_NS_SEPARATOR, '\\'],
83+
5 => [T_STRING, 'Exception'],
84+
6 => '(',
85+
]
86+
],
87+
'PHP 8' => [
88+
[
89+
0 => [T_THROW, 'throw'],
90+
1 => [T_WHITESPACE, ' '],
91+
2 => [T_NEW, 'new'],
92+
3 => [T_WHITESPACE, ' '],
93+
4 => [T_NAME_FULLY_QUALIFIED, '\\Exception'],
94+
6 => '(',
95+
]
96+
]
97+
];
98+
}
8099
}

dev/tests/static/framework/tests/unit/testsuite/Magento/TestFramework/Integrity/Library/PhpParser/UsesTest.php

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\TestFramework\Integrity\Library\PhpParser;
77

88
/**
9+
* Check Uses parsing
910
*/
1011
class UsesTest extends \PHPUnit\Framework\TestCase
1112
{
@@ -43,10 +44,10 @@ public function testHasUses($tokens)
4344
*
4445
* @return array
4546
*/
46-
public function hasUsesDataProvider()
47+
public function hasUsesDataProvider(): array
4748
{
4849
return [
49-
'simple' => [
50+
'simple_php7' => [
5051
[
5152
0 => [T_USE, 'use '],
5253
1 => [T_STRING, 'Magento'],
@@ -59,7 +60,7 @@ public function hasUsesDataProvider()
5960
8 => ';',
6061
],
6162
],
62-
'several_simple' => [
63+
'several_simple_php7' => [
6364
[
6465
0 => [T_USE, 'use '],
6566
1 => [T_STRING, 'Magento'],
@@ -83,7 +84,7 @@ public function hasUsesDataProvider()
8384
19 => ';',
8485
],
8586
],
86-
'several_with_comma_separate' => [
87+
'several_with_comma_separate_php7' => [
8788
[
8889
0 => [T_USE, 'use '],
8990
1 => [T_STRING, 'Magento'],
@@ -105,6 +106,36 @@ public function hasUsesDataProvider()
105106
17 => [T_STRING, 'OtherObject'],
106107
18 => ';',
107108
],
109+
],
110+
'simple' => [
111+
[
112+
0 => [T_USE, 'use '],
113+
1 => [T_NAME_QUALIFIED, 'Magento\\Core\\Model\\Object'],
114+
2 => ';',
115+
],
116+
],
117+
'several_simple' => [
118+
[
119+
0 => [T_USE, 'use '],
120+
1 => [T_NAME_QUALIFIED, 'Magento\\Core\\Model\\Object'],
121+
2 => ';',
122+
3 => [T_USE, 'use '],
123+
4 => [T_NAME_QUALIFIED, 'Magento\\Core\\Model\\Object2'],
124+
5 => [T_AS, 'as '],
125+
6 => [T_STRING, 'OtherObject'],
126+
7 => ';',
127+
],
128+
],
129+
'several_with_comma_separate' => [
130+
[
131+
0 => [T_USE, 'use '],
132+
1 => [T_NAME_QUALIFIED, 'Magento\\Core\\Model\\Object'],
133+
8 => ',',
134+
5 => [T_NAME_QUALIFIED, 'Magento\\Core\\Model\\Object2'],
135+
16 => [T_AS, 'as '],
136+
17 => [T_STRING, 'OtherObject'],
137+
18 => ';',
138+
],
108139
]
109140
];
110141
}
@@ -142,10 +173,10 @@ public function testGetClassNameWithNamespace($className, $tokens)
142173
*
143174
* @return array
144175
*/
145-
public function classNamesDataProvider()
176+
public function classNamesDataProvider(): array
146177
{
147178
return [
148-
'class_from_uses' => [
179+
'class_from_uses_php7' => [
149180
'Object2',
150181
[
151182
0 => [T_USE, 'use '],
@@ -159,7 +190,7 @@ public function classNamesDataProvider()
159190
8 => ';'
160191
],
161192
],
162-
'class_from_uses_with_as' => [
193+
'class_from_uses_with_as_php7' => [
163194
'ObjectOther',
164195
[
165196
0 => [T_USE, 'use '],
@@ -174,6 +205,25 @@ public function classNamesDataProvider()
174205
9 => [T_STRING, 'ObjectOther'],
175206
10 => ';'
176207
],
208+
],
209+
'class_from_uses' => [
210+
'Object2',
211+
[
212+
0 => [T_USE, 'use '],
213+
1 => [T_NAME_QUALIFIED, 'Magento\\Core\\Model\\Object2'],
214+
2 => ';'
215+
],
216+
],
217+
'class_from_uses_with_as' => [
218+
'ObjectOther',
219+
[
220+
0 => [T_USE, 'use '],
221+
1 => [T_NAME_QUALIFIED, 'Magento\\Core\\Model\\Object2'],
222+
2 => [T_WHITESPACE, ' '],
223+
3 => [T_AS, 'as '],
224+
4 => [T_STRING, 'ObjectOther'],
225+
5 => ';'
226+
]
177227
]
178228
];
179229
}

0 commit comments

Comments
 (0)