Mindexperiment's avatar

Test with Orchestra class not found

Hi,

I'm setting up an environment to test a new package for a newsletter.

I need to use Orchestra to run tests on the package but when I try to vendor/bin/phpunit vendor/agpretto/newsletter I get a PHP Fatal error: Class 'Agpretto\Newsletter\Tests\Integration\IntegrationTestCase' not found in /var/www/vhosts/xxx.com/vendor/agpretto/newsletter/tests/Integration/NewsletterTest.php on line 7

It's strange because with a normal PHPUnit\TestCase the test runs but obviously doesn't load migrations ect.. ect..

The structure of the folder is similar to the Laravel Cashier package. The config.json is setup:


    "require-dev": {
        "mockery/mockery": "^1.0",
        "orchestra/testbench": "^3.8|^4.0|^5.0",
        "phpunit/phpunit": "^7.5|^8.0"
    },
    "autoload": {
        "psr-4": {
            "Agpretto\Newsletter\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Agpretto\Newsletter\Tests\": "tests/"
        }
    },

also phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="vendor/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Package Test Suite">
            <directory suffix=".php">./tests</directory>
        </testsuite>
    </testsuites>
</phpunit>

inside /tests folder I have a test case:


<?php

namespace Agpretto\Newsletter\Tests;

use Agpretto\Newsletter\NewsletterServiceProvider;
use Orchestra\Testbench\TestCase as OrchestraTestCase;

abstract class TestCase extends OrchestraTestCase
{
    protected function getPackageProviders($app)
    {
        return [NewsletterServiceProvider::class];
    }
}

then inside /tests/Integration I have the IntegrationTestCase:


<?php

namespace Agpretto\Newsletter\Tests\Integration;

use Agpretto\Newsletter\Tests\TestCase;
use Illuminate\Database\Eloquent\Model as Eloquent;

abstract class IntegrationTestCase extends TestCase
{
    protected function setUp(): void
    {
        parent::setUp();

        Eloquent::unguard();

        $this->loadLaravelMigrations();

        $this->artisan('migrate')->run();
    }
}


and finally the test to be performed:


<?php

namespace Agpretto\Newsletter\Tests\Integration;

use Agpretto\Newsletter\Newsletter;

class NewsletterTest extends IntegrationTestCase
{
    public function test_Newsletter_CanBeCreated()
    {
        $newsletter = new Newsletter();

        $newsletter = $newsletter->subscribe('[email protected]');

        $this->assertEquals(1, count(Newsletter::all()));
    }
}

simple.. but It doesn't work.. I try with composer dump-autoload, I installed the Orchestra package on the main laravel installation but still don't work. Where can I found a possible error? How can I setup my environment?

Thank you

0 likes
2 replies
Mindexperiment's avatar
Mindexperiment
OP
Best Answer
Level 2

Ok I found the solution.

I was testing a package from the main application [mainapp]$ vendor/bin/phpunit vendor/agpretto/newsletter but the package has to be tested in isolation so, from within the package folder:

  1. install the test suite dependency [package]$ composer install

  2. then run tests from the package [package]$ vendor/bin/phpunit

that solve the problem.

1 like
bpuig's avatar

I think you need to put in your root folder (laravel, not package) composer.json:

"autoload-dev": {
    "psr-4": {
      "Tests\\": "tests/",
      "Agpretto\\Newsletter\\Tests\\": "vendor/agpretto/newsletter/tests/"
    }
  },

I know this is a year old post, but I leave this here because I was looking for the same issue.

Edit: I forgot to say that you need to add your tests to your main phpunit.xml file and also your variables:

<testsuite name="Package Test Suite">
      <directory suffix=".php">./vendor/agpretto/newsletter/tests/</directory>
</testsuite>

Please or to participate in this conversation.