Skip to content

Commit 56c34ba

Browse files
[Magento Community Engineering] Community Contributions - 2.4-develop-express-lane-prs
- merged with '2.4-develop-fast-lane-prs' branch
2 parents 8f9a618 + 59289ca commit 56c34ba

File tree

7 files changed

+114
-23
lines changed

7 files changed

+114
-23
lines changed

dev/tests/integration/testsuite/Magento/Newsletter/Model/SubscriberTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,15 @@ public function testUnsubscribeSubscribe(): void
8989
$this->assertEquals($subscriber, $subscriber->unsubscribe());
9090
$this->assertContains(
9191
'You have been unsubscribed from the newsletter.',
92-
$this->transportBuilder->getSentMessage()->getRawMessage()
92+
$this->transportBuilder->getSentMessage()->getBody()->getParts()[0]->getRawContent()
9393
);
9494
$this->assertEquals(Subscriber::STATUS_UNSUBSCRIBED, $subscriber->getSubscriberStatus());
9595
// Subscribe and verify
9696
$this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $subscriber->subscribe('customer@example.com'));
9797
$this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $subscriber->getSubscriberStatus());
9898
$this->assertContains(
9999
'You have been successfully subscribed to our newsletter.',
100-
$this->transportBuilder->getSentMessage()->getRawMessage()
100+
$this->transportBuilder->getSentMessage()->getBody()->getParts()[0]->getRawContent()
101101
);
102102
}
103103

@@ -116,14 +116,14 @@ public function testUnsubscribeSubscribeByCustomerId(): void
116116
$this->assertEquals(Subscriber::STATUS_UNSUBSCRIBED, $subscriber->getSubscriberStatus());
117117
$this->assertContains(
118118
'You have been unsubscribed from the newsletter.',
119-
$this->transportBuilder->getSentMessage()->getRawMessage()
119+
$this->transportBuilder->getSentMessage()->getBody()->getParts()[0]->getRawContent()
120120
);
121121
// Subscribe and verify
122122
$this->assertSame($subscriber, $subscriber->subscribeCustomerById(1));
123123
$this->assertEquals(Subscriber::STATUS_SUBSCRIBED, $subscriber->getSubscriberStatus());
124124
$this->assertContains(
125125
'You have been successfully subscribed to our newsletter.',
126-
$this->transportBuilder->getSentMessage()->getRawMessage()
126+
$this->transportBuilder->getSentMessage()->getBody()->getParts()[0]->getRawContent()
127127
);
128128
}
129129

@@ -143,7 +143,7 @@ public function testConfirm(): void
143143
$subscriber->confirm($subscriber->getSubscriberConfirmCode());
144144
$this->assertContains(
145145
'You have been successfully subscribed to our newsletter.',
146-
$this->transportBuilder->getSentMessage()->getRawMessage()
146+
$this->transportBuilder->getSentMessage()->getBody()->getParts()[0]->getRawContent()
147147
);
148148
}
149149

dev/tests/integration/testsuite/Magento/ProductAlert/Model/EmailTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ public function testSend($isCustomerIdUsed)
101101
$this->_emailModel->send();
102102

103103
$this->assertContains(
104-
'Smith,',
105-
$this->transportBuilder->getSentMessage()->getRawMessage()
104+
'John Smith,',
105+
$this->transportBuilder->getSentMessage()->getBody()->getParts()[0]->getRawContent()
106106
);
107107
}
108108

dev/tests/integration/testsuite/Magento/ProductAlert/Model/ObserverTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ public function testProcess()
7070
{
7171
$this->observer->process();
7272
$this->assertContains(
73-
'ohn Smith,',
74-
$this->transportBuilder->getSentMessage()->getRawMessage()
73+
'John Smith,',
74+
$this->transportBuilder->getSentMessage()->getBody()->getParts()[0]->getRawContent()
7575
);
7676
}
7777

