Skip to content

Commit 82d32ea

Browse files
Merge remote-tracking branch 'origin/2.4-develop' into Arrows-AC-15800-Test_Helpers
2 parents 75c8464 + 297b774 commit 82d32ea

29 files changed

+3507
-120
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Authorization\Test\Unit\Helper;
9+
10+
use Magento\Authorization\Model\Role;
11+
12+
/**
13+
* Test helper for Magento\Authorization\Model\Role
14+
*
15+
* This helper provides only the custom GWS-related methods that are not available
16+
* in the parent Role class. Standard methods like getData(), setData(), and load()
17+
* are inherited from Magento\Framework\Model\AbstractModel.
18+
*/
19+
class RoleTestHelper extends Role
20+
{
21+
/**
22+
* @var array
23+
*/
24+
private $gwsWebsites = [];
25+
26+
/**
27+
* @var array
28+
*/
29+
private $gwsStoreGroups = [];
30+
31+
/**
32+
* @var bool
33+
*/
34+
private $gwsDataIsset = false;
35+
36+
/**
37+
* Skip parent constructor to avoid dependency injection requirements in tests
38+
*/
39+
public function __construct()
40+
{
41+
// Intentionally empty - avoids parent constructor dependencies
42+
}
43+
44+
/**
45+
* Get GWS websites
46+
*
47+
* @return array
48+
*/
49+
public function getGwsWebsites()
50+
{
51+
return $this->gwsWebsites;
52+
}
53+
54+
/**
55+
* Set GWS websites
56+
*
57+
* @param array $websites
58+
* @return $this
59+
*/
60+
public function setGwsWebsites($websites)
61+
{
62+
$this->gwsWebsites = $websites;
63+
return $this;
64+
}
65+
66+
/**
67+
* Get GWS store groups
68+
*
69+
* @return array
70+
*/
71+
public function getGwsStoreGroups()
72+
{
73+
return $this->gwsStoreGroups;
74+
}
75+
76+
/**
77+
* Set GWS store groups
78+
*
79+
* @param array $storeGroups
80+
* @return $this
81+
*/
82+
public function setGwsStoreGroups($storeGroups)
83+
{
84+
$this->gwsStoreGroups = $storeGroups;
85+
return $this;
86+
}
87+
88+
/**
89+
* Set GWS data isset flag
90+
*
91+
* @param bool $value
92+
* @return $this
93+
*/
94+
public function setGwsDataIsset($value)
95+
{
96+
$this->gwsDataIsset = $value;
97+
return $this;
98+
}
99+
100+
/**
101+
* Load role (overridden to avoid database access in tests)
102+
*
103+
* @param mixed $modelId
104+
* @param string|null $field
105+
* @return $this
106+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
107+
*/
108+
public function load($modelId, $field = null)
109+
{
110+
return $this;
111+
}
112+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Backend\Test\Unit\Helper;
9+
10+
use Magento\Backend\Test\Unit\App\Action\Stub\ActionStub;
11+
12+
/**
13+
* Test helper for ActionStub with additional test-specific methods
14+
*
15+
* This helper extends ActionStub to provide the setDirtyRulesNoticeMessage()
16+
* method which is used in production by CatalogRule controllers but doesn't
17+
* exist in the base ActionStub class. Since PHPUnit 12 removed addMethods(),
18+
* this helper is necessary to test code that calls this method.
19+
*
20+
* @SuppressWarnings(PHPMD.AllPurposeAction)
21+
*/
22+
class ActionStubTestHelper extends ActionStub
23+
{
24+
public function __construct()
25+
{
26+
// Skip parent constructor for testing
27+
}
28+
29+
/**
30+
* Stub method for testing CatalogRule controllers
31+
*
32+
* This method is called by AdminGws\Model\Controllers::promoCatalogIndexAction()
33+
* but doesn't exist in the base ActionStub class.
34+
*
35+
* @param string $message
36+
* @return $this
37+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
38+
*/
39+
public function setDirtyRulesNoticeMessage($message)
40+
{
41+
return $this;
42+
}
43+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Copyright 2016 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Backend\Test\Unit\Helper;
9+
10+
use Magento\Backend\Model\Session\Quote;
11+
12+
/**
13+
* Test helper for Session\Quote
14+
*
15+
* This helper extends the concrete Session\Quote class to provide
16+
* test-specific functionality without dependency injection issues.
17+
*/
18+
class SessionQuoteTestHelper extends Quote
19+
{
20+
/**
21+
* Constructor that skips parent initialization
22+
*/
23+
public function __construct()
24+
{
25+
// Skip parent constructor to avoid dependency injection issues
26+
}
27+
28+
/**
29+
* Get store ID
30+
*
31+
* @return int
32+
*/
33+
public function getStoreId()
34+
{
35+
return 1;
36+
}
37+
}

app/code/Magento/Backend/Test/Unit/Helper/SessionTestHelper.php

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,97 @@
88
namespace Magento\Backend\Test\Unit\Helper;
99

1010
use Magento\Backend\Model\Auth\Session;
11+
use Magento\Framework\Session\Test\Unit\Helper\StorageTestHelper;
12+
use Magento\User\Model\User;
1113

14+
/**
15+
* Test helper for creating Auth Session mocks with various methods
16+
*
17+
* This helper extends the concrete Auth\Session class directly, providing a clean
18+
* way to add test-specific methods without using anonymous classes.
19+
* Extends Auth\Session to be compatible with type hints requiring that specific class.
20+
*
21+
* METHODS PROVIDED:
22+
* - setUser() / getUser() - User management
23+
* - setIsLoggedIn() / isLoggedIn() - Login state management
24+
* - setSkipLoggingAction() - Logging control
25+
*/
1226
class SessionTestHelper extends Session
1327
{
1428
/**
15-
* @var mixed
29+
* @var User|null
1630
*/
17-
private $user;
18-
31+
private $user = null;
32+
33+
/**
34+
* @var bool
35+
*/
36+
private $isLoggedIn = false;
37+
1938
public function __construct()
2039
{
21-
// Skip parent constructor to avoid dependencies
40+
// Skip parent constructor for testing
41+
// Initialize storage with the StorageTestHelper
42+
$this->storage = new StorageTestHelper();
2243
}
23-
44+
2445
/**
25-
* @return mixed
46+
* Get user
47+
*
48+
* @return User|null
2649
*/
2750
public function getUser()
2851
{
2952
return $this->user;
3053
}
31-
54+
3255
/**
33-
* @param mixed $user
56+
* Set user
57+
*
58+
* @param User $user
3459
* @return $this
3560
*/
3661
public function setUser($user)
3762
{
3863
$this->user = $user;
64+
$this->isLoggedIn = true;
65+
return $this;
66+
}
67+
68+
/**
69+
* Set skip logging action
70+
*
71+
* @param mixed $value
72+
* @return $this
73+
*/
74+
public function setSkipLoggingAction($value)
75+
{
76+
// Store in parent's storage using magic method (via StorageTestHelper::__call)
77+
// phpcs:ignore Magento2.Functions.StaticFunction
78+
/** @phpstan-ignore-next-line - StorageTestHelper::__call handles dynamic methods */
79+
$this->storage->setSkipLoggingAction($value);
80+
return $this;
81+
}
82+
83+
/**
84+
* Check if user is logged in
85+
*
86+
* @return bool
87+
*/
88+
public function isLoggedIn()
89+
{
90+
return $this->isLoggedIn;
91+
}
92+
93+
/**
94+
* Set is logged in
95+
*
96+
* @param bool $value
97+
* @return $this
98+
*/
99+
public function setIsLoggedIn($value)
100+
{
101+
$this->isLoggedIn = $value;
39102
return $this;
40103
}
41104
}
42-

app/code/Magento/Catalog/Test/Unit/Helper/ProductTestHelper.php

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ class ProductTestHelper extends Product
5151
private $setOptionsCalled = false;
5252
/** @var array */
5353
private $setOptionsParams = [];
54+
/**
55+
* @var float
56+
*/
57+
private $cost;
5458

5559
/**
5660
* @var bool
@@ -240,17 +244,24 @@ protected function _construct()
240244
/**
241245
* Constructor
242246
*
243-
* @param mixed $resource Optional resource parameter or DataObject for URL
247+
* @param float $cost
248+
* @param ?DataObject $urlDataObject
249+
* @param mixed $resource
244250
*/
245-
public function __construct($resource = null)
246-
{
251+
public function __construct(
252+
$cost = 0.0,
253+
?DataObject $urlDataObject = null,
254+
$resource = null
255+
) {
247256
if ($resource instanceof DataObject) {
248257
$this->urlDataObject = $resource;
249258
$this->resource = null;
250259
} else {
251260
$this->resource = $resource;
252261
$this->urlDataObject = null;
253262
}
263+
264+
$this->cost = $cost;
254265
$this->_data = [];
255266
$this->testData = [];
256267
}
@@ -2501,4 +2512,40 @@ public function hasUrlDataObject()
25012512
{
25022513
return (bool)$this->urlDataObject;
25032514
}
2515+
2516+
/**
2517+
* Set is object new
2518+
*
2519+
* Convenience wrapper for parent's isObjectNew($flag) method.
2520+
*
2521+
* @param bool $flag
2522+
* @return $this
2523+
*/
2524+
public function setIsObjectNew($flag)
2525+
{
2526+
parent::isObjectNew($flag);
2527+
return $this;
2528+
}
2529+
2530+
/**
2531+
* Check if product has options
2532+
*
2533+
* This method is mocked by tests to control option behavior.
2534+
*
2535+
* @return bool
2536+
*/
2537+
public function hasOptions(): bool
2538+
{
2539+
return isset($this->_data['has_options']) && $this->_data['has_options'];
2540+
}
2541+
2542+
/**
2543+
* Get cost
2544+
*
2545+
* @return float
2546+
*/
2547+
public function getCost()
2548+
{
2549+
return $this->getData('cost') ?? $this->cost;
2550+
}
25042551
}

0 commit comments

Comments
 (0)