Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit testing an app that uses Sentinel #46

Closed
levacic opened this issue Nov 24, 2014 · 1 comment
Closed

Unit testing an app that uses Sentinel #46

levacic opened this issue Nov 24, 2014 · 1 comment
Assignees
Labels

Comments

@levacic
Copy link

levacic commented Nov 24, 2014

So I'm using Laravel 4.2 with Sentinel 1.0.5, but I can see the same problematic code in the latest version as well, and it's this line in particular:

$this->garbageCollect();

When I run unit tests without a database (which isn't unreasonable), I randomly get exceptions when Sentinel fails to find the activations and permissions database tables - which is expected, since there is no database in the first place.

Is there any recommended way to do what I want, that isn't "use a database for testing"?

The boot() method gets called when the application gets booted, so I don't know if there's a way around it - perhaps if the garbage collection code is moved to the closure within SentinelServiceProvider::registerSentinel()? Then garbage collection would only kick in when an actual Sentinel instance is requested somewhere - which is not something I'll ever be doing in my app's unit tests - I might only have mocks of the Sentinel class there, but since this package already has comprehensive tests, I don't want to test Sentinel itself in my app's tests.

Something like this, basically:

    protected function registerSentinel()
    {
        $that = $this;

        $this->app['sentinel'] = $this->app->share(function($app) use ($that)
        {
            $that->garbageCollect();

            /* rest of the service provider */
        }
    }

Of course, the call to $this->garbageCollect() within the service provider's boot() method would be removed.

Another solution, which seems to work for me is to have an environment-specific Sentinel configuration file. In other words, I have the file app/config/packages/cartalyst/sentinel/testing/config.php with the following contents:

<?php

return [
    'activations' => [
        'lottery' => [0, 100],
    ],
    'reminders' => [
        'lottery' => [0, 100],
    ],
];

Am I wrong in feeling that this is a little too hacky? Because, basically, it's like saying "in testing, you have a 0% chance of triggering garbage collection", as opposed to "in testing, don't ever try to perform garbage collection". Or maybe this is okay, and I'm just being obtuse.

Thanks in advance for any comments on this!

@james-daddies
Copy link

You can setup a SQLite db for testing. Set this up as your default in database.php under config/testing

'sqlite' => array(
    'driver'   => 'sqlite',
    'database' => ':memory:',
    'prefix'   => '',
),

Then in your setup method for your unit tests:

$this->app['artisan']->call('migrate', ['--package' => 'cartalyst/sentinel']);

Making the SQLite db in memory, you still get your fast test times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants