2

I'm currently experiencing a weird issue. I've written a bunch of integration tests, and it is my understanding that I can use the @magentoDbIsolation annotation to isolate my database transactions during a test (documentation).

However, I am currently in a situation where my test passes when @magentoDbIsolation is disabled, but it fails when @magentoDbIsolation is enabled. I'm asserting the actual data in the database to check if the product repository has written everything correct. The test:

/**
 * @throws \Exception
 * @magentoDbIsolation enabled
 */
public function testDifferentValuesForDifferentStores()
{
    // Create default product:
    $importValue = $this->createImportValue('abc', ['name' => 'Name', 'price' => 9.95, 'description' => 'Foo']);
    $this->productLoader->importSingle($importValue);

    // Create import values for every store code:
    $stores = $this->storeManager->getStores();
    foreach ($stores as $store) {
        // Only the name differs per store:
        $importValue = $this->createImportValue('abc', [
            'name' => "Name ({$store->getCode()})",
            'price' => 9.95
        ]);
        $importValue->setStoreCode($store->getCode());
        $this->productLoader->importSingle($importValue);
    }

    // Check if the values differ:
    foreach ($stores as $store) {
        $product = $this->productRepository->get('abc', false, $store->getId());
        $this->assertEquals("Name ({$store->getCode()})", $product->getName());
        // Check if there are no stupid duplicates stored:
        // Should be one, since only the name differs:
        // This assertion passes when dbIsolation is disabled, but fails when it's enabled
        $this->assertEquals(1, $this->resource->getConnection()->fetchOne(
            'SELECT COUNT(*) FROM `catalog_product_entity_varchar` WHERE `store_id` = ' . $store->getId() .
            ' AND attribute_id = (SELECT attribute_id FROM eav_attribute WHERE attribute_code = \'name\' AND entity_type_id = 4)'
        ));
    }
}

Now the code that handles the importing does nothing fancy. It extracts the values from my import object, creates a product and saves it in the repository. It does this in an emulated environment, but I've also tested without this but the test still fails.

Does anyone has a clue what might be going on here? Are there specific situations where the @magentoDbIsolation can trigger specific gotcha's that you should be aware of?

1

DB isolation does not work if the code under test changes the database schema, e.g. triggers the product indexer, which creates temporary tables. But the result in this case is, that the isolation does not work because the transaction gets committed instead of rolled back. This might break subsequent tests but not the test where this happened itself.

In your case, the test might pass by accident. To find out, don't just check the count after the import, but also verify your assumptions:

  • check the count before the import
  • check the actual value, not only count it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.