2

I am developing a package link here for Laravel 4.2. I want to write unit tests and database tests. As far I read I have to create an instance of Laravel app in order to use all the features in testing.

I did a modification on composer.json and added the Laravel package like this

 "require-dev": {
        "phpunit/phpunit" : "4.*",
        "laravel/laravel": "4.2.*"
    },

After composer update I created a TestCase class like the one from Laravel

public function createApplication()
    {
        $unitTesting = true;
        $testEnvironment = 'testing';
        return require __DIR__.'/../vendor/laravel/laravel/bootstrap/start.php';
    }

The issue is when doing a require in start.php I have an error with creating the framework with path ./vjroby/laravel-nonce/vendor/laravel/laravel/vendor/laravel/framework/src

because there is not such file, the file is in ./vjroby/laravel-nonce/vendor/laravel/framework/src

Robert Gabriel
  • 1,151
  • 12
  • 24
  • I think I will try this approach [here](https://github.com/bllim/laravel4-datatables-package/tree/master/tests) . Should this be best practice? – Robert Gabriel Jan 28 '15 at 05:53

3 Answers3

1

I managed to integrate database testing with sqlite in memory. I'm posting the answer so maybe somebody will need it in the future.

  1. I created a bootstrap directory inside the tests directory
  2. I modified the phpunit.xml to load the autoload from the boostrap directory: bootstrap="tests/bootstrap/autoload.php"
  3. Inside the boostrap directory I have 3 files Git Link , with the help of these files I was able to create a Laravel application to test with
  4. Above is my Database Test, I had to crate the migration because I couldn't make the app run the miration, I tried Artisan::call() without any success.

     <?php
        use Mockery as m;
    
        class NonceTest extends Illuminate\Foundation\Testing\TestCase{
    
        public function tearDown()
        {
            m::close();
        }
        public function testWithDatabase(){
        $nonce = new \Vjroby\LaravelNonce\Nonce();
    
        $nonceId = uniqid();
    
        $nonce->setNonce($nonceId, '');
    
        $nonce = DB::table('nonce')->where('id', $nonceId)->get();
    
        $this->assertTrue(count($nonce) == 1);
    
        $this->assertEquals($nonceId, $nonce[0]->id);
    }
    
    /**
     * Creates the application.
     *
     * Needs to be implemented by subclasses.
     *
     * @return \Symfony\Component\HttpKernel\HttpKernelInterface
     */
    public function createApplication()
    {
        $unitTesting = true;
    
        $testEnvironment = 'testing';
    
        $app = require __DIR__.'/bootstrap/start.php';
    
        $app->register('Vjroby\LaravelNonce\LaravelNonceServiceProvider');
    
        return $app;
    }
    public function setUp()
    {
        parent::setUp();
    
        $this->app['config']->set('database.default', 'testing');
    
        $a = __DIR__.'/../src/migrations';
    
        $this->app['config']->set('database.connections.testing', array(
            'driver'   => 'sqlite',
            'database' => ':memory:',
            'prefix'   => '',
        ));
    
        $this->migrate();
    }
    public function migrate()
    {
        Schema::dropIfExists('nonce');
        Schema::create('nonce', function(\Illuminate\Database\Schema\Blueprint $table){
            $table->string('id',255);
            $table->string('data',255)->nullable();
            $table->nullableTimestamps();
    
            $table->unique(['id', 'data']);
            $table->index(['id', 'data']);
        });
    
    }
    
    }
    
Robert Gabriel
  • 1,151
  • 12
  • 24
0

require base_path('vendor/laravel/laravel/bootstrap/start.php');

Ionut Bajescu
  • 1,243
  • 1
  • 11
  • 9
0

Checkout the Laracasts videos. You will see Unit testing with Codeception, Behat and PHPUnit. You will get an idea of what libraries to use and the workflow and process of developing with these tools.

https://laracasts.com/search?q=testing&q-where=lessons

ART GALLERY
  • 540
  • 2
  • 8
  • I did unit and database testing for my projects in Laravel, I'm quite aware of the process with Mockery, stubs, using sqllite. But, I don't know how to create an app for testing the package. I have a payed account at Laracsts. I don't think providing a link with all the lessons in testing helps. I don't think you have experience with I'm looking. – Robert Gabriel Jan 28 '15 at 05:24
  • If you watch the videos you will have a better idea of how to structure your app with unit testing your packages. Codeception is a good place to start, it will allow you to insert records into the database and retrieve them with API call and put data into your app with API calls and check they are indeed in the database. I would recommend starting with codeception since that includes phpunit. You wouldn't be asking these questions if you watched the videos. – ART GALLERY Jan 28 '15 at 17:58
  • Look at the tests in this project https://github.com/Codeception/sample-l4-app/blob/master/tests/api/PostsResourceCest.php maybe this will help you. – ART GALLERY Jan 28 '15 at 18:27
  • you also wouldn't have `"laravel/laravel": "4.2.*"` in "require-dev", rather in "require" only have libraries that are needed during development and testing in "require-dev", with this you won't have laravel install in production unless you place in inside "require" – ART GALLERY Jan 28 '15 at 18:29
  • Because is a Laravel package I only need Laravel framework when testing, so as explained [here](https://getcomposer.org/doc/04-schema.md#require-dev) in require-dev there packages that are needed for developing and testing. Although thanks for your tips. – Robert Gabriel Jan 29 '15 at 20:07