php - Cannot authenticate in custom Laravel test environment (codeception) -
php - Cannot authenticate in custom Laravel test environment (codeception) -
i running vagrant (homestead), php 5.5.12, , laravel 4.2. i'm trying configure codeception utilize testing environment acceptance tests (running on phantomjs). on homestead box have 2 environments: local , test. careful not double on default 'testing' environment, reserved phpunit tests.
my issue while can utilize eloquent instantiate user (and retrieve it), cannot authenticate user of tests. preventing me running test suite, 90% of requires user authentication.
i have 2 sites defined in /etc/nginx/sites-available: myapp.app , myapp.test. define fastcgi_param app_env local , test (respectively). have specified app_env in ~/.bashrc, echo $app_env returns 'local'. have created corresponding config/test folder database.php credentials point test database. below default codeception.yml settings, , overridden acceptance.suite.yml settings (which suite running).
i cannot authenticate in test environment. attempts homecoming false or null.
codeception.ymlactor: tester paths: tests: tests log: tests/_output data: tests/_data helpers: tests/_support settings: bootstrap: _bootstrap.php colors: true memory_limit: 1024m modules: config: db: dsn: 'pgsql:host=localhost;dbname=myapp' user: 'myapp' password: 'myapp' dump: tests/_data/dump.sql
acceptance.suite.yml class_name: acceptancetester modules: enabled: - laravel4 - acceptancehelper - webdriver - db config: webdriver: url: 'http://myapp.test' browser: phantomjs window_size: 1024x768 db: dsn: 'pgsql:host=localhost;dbname=myapp_testing' user: 'myapp_testing' password: 'myapp_testing' dump: tests/_data/test-dump.sql populate: true cleanup: false
this acceptance helper compiles acceptancetester class , run in before filter on cests requiring authentication. tested output of user::create method here , in fact create user object can access from, example, within login method. doesn't, however, appear in database (viewed navicat). database populated test-dump.sql file, , contains schema without data. i'm not sure why eloquent creation method doesn't write database, best guess db beingness rolled after test suite has run.
acceptancehelper.php<?php namespace codeception\module; // here can define custom actions // public methods declared in helper class available in $i utilize acceptancetester; utilize hash; utilize loginpage; utilize user; class acceptancehelper extends \codeception\module { public function loginuser(acceptancetester $i) { user::create([ 'first_name' => 'test', 'last_name' => 'test', 'password' => hash::make('1234'), 'email' => 'test@test.com' ]); $this->login($i, 'test@test.com', '1234'); } public function login(acceptancetester $i, $email, $password) { $i->amonpage(loginpage::$url); $i->fillfield('email', $email); $i->fillfield('password', $password); $i->click(loginpage::$loginbutton); $i->see('welcome. email address test@test.com'); } public function logoutuser(acceptancetester $i) { $i->amonpage('/logout'); $i->seecurrenturlequals('/login'); $i->see('you have been logged out'); } }
below store action on sessions controller should authenticate users (and works fine in 'local' environment myapp.app http requests). login attempts on myapp.test, auth::attempt method returns false.
sessionscontroller.phppublic function store() { $this->loginform->validate($input = input::only('email', 'password')); if (auth::attempt($input)) { homecoming redirect::intended('profile')->with('flash_message', 'you have been logged in!'); } homecoming redirect::back()->with('flash_message', 'invalid credentials.')->withinput(); }
i dialled downwards right auth::attempt method, calls auth/guard->hasvalidcredentials.
protected function hasvalidcredentials($user, $credentials) { homecoming ! is_null($user) && $this->provider->validatecredentials($user, $credentials); }
this returns null. clarify, returns null after phone call $user->first_name returns true, or user::all()->first()->first_name returns right first name. in other words, can create , retrieve user using eloquent, , afterwards test auth::attempt calls hasvalidcredentials, calls eloquent/builder->where, looks based on email address of login form, , returns null. possible user stored in memory , not in database?
i have manually created user , tried authenticating manually, , login form reloads. have no such issues authenticating in 'local' environment.
the below method called during authentication, , returns query object contains null result.
eloquent/builder.php line: 569call_user_func_array(array($this->query, 'where'), func_get_args());
below usercest.php file. baseacceptancehelper simply calls acceptancetester->login function authenticate default test user. fails.
usercest.phpuse myapp\helpers\testhelpers\baseacceptancehelper; class usercest extends baseacceptancehelper { public function _before(acceptancetester $i) { parent::_before($i); } public function _after() { } public function view_profile(acceptancetester $i) { $i->wantto("view user's profile"); $i->amonpage('users'); navbarcomponent::gotomyprofile($i); $i->seeincurrenturl('profile'); } }
any help appreciated. i'm @ loss why authentication failing.
the solution define configuration settings laravel4 module, in acceptance.suite.yml:
class_name: acceptancetester modules: enabled: - laravel4 - acceptancehelper - webdriver - db config: laravel4: cleanup: false environment: test webdriver: url: 'http://myapp.test' browser: phantomjs window_size: 1024x768 db: dsn: 'pgsql:host=localhost;dbname=myapp_testing' user: 'myapp_testing' password: 'myapp_testing' dump: tests/_data/test-dump.sql populate: true cleanup: false
cleanup: true
prevents rollback of db environment between tests, , environment: test
tells laravel4 utilize right database.
another issue ran into, didn't realize @ time of posting, 'test' environment folder had duplicated cache.php , session.php configuration files default laravel 'testing' environment, assigns both cache , session in-memory array. no session info persisting between pages, making authentication impossible. deleted these 2 files config/test, , good.
php authentication laravel codeception
Comments
Post a Comment