dev/tests/static/framework/Magento/TestFramework/Dependency/PhpRule.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Framework\App\Utility\Files;
1313
use Magento\Framework\Exception\LocalizedException;
1414
use Magento\Framework\UrlInterface;
15+
use Magento\TestFramework\Dependency\Reader\ClassScanner;
1516
use Magento\TestFramework\Dependency\Route\RouteMapper;
1617
use Magento\TestFramework\Exception\NoSuchActionException;
1718

@@ -79,24 +80,33 @@ class PhpRule implements RuleInterface
7980
*/
8081
private $whitelists;
8182

83+
/**
84+
* @var ClassScanner
85+
*/
86+
private $classScanner;
87+
8288
/**
8389
* @param array $mapRouters
8490
* @param array $mapLayoutBlocks
8591
* @param array $pluginMap
8692
* @param array $whitelists
87-
* @throws \Exception
93+
* @param ClassScanner|null $classScanner
94+
*
95+
* @throws LocalizedException
8896
*/
8997
public function __construct(
9098
array $mapRouters,
9199
array $mapLayoutBlocks,
92100
array $pluginMap = [],
93-
array $whitelists = []
101+
array $whitelists = [],
102+
ClassScanner $classScanner = null
94103
) {
95104
$this->_mapRouters = $mapRouters;
96105
$this->_mapLayoutBlocks = $mapLayoutBlocks;
97106
$this->pluginMap = $pluginMap ?: null;
98107
$this->routeMapper = new RouteMapper();
99108
$this->whitelists = $whitelists;
109+
$this->classScanner = $classScanner ?? new ClassScanner();
100110
}
101111

102112
/**
@@ -176,7 +186,7 @@ private function caseClassesAndIdentifiers($currentModule, $file, &$contents)
176186
if (empty($matches['class_inside_module'][$i]) && !empty($matches['module_scoped_key'][$i])) {
177187
$dependencyType = RuleInterface::TYPE_SOFT;
178188
} else {
179-
$currentClass = $this->getClassFromFilepath($file, $currentModule);
189+
$currentClass = $this->getClassFromFilepath($file);
180190
$dependencyType = $this->isPluginDependency($currentClass, $dependencyClass)
181191
? RuleInterface::TYPE_SOFT
182192
: RuleInterface::TYPE_HARD;
@@ -196,14 +206,11 @@ private function caseClassesAndIdentifiers($currentModule, $file, &$contents)
196206
* Get class name from filename based on class/file naming conventions
197207
*
198208
* @param string $filepath
199-
* @param string $module
200209
* @return string
201210
*/
202-
private function getClassFromFilepath($filepath, $module)
211+
private function getClassFromFilepath(string $filepath): string
203212
{
204-
$class = strstr($filepath, str_replace(['_', '\\', '/'], DIRECTORY_SEPARATOR, $module));
205-
$class = str_replace(DIRECTORY_SEPARATOR, '\\', strstr($class, '.php', true));
206-
return $class;
213+
return $this->classScanner->getClassName($filepath);
207214
}
208215

209216
/**
@@ -267,7 +274,8 @@ private function isPluginDependency($dependent, $dependency)
267274
if ($subject === $dependency) {
268275
return true;
269276
} elseif ($subject) {
270-
$subjectModule = substr($subject, 0, strpos($subject, '\\', 9)); // (strlen('Magento\\') + 1) === 9
277+
$moduleNameLength = strpos($subject, '\\', strpos($subject, '\\') + 1);
278+
$subjectModule = substr($subject, 0, $moduleNameLength);
271279
return strpos($dependency, $subjectModule) === 0;
272280
} else {
273281
return false;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\TestFramework\Dependency\Reader;
9+
10+
use Magento\Setup\Module\Di\Code\Reader\FileClassScanner;
11+
12+
/**
13+
* Search classes in file by path.
14+
*/
15+
class ClassScanner
16+
{
17+
/**
18+
* @var string[]
19+
*/
20+
private $classNames = [];
21+
22+
/**
23+
* Get class name by file name.
24+
*
25+
* @param string $filePath
26+
*
27+
* @return string
28+
*/
29+
public function getClassName(string $filePath): string
30+
{
31+
if (!isset($this->classNames[$filePath])) {
32+
$this->classNames[$filePath] = $this->loadClassName($filePath);
33+
}
34+
35+
return $this->classNames[$filePath];
36+
}
37+
38+
/**
39+
* Load class name from file.
40+
*
41+
* @param string $filePath
42+
*
43+
* @return string
44+
*/
45+
private function loadClassName(string $filePath): string
46+
{
47+
$scanner = new FileClassScanner($filePath);
48+
return $scanner->getClassName();
49+
}
50+
}

dev/tests/static/framework/autoload.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@
3434

3535
$generatedCode = DirectoryList::getDefaultConfig()[DirectoryList::GENERATED_CODE][DirectoryList::PATH];
3636
$autoloadWrapper->addPsr4('Magento\\', $baseDir . '/' . $generatedCode . '/Magento/');
37+
38+
$setup = DirectoryList::getDefaultConfig()[DirectoryList::SETUP][DirectoryList::PATH];
39+
$autoloadWrapper->addPsr4('Magento\\Setup\\', $baseDir . '/' . $setup . '/Magento/Setup/');

dev/tests/static/framework/tests/unit/testsuite/Magento/TestFramework/Dependency/PhpRuleTest.php

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Framework\Exception\LocalizedException;
99
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
10+
use Magento\TestFramework\Dependency\Reader\ClassScanner;
1011
use Magento\TestFramework\Exception\NoSuchActionException;
1112

1213
/**
@@ -24,6 +25,11 @@ class PhpRuleTest extends \PHPUnit\Framework\TestCase
2425
*/
2526
private $objectManagerHelper;
2627

28+
/**
29+
* @var ClassScanner
30+
*/
31+
private $classScanner;
32+
2733
/**
2834
* @inheritDoc
2935
* @throws \Exception
@@ -39,13 +45,16 @@ protected function setUp()
3945
$whitelist = [];
4046

4147
$this->objectManagerHelper = new ObjectManagerHelper($this);
48+
$this->classScanner = $this->createMock(ClassScanner::class);
49+
4250
$this->model = $this->objectManagerHelper->getObject(
4351
PhpRule::class,
4452
[
4553
'mapRouters' => $mapRoutes,
4654
'mapLayoutBlocks' => $mapLayoutBlocks,
4755
'pluginMap' => $pluginMap,
4856
'whitelists' => $whitelist,
57+
'classScanner' => $this->classScanner
4958
]
5059
);
5160
}
@@ -62,14 +71,20 @@ public function testNonPhpGetDependencyInfo()
6271
/**
6372
* @param string $class
6473
* @param string $content
74+
* @param int $expectedScans
6575
* @param array $expected
66-
* @dataProvider getDependencyInfoDataProvider
76+
*
6777
* @throws \Exception
78+
* @dataProvider getDependencyInfoDataProvider
6879
*/
69-
public function testGetDependencyInfo($class, $content, array $expected)
80+
public function testGetDependencyInfo(string $class, string $content, int $expectedScans, array $expected): void
7081
{
7182
$file = $this->makeMockFilepath($class);
7283
$module = $this->getModuleFromClass($class);
84+
$this->classScanner->expects($this->exactly($expectedScans))
85+
->method('getClassName')
86+
->with($file)
87+
->willReturn($class);
7388
$this->assertEquals($expected, $this->model->getDependencyInfo($module, 'php', $file, $content));
7489
}
7590

