Fixture for a product's group price not working
-
10-12-2020 - |
Вопрос
I'm using EcomDev_PHPUnit on Magento EE 1.14.2.1, and am trying to set up a simple unit test for a configurable and simple product which have group prices associated, but I'm not having any luck getting the fixture to work.
My test:
# File: .../ObserverTest.php
/**
* @loadFixture stores
* @loadFixture
*/
public function testGroupPriceExample()
{
$configurable = Mage::getModel('catalog/product')->load(1);
var_dump($configurable->getData('group_price'));
}
For now, I just want to output the group_price
as happens when I do this through the observer subject class that I want to test.
My fixtures:
# File: fixtures/stores.yaml
scope:
website:
- website_id: 1
code: us_eng
name: United States
default_group_id: 1
group:
- group_id: 1
website_id: 1
name: United States
default_store_id: 1
root_category_id: 2
store:
- store_id: 1
website_id: 1
group_id: 1
code: us_eng
name: United States
The stores fixture works fine, and I have used it in plenty of other unit tests.
The product fixture:
# File: fixtures/testGroupPriceExample.yaml
# Set up a configurable with group pricing and a bunch of simples without
eav:
catalog_product:
- entity_id: 1
sku: 12345/RED/
name: Configurable
attribute_set_id: 4
entity_type_id: 4
type_id: configurable
group_price: test
- entity_id: 2
sku: 12345/RED/S
attribute_set_id: 4
entity_type_id: 4
name: Some simple
type_id: simple
- entity_id: 3
sku: 12345/RED/M
attribute_set_id: 4
entity_type_id: 4
name: Another simple
type_id: simple
tables:
# Set up the simple/configurable relationships
catalog/product_relation:
- parent_id : 1
child_id : 2
- parent_id : 1
child_id : 2
# Set up the customer group
customer/customer_group:
- customer_group_id: 4
customer_group_code: Loyalty Group
tax_class_id: 3
I've tried a bunch of different ways of getting a group price set, e.g. under tables: catalog/product_group_price
(results in fixture load failure), eav: catalog/product_group_price
(same), directly as an attribute on the product entity e.g. group_price: 123.00
and still I always get NULL
from the var_dump
in my unit test.
Has anyone created a group price fixture before and could point me in the direction of how to do it?
FYI the stores fixture is in place so that I can fixture a product's group price to be say "$25.99" for Loyalty Group (group_id 4) and the US website (website_id 1).
Решение
First, remove the group_price
attribute from your product fixture and assign a proper price that is higher than the group price, e.g.
eav:
catalog_product:
- entity_id: 1
[…]
price: 29.99
Second, use the correct table alias for your group price fixture, e.g.
tables:
[…]
catalog/product_attribute_group_price:
- value_id: 1
entity_id: 1
all_groups: 0
customer_group_id: 4
value: 25.99
website_id: 0
Third, in your test method, make sure your "customer" is in a customer group that a group price can be applied to, e.g.
$sessionMock = $this->getModelMock(
'customer/session',
array('init', 'getCustomerGroupId')
);
$sessionMock
->expects($this->any())
->method('getCustomerGroupId')
->willReturn(4)
;
$this->replaceByMock('singleton', 'customer/session', $sessionMock);
And finally, test whatever is important to be tested, e.g.
$configurable = Mage::getModel('catalog/product')->load(1);
$this->assertNotNull($configurable->getGroupPrice());
$this->assertLessThan($configurable->getPrice(), $configurable->getGroupPrice());