fenos's avatar

[L5] Understanding Package Development

Hi guys, I want to update my Laravel 4 packages to Laravel 5 for all you guys. I would like deliver the updated package with unit and integration tests running on Trevis CI I just need some of your precious suggests.

My understanding

Laravel 5 has removed the workbench feature (no problem), so for create a new package just create a new folder and init composer installing the dependencies.

Dependencies composer.json

As I know when developing packages we have to pull in the "illuminate/support": "~5.0". Taking a look his components they are useful Contracts and Helpers.

Question

My current package use Eloquent in it's features, illuminate/support doesn't provider it. Should i pull in the "laravel/framework": "5.0.*" component to be able to use it?

Testing

How I said my package use Eloquent so it means run queries for you, I want to be able to set up my integration tests with sqlite in memory.

My previous experience in L4 this process was a bit tricky even if pulling the Orchestra\TestCase package was helping us to set environment to testing and other useful stuff.

Question

I feel forced to pull in the "laravel/framework": "5.0.*" in this case, otherwise how can I bootstrap the application for run my integration tests in memory? am I right?

If you have experience with the "new system" of packages development please do light on this questions. Thank you!

0 likes
12 replies
fenos's avatar

@AtoZ Yes I know that Eloquent is an Alias I think you didn't get my question. :p

RomainLanz's avatar

Maybe I didn't catch what you want to mean with

My current package use Eloquent in it's features, illuminate/support doesn't provider it

I think that you need to extends your class with Eloquent and use relations or something similar.

When you want to Unit Test your class, don't forget that you must not test 3rd party package. Eloquent is already tested. Just test your logic.

fenos's avatar

@AtoZ No problem, My issue is not unit testing a Model, but to do Integration test of my Repositories directly from my package. If you take a look the repository "illuminate/support": "~5.0" it doesn't provide you full framework classes, but just useful contracts and Helpers.

Thanks anyway :)

fenos's avatar

none can help? I'm struggling to bootstrap the framework to run my DB tests any of you had experience in this?

JanHenkG's avatar

Why don't you just pull in the Laravel framework as a development only dependency?

fenos's avatar

@JanHenkG Yes, I pulled in "laravel/framework": "5.0.*" as dependency, but I don't understand how to bootstrap the framework for run the tests. Because i'm developing a package I don't have the kernels of console and http.

@JarekTkaczyk This the following example test:

Notification Repository

use Laracasts\TestDummy\Factory;

class NotificationCategoryRepositoryTest extends TestCaseDB {

    /**
     * @var CategoryRepository
     */
    protected $categoryRepo;

    public function setUp()
    {
        $this->categoryRepo = new CategoryRepository( new NotificationCategory());

        parent::setUp();
    }
    
    /** @test */
    function it_find_a_category_by_id()
    {
        $record = Factory::create('Fenos\Notifynder\Models\NotificationCategory');

        $category = $this->categoryRepo->find($record->id);

        $this->assertEquals(1,$category->id);
    }

TestCaseDb Is the class where I should bootstrap and set up the framework, here is where I'm stuck, because the framework is not bootstrapped correctly I got Exception :

Class 'Artisan' not found in /Users/me/LaravelProjects/Projects/pack/Notifynder/tests/TestCaseDB.php on line 12
use Artisan, DB;

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

    public function setUp()
    {
        parent::setUp();

        // This should only do work for Sqlite DBs in memory.
        Artisan::call('migrate');

        // We'll run all tests through a transaction,
        // and then rollback afterward.
        DB::beginTransaction();
    }

    /**
     * Rollback transactions after each test.
     */
    public function tearDown()
    {
        parent::tearDown();
        DB::rollback();
    }

    // Here where I should create the application
    // the following is just some trial (not working)
    public function createApplication()
    {
        $app =  new \Illuminate\Foundation\Application(
            realpath(__DIR__.'/../')
        );

//        $app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();

        return $app;
    }
}

Also how can i set up the default database to sqlite in memory when running the tests? Hope to fix this, so i can continue with the package.

max23's avatar

I'm stuck at the exact same point right now.

My alternative approach would be to bootstrap Laravel, require my package and then write my tests at Laravel level. But this can't be the right way to go .. Also I want to deliver the tests with my package.

Hope there's a solution for that..

fenos's avatar

@max23 yes exactly, it can't be the solution for it because then you can't delivery the tests into your package as I want too :) Sure we will find some solution. :) I'm still trying to make it work :p

fenos's avatar
fenos
OP
Best Answer
Level 4

@max23 i solved the problem, as for laravel 4, I used Orchestra\TestBench package in it version 3.0.*. It has all the boilerplate to bootstrap the framework and run our integration tests during the package development.

Repo : https://github.com/orchestral/testbench

max23's avatar

@fenos Thanks for your feedback :) I had no time to test it yet, but I will try the orchestral/testbench as well! I'm curious if can get it to run with Behat :)

Please or to participate in this conversation.