9
votes

So, I'm writing a basic Laravel package and I seem to have stumbled upon yet another problem, this time with testing.

The package in development is currently in a packages folder in the root of the project. I have modified the composer.json file of the package to include the dependencies I need

"require-dev": {
    "phpunit/phpunit": "~4.0",
    "laravel/laravel": "dev-develop"
}

However , whenever I try running phpunit tests in the package folder (which contains a folder named tests along with a sample test), I get the following error:

PHP Fatal error: Class 'Illuminate\Foundation\Testing\TestCase' not found in /workspace/laravel/packages/sample/http-request/tests/HttpRequestTest.php on line 8

The test file is just the auto-generated stub:

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class HttpRequestTest extends Illuminate\Foundation\Testing\TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testExample()
    {
        $this->assertTrue(true);
    }
}

Any idea why this isn't working? The app tests run without a hitch, but the app itself doesn't have dependencies other than what's in the box.

SOLUTION

Managed to make it work independently by extending the PHPUnit_Framework_TestCase:

class HttpRequestTest extends PHPUnit_Framework_TestCase 

However , running it like:

vendor/bin/phpunit packages/yourname/package-name/

Works as well, so I picked it as an answer.

4
Thanks for wanting to add a solution here. In future, please add that as an answer below the question - we like a clean separation between Q&A here. Cheers! - halfer

4 Answers

8
votes

This works for me:

class HttpRequestTest extends TestCase

And running test with:

vendor/bin/phpunit packages/yourname/package-name/
1
votes

(Posted on behalf of the OP as an answer).

Managed to make it work independently by extending the PHPUnit_Framework_TestCase:

class HttpRequestTest extends PHPUnit_Framework_TestCase 

However , running it like:

vendor/bin/phpunit packages/yourname/package-name/

Works as well, so I picked it as an answer.

0
votes

For Windows environments you need to use backslashes!

vendor\bin\phpunit packages\yourname\package-name

0
votes
  • Create a directory named tests in your root of the package and write various test classes inside it. You can have all functioning of your package inside various methods in a single class or you can use multiple classes.

  • Move to your project's root and run following commands,

    vendor/bin/phpunit packages/YOUR_DIRECTORY_NAME/PACKAGE_NAME

  • Wait a little and you will get the response for your test cases. No. of positive and negative tests will be shown.