psmail's avatar

Codeception: acceptance test passes, functional test does not

Hi

This one is difficult to communicate - I can paste code examples here but let's see how we go without them first.

I'm trying (yet another) approach to multi tenancy. The approach I am taking is -

  • Modify the auth filter to create a tenant context in (just like Culttt - but I just use the set and id methods of his repo)
  • Use TenantScopeTrait in my model(s) which is just a global scope and borrow very heavily from the Laravel SoftDeletingTrait - the only difference, of course is that rather than looking for null (or not null, as the case may be) deleted_at columns, I am looking for tenant_id = static::$tenantContext->id()
  • In that TenantScopeTrait, I use public static function boot() to access the $tenantContext (shared) object, which you'll recall is created when the auth filter is run

There is not much more to it. It is really simple and I like this approach. Using Codeception, my acceptance tests pass, my functional tests do not.

As data separation is important here, I really do need to get functional testing up and running - I am keen to peek into the database to make sure I have separated the data properly.

The error I get is

Couldn't click "Create":
ErrorException: Trying to get property of non-object

Scenario Steps:
5. I click "Create"
4. I fill field "password","password"
3. I fill field "email","joe@email.com"
2. I am on page "http://demo.domain.app/login"
1. As a user

... or ...

Can't be on page "http://demo.domain.app/clients":
ErrorException: Trying to get property of non-object

Scenario Steps:
3. I am on page "http://demo.domain.app/clients"
2. I am logged as "{"id":"2","tenant_id":"1","client_id":null,"fname":"Joe","lname":"Bloggs","email":"joe@email.com","created_at":"2014-08-21 15:26:11","updated_at":"2014-08-21 16:28:17","deleted_at":null}"
1. As a user

... depending on which approach I use to login with Codeception. They are the same outcome using two different methods to login.

Do you have any ideas where I might start looking? I can provide code examples if need be.

Thanks for reading and apologies for such a ... well ... generic question.

0 likes
4 replies
psmail's avatar

OK ... I think I might have a reasonable code example.

This does not pass a Codeception functional test

Route::filter('auth', function ()
{
    if (Auth::guest())
    {
        if (Request::ajax())
        {
            return Response::make('Unauthorized', 401);
        } else
        {
            return Redirect::guest('login');
        }
    }

    $tenantContext = App::make('BRZ\MultiTenant\TenantContextInterface');
    $tenantContext->set(Auth::user());
});

but this does

class ClientsController extends \BaseController {

    public function __construct(TenantContextInterface $tenantContext)
    {
        $this->beforeFilter('auth');

        if(Auth::check())
        {
            $tenantContext->set(Auth::user());
            var_dump("TenantContextRepository: set = " . $tenantContext->id());
        }
    }
...

Do you have any thoughts on why this might be?

SP1966's avatar

I was having problems with the can't be on page error and found it was because I was splitting up my routes into separate files and using Require_once to import them. Once I changed to Require they worked fine. Just thought I'd throw that out there.

uxweb's avatar

Can you post more pieces of code?, like the actual test and controller used by the action you try to test, that will be of more help.

ross.edman's avatar

Seems like an obvious question to ask but I was having trouble with this as well.... Did you add the Db module to the functional test suite and build?

Please or to participate in this conversation.