It’s time again for a learning coffee break, today on software testing! This time, 5 minutes are just enough for a short overview. Take yourself some more time to read all linked articles.
Magento 2 Integration Tests: @magentoConfigFixture
I did not find a good documentation on how to use the @magentoConfigFixture
annotation for configuration fixtures in Magento 2 integration tests, so here is my summary after inspecting the core code (Magento 2.1.0, Magento\TestFramework\Annotation\ConfigFixture
)
How to use @magentoConfigFixture
Set default value 42 for configuration path x/y/z
:
/** * @magentoConfigFixture default/x/y/z 42 */
Set store specific value 42 for configuration path x/y/z
in store with code store1
/** * @magentoConfigFixture store1_store x/y/z 42 */
Set store specific value 42 for configuration path x/y/z
in current store (i.e. default store)
/** * @magentoConfigFixture current_store x/y/z 42 */
These are all possible formats. The first parameter must end with _store
or be ommitted. And if it is omitted, the path must start with default/
, otherwise it is ignored.
Implications
- You cannot set configuration values on website level
- Do not use “current” as a real store code, otherwise you cannot use config fixtures for that store
EcomDev PHPUnit Tip #14
Tip #14: Registry Fixtures
As already explained in Tip #1, helpers, singletons and registry values can be reset per test. Finding the problematic singletons etc. often was the most difficult part of writing integration tests with EcomDev, so I started to collect them centralized in one fixtures/registry.yaml
file for all tests 1. Better reset one too many than one to little.
The file is structured like this:
Continue reading “EcomDev PHPUnit Tip #14”
Notes:
- See also: YAML Directory Fallback ↩
The week weeks on StackExchange #24-29 / 2016
The weekly format did not last long, but a few posts on Magento StackExchange assembled in the last 5 weeks that might be worth some attention (and a new t-shirt):
Magento 2
- Retrieving a product image URL in a custom block seems to be non-trivial and can be done “wrong” in many ways: Getting full image URL of product in template
- How to define a custom search engine: Magento 2: what is the search_engine.xml? How to declare a new search engine? I plan a separate blog post on this topic soon.
- Interesting find: Setting the area to
adminhtml
does not automatically set theadmin
store: Magento 2 integration test in admin context - And an open question: Right way to implement getExtensionAttributes()
Magento 1
- In How to correctly select the first item from a filtered collection? I realize that you should not use
$collection->getData()
(and there’s the obligatory warning for a typical performance trap) - A short overview over all CMS and email template directives, or a try of it: Why do we have to use “store” for links in CMS like <a href=“{{store url=’home’}}”>home</a>
- A reminder, to prefer
theme.xml
overlocal.xml
: Is it possible to include a parent local.xml?
- What is the unit of the weigh attribute? What Is The Default Magento Weight Unit And How Can Change It
The week on StackExchange #9 / 2016
I’ll try a new weekly format on the blog with a summary of recent questions and answers from StackExchange, all around PHP and Magento. Let’s see how it works out and start right away:
New Answers
- In Creating Integration Tests for Magento 2 Modules I explained how to place your own integration tests outside of
dev/tests/integration
. - In Protect a site from wappalyzer I evaluate, if it’s possible to protect a Magento site from automatic detection and how.
- A quickie on design patterns: Data Mapper – should I use dependency injection?
Open questions
- What happened to translation scopes with the new global
__()
function: How does translation scope work in Magento 2? - I thought I had understood static file generation in Magento 2, but what are the templates doing there: When and how are phtml templates generated in view_preprocessed?
In the next week there is more to come on the topic of Magento 2, since I am just starting to get deeper into it.
EcomDev_PHPUnit Tip #9
Tip #9: Checkout Test
3 Years ago I already wrote an article about how to write a checkout integration test. But the practices used there are not longer up to date and some workarounds have become unnecessary. This post shows what’s necessary to write a test that simulates the Magento checkout, using techniques learned in Tip #1.
- Since there are some singletons involved, make sure to reset their state:
/* * @test * @singleton checkout/session * @singleton customer/session * @singleton checkout/cart */
- You should visit the cart page once to trigger totals collection. Assuming, the customer id is 1 and she has an active quote (from previously added products in the test or from a quote fixture), you start with:
$this->customerSession(1); $this->dispatch('checkout/cart');
- Before each new request, you have to reset the checkout session singleton manually within the test, otherwise the quote is not reloaded and you might even lose it completely:
Mage::unregister('_singleton/checkout/session'); $this->customerSession(1); $this->dispatch('checkout/onepage');
- Sometimes you want to logout a customer with active shopping cart. This needs three steps:
Mage::getSingleton('customer/session')->logout(); Mage::getSingleton('checkout/cart')->unsetData(); $this->guestSession();
Mock Admin Session With EcomDev_PHPUnit In Magento Enterprise
In integration tests with EcomDev_PHPUnit_Test_Case_Controller
there is a useful helper method adminSession()
, to test requests in the Magento backend. With Magento Enterprise you might get this error message if you use it:
Exception: Warning: in_array() expects parameter 2 to be array, null given in /home/vagrant/mwdental/www/app/code/core/Enterprise/AdminGws/Model/Role.php on line 83
As seen on Magento EE 1.14.1.
Without disclosing details about the Enterprise modules, here a solution where the responsible observer gets mocked:
$adminObserverMock = $this->getModelMock( 'enterprise_admingws/observer', array('adminControllerPredispatch')); $adminObserverMock->expects($this->any()) ->method('adminControllerPredispatch') ->will($this->returnSelf()); $this->replaceByMock('singleton', 'enterprise_admingws/observer', $adminObserverMock); $this->adminSession();