fooman / printorderpdf-implementation-m2

5 stars 8 forks source link

Easier way to run integration tests #5

Closed jissereitsma closed 3 years ago

jissereitsma commented 5 years ago

When trying to run the integration tests in this module (as we discussed earlier :)) I wanted to take this the easy route - no copying of files, just a composer install, a modification of the PHPUnit file and done:

<testsuite name="Fooman">
    <directory suffix="Test.php">../../../vendor/fooman/*/tests/integration/testsuite/Fooman/*</directory>

This worked, up to the point of fixtures causing havoc. This issue is already discussed here: https://magento.stackexchange.com/questions/93422/creating-integration-tests-for-magento-2-modules/104897#104897 The solution that Fabian suggests is solid but had some minor issues. I'll just go through the steps I took here (without a PR).

The trick of Fabian was to add @magentoDataFixture loadFixture where loadFixture is not a file but a method name. However, his sample method does not work with a simple __DIR__ because that is pointing towards the TestFramework folder and not the current folder. I just used the ComponentRegistrar to get me where I wanted:

    public static function loadFixture()
    {
        /** @var ComponentRegistrar $componentRegistrar */
        $componentRegistrar = ObjectManager::getInstance()->get(ComponentRegistrar::class);
        $modulePath = $componentRegistrar->getPath('module', 'Fooman_PrintOrderPdf');
        require $modulePath . '/../tests/integration/testsuite/Fooman/PrintOrderPdf/Block/Adminhtml/_files/order.php';
    }

The path to the fixture is a bit insane, but this can maybe improved a bit (tests/integration/testsuite/Fooman/PrintOrderPdf/_files?).

In the file tests/integration/testsuite/Fooman/PrintOrderPdf/Block/Adminhtml/_files/order.php I removed the inclusion of the Magento core fixture (require). We can simply use a DocBlock @magentoDataFixture Magento/Sales/_files/order.php for this in the PrintOrderButtonTest class:

    /**
     * @magentoDataFixture Magento/Sales/_files/order.php
     * @magentoDataFixture loadFixtures
     */

After this, running the test works as expected: Nothing being copied to dev/tests/integration but just a generic PHPUnit file that points to Fooman folders.

jissereitsma commented 5 years ago

As a remark: I've never bumped into this issue myself, because I've been using separate DataProvider classes myself - I always disliked the linear and hacky way of doing things in a fixture file, without much OOP to organize your code. So, I'm just using setUp to include a certain data provider and then call upon its methods within my own test methods to generate whatever I want to generate: The database transaction rollbacks work just fine for this.

fooman commented 5 years ago

Thanks for checking - yes this was indeed the issue I mentioned around paths that I ran into (forgot it only applied to custom fixture files). For 2.3.2 there is now also this.

jissereitsma commented 5 years ago

Ah cool thing - same solution but then in the core!

jissereitsma commented 5 years ago

(close when you feel to)

fooman commented 3 years ago

The ExtDN integration test Github action is now live in this repo https://github.com/fooman/printorderpdf-implementation-m2/runs/1223989056?check_suite_focus=true