Laravels neat testing helpers in workbench?

2019-06-05 10:46发布

Looking at the laravel testing documentation, you get an idea of all the cool helpers I'd love to use in workbench.

Sadly I'm unable to do so.

Illuminate\Foundation\Testing\TestCase seems to be unavailable when running my tests from the package directory and therefore i can't extend it.

<?php namespace Acme\Foo;

    class TestCase extends \Illuminate\Foundation\Testing\TestCase {

    /**
     * Creates the application.
     *
     * @return \Symfony\Component\HttpKernel\HttpKernelInterface
     */
    public function createApplication()
    {
        $unitTesting = true;

        $testEnvironment = 'testing';

        return require __DIR__.'/../../../../bootstrap/start.php';
    }

}

PHP Fatal error:  Class 'Illuminate\Foundation\Testing\TestCase' not found in 
/Applications/MAMP/htdocs/Webseiten/acme/workbench/acme/foo/tests/TestCase.php on line 3

I think thats not the correct way, this should be done anyways...

I have already found orchestral/testbench but I'm not really sure if it is a good idea to use it. It seems to require the whole laravel framework, which doesn't make sense to me? ( A package should extend a laravel install and not add the framework a second time!? )

3条回答
姐就是有狂的资本
2楼-- · 2019-06-05 10:49

I just did like this,

First of all, change your phpunit.xml content on your workbench directory. You will "bootstrap" attribute section, change it from vendor/autoload.php to ../../../bootstrap/autoload.php

Then try running phpunit on your workbench directory.

查看更多
【Aperson】
3楼-- · 2019-06-05 11:02

For me, because of the way workbench is setup, the test cases can only extend from PHPUnit_Framework_TestCase. So I run the test cases from the app folder instead of the package folder.

I choose the second option as testbench heavily depends on the particular Laravel version and seems a bit heavy to set up. What I did is configure the phpunit.xml under the main app to include the package tests folder as a test suite:

<testsuite name="MyPack">
    <directory>./workbench/winponta/mypack/tests/</directory>
</testsuite>

And then run:

$ phpunit --testsuite=MyPack

Using this method, we can still extends from \Illuminate\Foundation\Testing\TestCase.

I found this solution at http://ngo-hung.com/blog/2014/04/16/laravel-unit-testing-in-workbench-note

查看更多
手持菜刀,她持情操
4楼-- · 2019-06-05 11:03

Removing

namespace Acme\Foo;

should fix it. Then you can run tests from the package dir. Also be sure to run composer dump-autoload in the package dir or php artisan dump-autoload from main app dir.

UPDATE I was able to replicate the error in a clean Laravel 4.1 installation and fixed it following this steps without installing extra packages:

1 - Install PHPUnit in the main laravel root composer.json (not the workbench package composer.json file)

"require-dev": {
     "phpunit/phpunit": "3.7.*"
},

2 - Run composer update in main laravel root dir.

3 - In your test classes use fully qualified names (namespace prefixed with )

<?php
class SomeTest extends \Illuminate\Foundation\Testing\TestCase {
   public function createApplication()
   {
       $unitTesting = true;
       $testEnvironment = 'testing';
       return require __DIR__.'/../../../../bootstrap/start.php';
   }
   public function testSomething()
   {
       $this->assertTrue(true);
   }
}

4 - To run workbench package tests, go to package directory and run main phpunit

# in workbench/package/vendor dir run
../../../vendor/bin/phpunit

5 - Output Green flag recognizing laravel app third party packages

marcanuy@bolso-server:~/public_html/pkgtesting/workbench/my/pack$ ../../../vendor/bin/phpunit --debug
PHPUnit 3.7.31 by Sebastian Bergmann.

Configuration read from /home/marcanuy/public_html/pkgtesting/workbench/my/pack/phpunit.xml


Starting test 'SomeTest::testSomething'.
.

Time: 64 ms, Memory: 6.25Mb

OK (1 test, 1 assertion)
查看更多
登录 后发表回答