@@ -82,11 +97,13 @@ public function getDependencyInfoDataProvider()
8297
'Extend class in same module' => [
8398
'Magento\SomeModule\SomeClass',
8499
'something extends \Magento\SomeModule\Any\ClassName {',
100+
0,
85101
[]
86102
],
87103
'Extend class in different module' => [
88104
'Magento\AnotherModule\SomeClass',
89105
'something extends \Magento\SomeModule\Any\ClassName {',
106+
1,
90107
[
91108
[
92109
'modules' => ['Magento\SomeModule'],
@@ -98,11 +115,13 @@ public function getDependencyInfoDataProvider()
98115
'getViewFileUrl in same module' => [
99116
'Magento\SomeModule\SomeClass',
100117
'$this->getViewFileUrl("Magento_SomeModule::js/order-by-sku-failure.js")',
118+
0,
101119
[]
102120
],
103121
'getViewFileUrl in different module' => [
104122
'Magento\AnotherModule\SomeClass',
105123
'$this->getViewFileUrl("Magento_SomeModule::js/order-by-sku-failure.js")',
124+
1,
106125
[
107126
[
108127
'modules' => ['Magento\SomeModule'],
@@ -114,11 +133,13 @@ public function getDependencyInfoDataProvider()
114133
'Helper class from same module' => [
115134
'Magento\SomeModule\SomeClass',
116135
'$this->helper("Magento\SomeModule\Any\ClassName")',
136+
0,
117137
[]
118138
],
119139
'Helper class from another module' => [
120140
'Magento\AnotherModule\SomeClass',
121141
'$this->helper("Magento\SomeModule\Any\ClassName")',
142+
1,
122143
[
123144
[
124145
'modules' => ['Magento\SomeModule'],
@@ -129,11 +150,14 @@ public function getDependencyInfoDataProvider()
129150
],
130151
'getBlock from same module' => [
131152
'Magento\SomeModule\SomeClass',
132-
'$this->getLayout()->getBlock(\'block.name\');', []
153+
'$this->getLayout()->getBlock(\'block.name\');',
154+
0,
155+
[]
133156
],
134157
'getBlock from another module' => [
135158
'Magento\AnotherModule\SomeClass',
136159
'$this->getLayout()->getBlock(\'block.name\');',
160+
0,
137161
[
138162
[
139163
'modules' => ['Magento\SomeModule'],
@@ -145,16 +169,19 @@ public function getDependencyInfoDataProvider()
145169
'Plugin on class in same module' => [
146170
'Magento\Module1\Plugin1',
147171
', \Magento\Module1\Subject $variable',
172+
0,
148173
[]
149174
],
150175
'Plugin depends on arbitrary class in same module' => [
151176
'Magento\Module1\Plugin1',
152177
', \Magento\Module1\NotSubject $variable',
178+
0,
153179
[]
154180
],
155181
'Plugin on class in different module' => [
156182
'Magento\Module1\Plugin2',
157183
'Magento\Module2\Subject',
184+
1,
158185
[
159186
[
160187
'modules' => ['Magento\Module2'],
@@ -166,6 +193,7 @@ public function getDependencyInfoDataProvider()
166193
'Plugin depends on arbitrary class in same module as subject' => [
167194
'Magento\Module1\Plugin2',
168195
'Magento\Module2\NotSubject',
196+
1,
169197
[
170198
[
171199
'modules' => ['Magento\Module2'],
@@ -177,6 +205,7 @@ public function getDependencyInfoDataProvider()
177205
'Plugin depends on arbitrary class in arbitrary module' => [
178206
'Magento\Module1\Plugin2',
179207
'Magento\OtherModule\NotSubject',
208+
1,
180209
[
181210
[
182211
'modules' => ['Magento\OtherModule'],
@@ -322,8 +351,9 @@ private function makeMockFilepath($class)
322351
* @param string $class
323352
* @return string
324353
*/
325-
private function getModuleFromClass($class)
354+
private function getModuleFromClass(string $class): string
326355
{
327-
return substr($class, 0, strpos($class, '\\', 9)); // (strlen('Magento\\') + 1) === 9
356+
$moduleNameLength = strpos($class, '\\', strpos($class, '\\') + 1);
357+
return substr($class, 0, $moduleNameLength);
328358
}
329359
}

0 commit comments

Comments
 (0)