Deal of The Day! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

Adobe Exam AD0-E722 Topic 4 Question 13 Discussion

Actual exam question for Adobe's AD0-E722 exam
Question #: 13
Topic #: 4
[All AD0-E722 Questions]

An Architect wants to create an Integration Test that does the following:

* Adds a product using a data fixture

* Executes $this->someLogic->execute($product) on the product

* Checks if the result is true.

$this->someLogic has the correct object assigned in the setup() method.

Product creation and the tested logic must be executed in the context of two different store views with IDs of 3 and 4, which have been created and are available for the test.

How should the Architect meet these requirements?

Show Suggested Answer Hide Answer
Suggested Answer: C

To create an integration test that executes different logic in different store views, the Architect needs to do the following steps:

Create one test class that extends \Magento\TestFramework\TestCase\AbstractController or \Magento\TestFramework\TestCase\AbstractBackendController, depending on the type of controller being tested1.

Create one test method that uses the @magentoDataFixture annotation to specify the data fixture file that creates the product2.

Use the \Magento\TestFramework\Store\ExecuteInStoreContext class to execute the fixture and the tested logic in different store views. This class has a method called executeInStoreContext, which takes two parameters: the store ID and a callable function.The callable function will be executed in the context of the given store ID, and then the original store ID will be restored3. For example:

PHPAI-generated code. Review and use carefully.More info on FAQ.

public function testSomeLogic()

{

// Get the product from the fixture

$product = $this->getProduct();

// Get the ExecuteInStoreContext instance from the object manager

$executeInStoreContext = $this->_objectManager->get(\Magento\TestFramework\Store\ExecuteInStoreContext::class);

// Execute the fixture in store view 3

$executeInStoreContext->executeInStoreContext(3, function () use ($product) {

// Do some operations on the product in store view 3

});

// Execute the tested logic in store view 4

$result = $executeInStoreContext->executeInStoreContext(4, function () use ($product) {

// Call the tested logic on the product in store view 4

return $this->someLogic->execute($product);

});

// Assert that the result is true

$this->assertTrue($result);

}


Integration tests | Magento 2 Developer Documentation

Data fixtures | Magento 2 Developer Documentation

Magento\TestFramework\Store\ExecuteInStoreContext | Magento 2 Developer Documentation

Contribute your Thoughts:

Merissa
5 days ago
Ah, the joys of Magento testing. I'm with Karrie on this one - option B seems like the cleanest approach. Keep it simple, two test methods, one for each store view. Who needs all that fancy ExecuteInStoreContext nonsense, am I right?
upvoted 0 times
...
Madelyn
7 days ago
Personally, I'd go with option C. Using the ExecuteInStoreContext class directly gives you more control and flexibility, in case the test requirements change down the line. Plus, it keeps everything nicely encapsulated in a single test method.
upvoted 0 times
...
Heike
8 days ago
Hmm, I'm not sure. Option A with the @magentoExecuteInStoreContext annotations seems like it might be a bit more reusable if we need to test in other store views in the future. But I can see the appeal of keeping it all in one test class.
upvoted 0 times
...
Karrie
9 days ago
I'm leaning towards option B. Creating two test methods, one for each store view, seems like a cleaner approach than having to use the ExecuteInStoreContext class manually. Plus, the annotations make it pretty clear what's happening.
upvoted 0 times
...
Stevie
11 days ago
Oof, Magento testing, that's a whole can of worms. I'd say option C looks the most straightforward - using the ExecuteInStoreContext class to handle the store view switching. But I'm curious to hear what the others think.
upvoted 0 times
...
Moira
13 days ago
Hmm, this seems like a tricky question. I'm not sure I fully understand the context of the Magento test framework and how the store views work. But I think the key is making sure we test the product creation and logic execution in the context of both store views 3 and 4.
upvoted 0 times
...

Save Cancel