mercredi 25 novembre 2015

Laravel - test custom package that depends on other custom packages - autoloading issue

recently I've splitted my CMS into composer packages, and now I have decided to start writing tests for them. However, I'm stuck at finding the right way to configure these tests in terms of other dependencies. After some research I've decided, that I should write PHPUnit Integration tests in isolation of the rest of the framework.

Given that I have a package Sitemap, which generates sitemap for websites using generators (Menu, Page, Photogallery, i hope you get the picture).

So that means, that package Sitemap depends on package Menu (so I can generate menu links into sitemap). Additionaly, package Menu depends on several libraries (etrepat/baum for example). I've moved this common dependencies into package Core and I'm registering them in CoreServiceProvider.php

Also, I'm using orchestra/testbench for testing the package.

Now, to the actual problem, when I define test dependencies for package like this:

protected function getPackageProviders()
    {
        return [
            CoreServiceProvider::class,
            MenuServiceProvider::class,
            SitemapServiceProvider::class
        ];
    }

CoreServiceProvider.php registers common dependencies like this:

/**
     * Register the service provider.
     *
     * @return void
     */
    public function register ()
    {
        $this->registerCommonPackages();

        //Other service providers, important part is registerCommonPackages
    }

    private function registerCommonPackages ()
    {
        $commonPackagesServiceProviders = [
            //Laracasts/Commander
            'Laracasts\Commander\CommanderServiceProvider',
            //Laracasts/validation
            'Laracasts\Validation\ValidationServiceProvider',
            //Cartalyst/Sentry
            'Cartalyst\Sentry\SentryServiceProvider',
            //Laravel/packer
            'Laravel\Packer\PackerServiceProvider',

            //Anahkiasen/former
            'Former\FormerServiceProvider',

            //laravel/meta
            'Laravel\Meta\MetaServiceProvider',
            //philf/setting
            'Philf\Setting\SettingServiceProvider',
            //fedeisias/laravel-js-routes
            'Fedeisas\LaravelJsRoutes\LaravelJsRoutesServiceProvider',
            //codesleeve/laravel-stapler
            'Codesleeve\LaravelStapler\Providers\L4ServiceProvider',
            //baum/node
            'Baum\BaumServiceProvider',
            //hailwood/database-config-loader
            'Hailwood\DatabaseConfigLoader\DatabaseConfigLoaderServiceProvider'
        ];
        foreach ( $commonPackagesServiceProviders as $commonPackagesServiceProvider ) {
            $this->app->register($commonPackagesServiceProvider);
        }
    }

I get Class 'Laracasts\Commander\CommanderServiceProvider' not found when running phpunit that's because those dependencies in other packages are not autoloaded, when i do require <autoload file in vendor dir in other package> it works. But I have to do this for every package that the tested package depends on. That does not seem like a clean solution. Am I doing something wrong? Or is this the only way?

Unfortunately I have not found anyone with similiar problem here on SO, everybody is testing just his package that doesn't depend on any other custom package.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire