ExamGecko
Question list
Search
Search

List of questions

Search

Related questions











Question 24 - AD0-E722 discussion

Report
Export

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?

A.
Create two test classes with one test method each. Use the @magentoExecuteinstoreContext 3 and $MagentoExecuteinStoreContext 4 annotations on the class level.
Answers
A.
Create two test classes with one test method each. Use the @magentoExecuteinstoreContext 3 and $MagentoExecuteinStoreContext 4 annotations on the class level.
B.
Create one test class with two test methods. Use the emagentostorecontext 3 annotation in one method and amagentostorecontext 4 in the other one.
Answers
B.
Create one test class with two test methods. Use the emagentostorecontext 3 annotation in one method and amagentostorecontext 4 in the other one.
C.
Create one test class with one test method. Use the \Magento\TestFramework\store\ExecuteinstoreContext class once in the fixture and another time in the test.
Answers
C.
Create one test class with one test method. Use the \Magento\TestFramework\store\ExecuteinstoreContext class once in the fixture and another time in the test.
Suggested answer: C

Explanation:

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

asked 02/10/2024
Quintin van Rooyen
44 questions
User
Your answer:
0 comments
Sorted by

Leave a comment first