# Testing in composer shopware project. How to include shopware unit tests with my tests?

**URL:** <https://forum.shopware.com/t/testing-in-composer-shopware-project-how-to-include-shopware-unit-tests-with-my-tests/61823>\
**Category:** International (English Only)\
**Created:** [17. September 2019 um 17:49 UTC](https://forum.shopware.com/t/testing-in-composer-shopware-project-how-to-include-shopware-unit-tests-with-my-tests/61823 "2019-09-17T17:49:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![komoito](https://avatars.discourse-cdn.com/v4/letter/k/48db29/32.png) [@komoito](https://forum.shopware.com/u/komoito)\
**Post date:** [17. September 2019 um 17:49 UTC](https://forum.shopware.com/t/testing-in-composer-shopware-project-how-to-include-shopware-unit-tests-with-my-tests/61823/1 "2019-09-17T17:49:22Z")

</div>

Hi guys!

I have question about unit tests. How to execute shopware tests in shopware “composer” generated project with my custom plugin tests using one autoload.php file.

I noticed when I created shopware project by composer that we don’t have tests folder inside, it is in vendor folder in shopware -\> shopware -\> tests folder. I want to have those tests executed with my plugin example tests&nbsp;in the same execution “context”. I managed to do some very interesting stuff like creating another bootstrap.php unit tests file for shopware tests, but i managed to&nbsp;execute them seperate. When I tried to execute them together i have problem with autoload.php and missing classes. How do you handle such a things in composer project ?&nbsp;

Here is some folder structure i created for the tests:

 ![](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/2X/d/de7bb2cc2ac4cc0938ef3acbe7653ca3e2a9a539.png)

The code :  
Tests-\> Bootstrap.php  
&nbsp;

```
boot();

        $container = $kernel->getContainer();
        $container->get('plugins')->Core()->ErrorHandler()->registerErrorHandler(E_ALL | E_STRICT);

        /** @var \Shopware\Models\Shop\Repository $repository */
        $repository = $container->get('models')->getRepository('Shopware\Models\Shop\Shop');

        $shop = $repository->getActiveDefault();
        $shop->registerResources();
        //Register for containers
        //self::registerResources();

        $_SERVER['HTTP_HOST'] = $shop->getHost();
    }

    private static function registerResources() {

        /*we can register some resources here for example

        Shopware()->Container()->set('swag_test_example.test_service', new \SwagTestExample\Service\TestExampleService(
            Shopware()->Container()->get('shopware_storefront.list_product_service'),
            Shopware()->Container()->get('shopware_storefront.context_service')
        ));

        */
    }

}

SwagTestKernel::start();

```

phpunit.xml.dist

```
        ./../custom/plugins/SwagTestExample/Tests
    
    
        
    
    
        
            
                ./../custom/plugins/SwagTestExample/Tests

```

ShopwareDefaultTestsBootstrap.php&nbsp; - becuase i use another autload.php i don’t have acces to basic configuration from env file (all code is in vendor etc )&nbsp; so i have to improvise some config reading code. All tests seems to work but those are executed seperately.&nbsp;

```
boot();

        $container = $kernel->getContainer();
        $container->get('plugins')->Core()->ErrorHandler()->registerErrorHandler(E_ALL | E_STRICT);

        /** @var \Shopware\Models\Shop\Repository $repository */
        $repository = $container->get('models')->getRepository('Shopware\Models\Shop\Shop');

        $shop = $repository->getActiveDefault();
        $shop->registerResources();

        $_SERVER['HTTP_HOST'] = $shop->getHost();
    }

    protected function getConfigPath()
    {
        return __DIR__. '/../vendor/shopware/shopware/tests/Functional/config.php';
    }

    protected function initializeConfig() {
        parent::initializeConfig();

        //Setup database connection configuration for phpunit tests.
        $this->config['db'] = [
            'username' => $this->getEnvFromFile('DB_USERNAME'),
            'password' => $this->getEnvFromFile('DB_PASSWORD'),
            'dbname' => $this->getEnvFromFile('DB_DATABASE'),
            'host' => $this->getEnvFromFile('DB_HOST'),
            'port' => $this->getEnvFromFile('DB_PORT'),
            'charset' => 'utf8mb4',
            'adapter' => 'pdo_mysql',
        ];
    }

    private function readEnvFile() {
        $fileContent = file_get_contents( __DIR__.'/../.env');
        return $fileContent;
    }

    private function getEnvFromFile($env) {
        $content = $this->readEnvFile();
        $lines = explode(PHP_EOL , $content);

        $linesCount = count($lines);

        for($i = 0 ; $i getEnvFromLine($line,$env);
                return $envValue;
            }
        }

        return '';
    }

    private function getEnvFromLine($line,$env) {
        $lines = explode('=',$line);
        $linesCount = count($lines);
        $isEnoughLines = $linesCount === 2;
        if($isEnoughLines) {
            return str_replace('"','',$lines[1]); //value
        } else {
            throw new Exception('The value for '.$env.' is not defined');
        }

    }

}

TestKernel::start();

```

And xml file for configuration shopware tests. shopware-phpunit.xml.dist

```
            ./../vendor/shopware/shopware/tests/Unit
            ./../vendor/shopware/shopware/tests/Functional
        
    
    
        
    
    
        
            disable
        
    

    
        
            ./../vendor/shopware/shopware/engine/Shopware/
            
                ./../vendor/shopware/shopware/engine/Shopware/Plugins/Community
                ./../vendor/shopware/shopware/engine/Shopware/Components/Xml

```

It would be nice to have those tests in one configuration file and with one context of testing. I think i am missing something important in my code could you guys please give me a hint or just write what I am doing wrong. Thanks a lot for any input from you! :)&nbsp;

---

<div class="post-metadata">

**Author:** ![komoito](https://avatars.discourse-cdn.com/v4/letter/k/48db29/32.png) [@komoito](https://forum.shopware.com/u/komoito)\
**Post date:** [27. September 2019 um 05:45 UTC](https://forum.shopware.com/t/testing-in-composer-shopware-project-how-to-include-shopware-unit-tests-with-my-tests/61823/2 "2019-09-27T05:45:53Z")

</div>

Hi guys I managed to run my tests with Shopware Tests. How to do it ?&nbsp;

First I was wondering why shopware classses are not included in my autoload. I dig deeper and went to composer.json.&nbsp;

Aand I&nbsp;just used psr-4 in autload section, to generete my autoload files, by composer dump-autload command

```
autoload": {
        "classmap": [
            "app/Application.php",
            "app/AppKernel.php",
            "app/ShopwareVersion.php"
        ],
        "psr-4": {
            "Shopware\\Tests\\": "vendor/shopware/shopware/tests"
        }
    },

```

So now i can just include my shopware tests to test my code and functionalities and execute them together. Here is example of phpunit.xml

&nbsp;

```
            ./../custom/plugins/SwagTestExample/Tests
        
      
            ./../vendor/shopware/shopware/tests/Unit
            ./../vendor/shopware/shopware/tests/Functional

```

&nbsp;
