Skip to content

Commit 1b1c592

Browse files
committed
chore: update examples to modern PHP versions
1 parent 153b2f8 commit 1b1c592

File tree

1 file changed

+80
-19
lines changed

1 file changed

+80
-19
lines changed

README.md

Lines changed: 80 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,53 +18,114 @@ composer require --dev ecomdev/magento2-test-essentials
1818

1919
## Examples
2020

21-
### Testing functionality that tries to resolve a default store
21+
### Testing Class with Store and Configuration
2222

23+
Imagine we have class that depending on current store configuration value view returns different values
24+
Usually you would mock every dependency call in it, which is error-prone as you never with dependency behaviour.
25+
26+
**YourService.php**
2327
```php
24-
use EcomDev\Magento2TestEssentials\ObjectManager;
25-
use EcomDev\Magento2TestEssentials\Store\StoreManager;
26-
use EcomDev\Magento2TestEssentials\Store\Store;
2728
use Magento\Store\Model\StoreManagerInterface;
29+
use Magento\Store\Model\ScopeInterface;
30+
use Magento\Framework\App\Config\ScopeConfigInterface;
2831
use Magento\Catalog\Model\Product;
2932

30-
class YourTestedClass
33+
class YourService
3134
{
3235
private $storeManager;
3336

34-
public function __construct(StoreManagerInterface $storeManager)
37+
public function __construct(
38+
private readonly StoreManagerInterface $storeManager,
39+
private readonly ScopeConfigInterface $scopeConfig
40+
)
3541
{
36-
$this->storeManager = $storeManager;
3742
}
3843

3944
public function applyCurrentStoreToProduct(Product $product)
4045
{
41-
$product->setStoreId($this->storeManager->getStore()->getId());
46+
$currentStore = $this->storeManager->getStore();
47+
48+
if ($aliasStore = $this->scopeConfig->getValue(
49+
'catalog/product/store_alias',
50+
ScopeInterface::SCOPE_STORE,
51+
$currentStore->getCode()
52+
)) {
53+
$product->setStoreId($this->storeManager->getStore($aliasStore)->getId());
54+
}
4255
}
4356
}
57+
```
58+
59+
60+
```php
61+
use PHPUnit\Framework\TestCase;
62+
use PHPUnit\Framework\Attributes\Test;
63+
use EcomDev\Magento2TestEssentials\ScopeConfig;
64+
use EcomDev\Magento2TestEssentials\Store\StoreManager;
65+
use EcomDev\Magento2TestEssentials\ObjectManager;
66+
use Magento\Catalog\Block\Product;
4467

45-
class YourTestedClassTest extends \PHPUnit\Framework\TestCase
68+
class YourServiceTest extends TestCase
4669
{
47-
/** @test */
48-
public function appliesStoreIdToProduct()
49-
{
50-
$applier = new YourTestedClass(
51-
StoreManager::new()
70+
private ObjectManager $objectManager;
71+
72+
protected function setUp() : void
73+
{
74+
$this->objectManager = ObjectManager::new()
75+
->withObject(StoreManager::class, StoreManager::new()
5276
->withStore(Store::new(3, 'store_three'))
53-
->setCurrentStore('store_three')
77+
->withStore(Store::new(1, 'store_one'))
78+
->setCurrentStore('store_three'))
79+
->withFactory(ScopeConfig::class, static ($objectManager) => ScopeConfig::new()
80+
->withStoreManager($objectManager->get(StoreManager::class))
81+
->withStoreValue('store_three', 'catalog/product/store_alias', 1));
82+
}
83+
84+
#[Test]
85+
public function appliesStoreIdToProductWhenFlagIsSet()
86+
{
87+
$service = new YourService(
88+
$this->objectManager->get(StoreManager::class),
89+
$this->objectManager->get(ScopeConfig::class)
5490
);
5591

56-
// Creates product without any constructor but if you rely on data fields, it works great
57-
$product = ObjectManager::new()->get(Product::class);
92+
// Creates product without any constructor
93+
// but if you rely on data fields, it works great
94+
$product = $this->objectManager->create(Product::class);
95+
96+
$applier->applyCurrentStoreToProduct(
97+
$product
98+
);
99+
100+
$this->assertEquals(1, $product->getStoreId());
101+
}
102+
103+
#[Test]
104+
public function keepsOriginalStoreId()
105+
{
106+
$service = new YourService(
107+
$this->objectManager->get(StoreManager::class)
108+
->setCurrentStore(1),
109+
$this->objectManager->get(ScopeConfig::class)
110+
);
58111

112+
$product = $this->objectManager->create(Product::class);
113+
59114
$applier->applyCurrentStoreToProduct(
60115
$product
61116
);
62117

63-
$this->assertEquals(3, $product->getStoreId());
118+
$this->assertEquals(null, $product->getStoreId());
64119
}
65120
}
66121
```
67122

123+
### Easy Resource Model Testing
124+
125+
```
126+
TBD
127+
```
128+
68129

69130
## Features
70131

@@ -73,5 +134,5 @@ class YourTestedClassTest extends \PHPUnit\Framework\TestCase
73134
- [x] `GroupInterface` implementation as a simple data object for testing store group related behaviour
74135
- [x] `WebsiteInterface` implementation as a simple data object for testing website related behaviour
75136
- [x] `ScopeConfigurationInterface` implementation for testing configuration dependent functionality
76-
- [ ] `DeploymentConfig` implementation for using in configuration caches, db connections, http cache, etc
137+
- [x] `DeploymentConfig` implementation for using in configuration caches, db connections, http cache, etc
77138
- [ ] `ResourceConnection` implementation for quick testing of database components

0 commit comments

Comments
 (0)