Search code examples
laraveltestinglaravel-testingpackage-development

Problem for run test on a Laravel package


I use a transformed Spatie/laravel-package-skeleton with my own ModuleServiceProvider and Module class for my system.

I didn't find suffisant ressource on the web for making my module system so maybe there is a problem of implementation.

Normaly ( I think ), when you devellop a package you want to be abstract like this your package is independant and can be installed on various Stack. But my package need to be directly linked to my app classes, because I have a src folder and a lot of classes directly linked to it.

I use Pest and Orchestra/test-bench for run my test

I've needed to add some dependencies in my packages composer.json and deal with psr-4 autoloading, so my composer.json autoload part looks like this:



"autoload": {
        "psr-4": {
            "App\\": "../../../app/",
            "Src\\": "../../../src/",
            "Src\\core\\": "../../../src/core/",
            "Src\\core\\database\\": "../../../src/core/database/",
            "Src\\core\\database\\factories\\": "../../../src/core/database/factories/",
            "Src\\core\\database\\migrations\\": "../../../src/core/database/migrations/",
            "Src\\core\\action\\": "../../../src/core/action/",
            "Src\\core\\component\\": "../../../src/core/component/",
            "Src\\core\\hook\\": "../../../src/core/hook/",
            "Src\\core\\module\\": "../../../src/core/module/",
            "AlexisVS\\MultipassTestingModule\\": "src/",
            "AlexisVS\\MultipassTestingModule\\Database\\Factories\\": "database/factories/",
            "AlexisVS\\MultipassTestingModule\\Database\\Migrations\\": "database/migrations/",
            "AlexisVS\\MultipassTestingModule\\Tests\\": "tests/"
        }
    },

I find that really strange but it loads them so I'm stayed like this

And now I have this error:

   FAILED  Tests\ArchTest > it will not use debugging functions                  TypeError   
  str_ends_with(): Argument #1 ($haystack) must be of type string, array given

  at vendor/laravel/framework/src/Illuminate/Support/Str.php:301
    297▕             $needles = (array) $needles;
    298▕         }
    299▕
    300▕         foreach ($needles as $needle) {
  ➜ 301▕             if ((string) $needle !== '' && str_ends_with($haystack, $needle)) {     
    302▕                 return true;
    303▕             }
    304▕         }
    305▕

  1   vendor/laravel/framework/src/Illuminate/Support/Str.php:301
  2   vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php:124
  3   vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php:92
  4   vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1820
  5   vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1786
  6   vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1578
  7   vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1497
  8   vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1533
  9   vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1486
  10  vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2335
  11  vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:2347
  12  /var/www/html/src/core/module/AbstractModuleClass.php:67
  13  /var/www/html/src/core/module/ModuleServiceProvider.php:42
  14  vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:36
  15  vendor/laravel/framework/src/Illuminate/Container/Util.php:41
  16  vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:93
  17  vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:35
  18  vendor/laravel/framework/src/Illuminate/Container/Container.php:661
  19  vendor/laravel/framework/src/Illuminate/Foundation/Application.php:1006
  20  vendor/laravel/framework/src/Illuminate/Foundation/Application.php:789
  21  vendor/laravel/framework/src/Illuminate/Foundation/Application.php:898
  22  vendor/laravel/framework/src/Illuminate/Foundation/Application.php:878
  23  vendor/laravel/framework/src/Illuminate/Foundation/Application.php:854
  24  vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:431
  25  vendor/orchestra/testbench-core/src/Concerns/CreatesApplication.php:369
  26  vendor/orchestra/testbench-core/src/Concerns/CreatesApplication.php:230
  27  vendor/orchestra/testbench-core/src/TestCase.php:85
  28  vendor/orchestra/testbench-core/src/Concerns/Testing.php:88
  29  vendor/orchestra/testbench-core/src/TestCase.php:52
  30  tests/TestCase.php:32
  31  vendor/pestphp/pest/src/Concerns/Testable.php:182
  32  vendor/pestphp/pest/src/Kernel.php:86
  33  vendor/pestphp/pest/bin/pest:91
  34  vendor/pestphp/pest/bin/pest:99

But the problem ( I think ) isn't here.

for a better context, i use: Laravel 10 PHP 8.2 Laravel sail

when i try to run test, I'm going to the vendor/alexisvs/multipass-testing-module and I install the dependencies and run composer run test

If someone have any idea ...

I don't even know what I could do to debug more
./vendor/vin/pest -vvv is a quite limited :)


Solution

  • It was effectively my database config in myPackage/tests/TestCase.php

    I've solved this with

    public function getEnvironmentSetUp($app)
    {
        config()->set('database.default', 'testing');
    }