Codeception Tests take 'default' datasource instead of 'test' in CakePHP 3
up vote
0
down vote
favorite
I'm able to run UnitTests via Codeception, but when it comes to database and fixtures its confusing...
My codeception.dist.yml
namespace: AppTestSuiteCodeception
paths:
tests: tests
output: tmp/tests
data: tests/Fixture
support: src/TestSuite/Codeception
envs: tests/Envs
settings:
bootstrap: bootstrap.php
colors: true
memory_limit: 1024M
actor_suffix: Tester
extensions:
enabled:
- CodeceptionExtensionRunFailed
modules:
config:
Db:
dsn: 'mysql:host=%DB_HOST_TEST%;dbname=%DB_NAME_TEST%'
user: '%DB_USER_TEST%'
password: '%DB_PASSWORD_TEST%'
dump: tests/_data/dump.sql
cleanup: true # reload dump between tests
populate: true # load dump before all tests
reconnect: true
enabled:
- Db
params:
- env
The dump.sql is imported into the test database on running my tests. Fixtures are not inserted...
Then in my Test I do something like this (shortened):
<?php
namespace AppTestUnitModelBehavior;
use AppModelBehaviorHashableBehavior;
use AppModelEntityUser;
use CakeORMTableRegistry;
use CakeDatasourceConnectionManager;
use CakeTestSuiteTestCase;
/**
* AppModelBehaviorHashableBehavior Test Case
*/
class HashableBehaviorTest extends CodeceptionTestUnit
{
/**
* Test subject
*
* @var AppModelBehaviorHashableBehavior
*/
public $Hashable;
public $fixtures = [
'app.Users',
'app.mandators'
];
/**
* Test subject
*
* @var AppModelTableUsersTable
*/
public $UsersTable;
/**
* setUp method
*
* @return void
*/
public function setUp()
{
parent::setUp();
$config = TableRegistry::getTableLocator()->exists('Users') ? : ['className' => UsersTable::class];
$this->UsersTable = TableRegistry::getTableLocator()->get('Users', $config);
$this->Hashable = new HashableBehavior($this->UsersTable);
}
/**
* tearDown method
*
* @return void
*/
public function tearDown()
{
unset($this->Hashable);
parent::tearDown();
}
/**
* Test beforeSave method
*
* @return void
* @throws Exception
*/
public function testBeforeSave()
{
$user = $this->UsersTable->get(1);
...
}
This gives me the first record from my default database and not from the test database. I have no Idea what is missing here... Glad for any help!
php unit-testing cakephp-3.0 codeception
add a comment |
up vote
0
down vote
favorite
I'm able to run UnitTests via Codeception, but when it comes to database and fixtures its confusing...
My codeception.dist.yml
namespace: AppTestSuiteCodeception
paths:
tests: tests
output: tmp/tests
data: tests/Fixture
support: src/TestSuite/Codeception
envs: tests/Envs
settings:
bootstrap: bootstrap.php
colors: true
memory_limit: 1024M
actor_suffix: Tester
extensions:
enabled:
- CodeceptionExtensionRunFailed
modules:
config:
Db:
dsn: 'mysql:host=%DB_HOST_TEST%;dbname=%DB_NAME_TEST%'
user: '%DB_USER_TEST%'
password: '%DB_PASSWORD_TEST%'
dump: tests/_data/dump.sql
cleanup: true # reload dump between tests
populate: true # load dump before all tests
reconnect: true
enabled:
- Db
params:
- env
The dump.sql is imported into the test database on running my tests. Fixtures are not inserted...
Then in my Test I do something like this (shortened):
<?php
namespace AppTestUnitModelBehavior;
use AppModelBehaviorHashableBehavior;
use AppModelEntityUser;
use CakeORMTableRegistry;
use CakeDatasourceConnectionManager;
use CakeTestSuiteTestCase;
/**
* AppModelBehaviorHashableBehavior Test Case
*/
class HashableBehaviorTest extends CodeceptionTestUnit
{
/**
* Test subject
*
* @var AppModelBehaviorHashableBehavior
*/
public $Hashable;
public $fixtures = [
'app.Users',
'app.mandators'
];
/**
* Test subject
*
* @var AppModelTableUsersTable
*/
public $UsersTable;
/**
* setUp method
*
* @return void
*/
public function setUp()
{
parent::setUp();
$config = TableRegistry::getTableLocator()->exists('Users') ? : ['className' => UsersTable::class];
$this->UsersTable = TableRegistry::getTableLocator()->get('Users', $config);
$this->Hashable = new HashableBehavior($this->UsersTable);
}
/**
* tearDown method
*
* @return void
*/
public function tearDown()
{
unset($this->Hashable);
parent::tearDown();
}
/**
* Test beforeSave method
*
* @return void
* @throws Exception
*/
public function testBeforeSave()
{
$user = $this->UsersTable->get(1);
...
}
This gives me the first record from my default database and not from the test database. I have no Idea what is missing here... Glad for any help!
php unit-testing cakephp-3.0 codeception
1
Sounds like CakePHPs fixture manager isn't running (that's where the default connection is exchanged for the test connection). Are you using github.com/cakephp/codeception? If not, you might want to give it a try, as CakePHP doesn't integrate with Codeception on its own.
– ndm
Nov 22 at 15:12
Yes I use github.com/cakephp/codeception - main problem is the week documentation...
– Tobias Gaertner
Nov 23 at 8:33
What does yourtests/Unit.suite.yml
file look like? Specifically, does it enable theCakeCodeceptionFramework
module?
– ndm
Nov 23 at 14:55
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm able to run UnitTests via Codeception, but when it comes to database and fixtures its confusing...
My codeception.dist.yml
namespace: AppTestSuiteCodeception
paths:
tests: tests
output: tmp/tests
data: tests/Fixture
support: src/TestSuite/Codeception
envs: tests/Envs
settings:
bootstrap: bootstrap.php
colors: true
memory_limit: 1024M
actor_suffix: Tester
extensions:
enabled:
- CodeceptionExtensionRunFailed
modules:
config:
Db:
dsn: 'mysql:host=%DB_HOST_TEST%;dbname=%DB_NAME_TEST%'
user: '%DB_USER_TEST%'
password: '%DB_PASSWORD_TEST%'
dump: tests/_data/dump.sql
cleanup: true # reload dump between tests
populate: true # load dump before all tests
reconnect: true
enabled:
- Db
params:
- env
The dump.sql is imported into the test database on running my tests. Fixtures are not inserted...
Then in my Test I do something like this (shortened):
<?php
namespace AppTestUnitModelBehavior;
use AppModelBehaviorHashableBehavior;
use AppModelEntityUser;
use CakeORMTableRegistry;
use CakeDatasourceConnectionManager;
use CakeTestSuiteTestCase;
/**
* AppModelBehaviorHashableBehavior Test Case
*/
class HashableBehaviorTest extends CodeceptionTestUnit
{
/**
* Test subject
*
* @var AppModelBehaviorHashableBehavior
*/
public $Hashable;
public $fixtures = [
'app.Users',
'app.mandators'
];
/**
* Test subject
*
* @var AppModelTableUsersTable
*/
public $UsersTable;
/**
* setUp method
*
* @return void
*/
public function setUp()
{
parent::setUp();
$config = TableRegistry::getTableLocator()->exists('Users') ? : ['className' => UsersTable::class];
$this->UsersTable = TableRegistry::getTableLocator()->get('Users', $config);
$this->Hashable = new HashableBehavior($this->UsersTable);
}
/**
* tearDown method
*
* @return void
*/
public function tearDown()
{
unset($this->Hashable);
parent::tearDown();
}
/**
* Test beforeSave method
*
* @return void
* @throws Exception
*/
public function testBeforeSave()
{
$user = $this->UsersTable->get(1);
...
}
This gives me the first record from my default database and not from the test database. I have no Idea what is missing here... Glad for any help!
php unit-testing cakephp-3.0 codeception
I'm able to run UnitTests via Codeception, but when it comes to database and fixtures its confusing...
My codeception.dist.yml
namespace: AppTestSuiteCodeception
paths:
tests: tests
output: tmp/tests
data: tests/Fixture
support: src/TestSuite/Codeception
envs: tests/Envs
settings:
bootstrap: bootstrap.php
colors: true
memory_limit: 1024M
actor_suffix: Tester
extensions:
enabled:
- CodeceptionExtensionRunFailed
modules:
config:
Db:
dsn: 'mysql:host=%DB_HOST_TEST%;dbname=%DB_NAME_TEST%'
user: '%DB_USER_TEST%'
password: '%DB_PASSWORD_TEST%'
dump: tests/_data/dump.sql
cleanup: true # reload dump between tests
populate: true # load dump before all tests
reconnect: true
enabled:
- Db
params:
- env
The dump.sql is imported into the test database on running my tests. Fixtures are not inserted...
Then in my Test I do something like this (shortened):
<?php
namespace AppTestUnitModelBehavior;
use AppModelBehaviorHashableBehavior;
use AppModelEntityUser;
use CakeORMTableRegistry;
use CakeDatasourceConnectionManager;
use CakeTestSuiteTestCase;
/**
* AppModelBehaviorHashableBehavior Test Case
*/
class HashableBehaviorTest extends CodeceptionTestUnit
{
/**
* Test subject
*
* @var AppModelBehaviorHashableBehavior
*/
public $Hashable;
public $fixtures = [
'app.Users',
'app.mandators'
];
/**
* Test subject
*
* @var AppModelTableUsersTable
*/
public $UsersTable;
/**
* setUp method
*
* @return void
*/
public function setUp()
{
parent::setUp();
$config = TableRegistry::getTableLocator()->exists('Users') ? : ['className' => UsersTable::class];
$this->UsersTable = TableRegistry::getTableLocator()->get('Users', $config);
$this->Hashable = new HashableBehavior($this->UsersTable);
}
/**
* tearDown method
*
* @return void
*/
public function tearDown()
{
unset($this->Hashable);
parent::tearDown();
}
/**
* Test beforeSave method
*
* @return void
* @throws Exception
*/
public function testBeforeSave()
{
$user = $this->UsersTable->get(1);
...
}
This gives me the first record from my default database and not from the test database. I have no Idea what is missing here... Glad for any help!
php unit-testing cakephp-3.0 codeception
php unit-testing cakephp-3.0 codeception
edited Nov 23 at 14:39
ndm
43k75289
43k75289
asked Nov 22 at 14:22
Tobias Gaertner
590622
590622
1
Sounds like CakePHPs fixture manager isn't running (that's where the default connection is exchanged for the test connection). Are you using github.com/cakephp/codeception? If not, you might want to give it a try, as CakePHP doesn't integrate with Codeception on its own.
– ndm
Nov 22 at 15:12
Yes I use github.com/cakephp/codeception - main problem is the week documentation...
– Tobias Gaertner
Nov 23 at 8:33
What does yourtests/Unit.suite.yml
file look like? Specifically, does it enable theCakeCodeceptionFramework
module?
– ndm
Nov 23 at 14:55
add a comment |
1
Sounds like CakePHPs fixture manager isn't running (that's where the default connection is exchanged for the test connection). Are you using github.com/cakephp/codeception? If not, you might want to give it a try, as CakePHP doesn't integrate with Codeception on its own.
– ndm
Nov 22 at 15:12
Yes I use github.com/cakephp/codeception - main problem is the week documentation...
– Tobias Gaertner
Nov 23 at 8:33
What does yourtests/Unit.suite.yml
file look like? Specifically, does it enable theCakeCodeceptionFramework
module?
– ndm
Nov 23 at 14:55
1
1
Sounds like CakePHPs fixture manager isn't running (that's where the default connection is exchanged for the test connection). Are you using github.com/cakephp/codeception? If not, you might want to give it a try, as CakePHP doesn't integrate with Codeception on its own.
– ndm
Nov 22 at 15:12
Sounds like CakePHPs fixture manager isn't running (that's where the default connection is exchanged for the test connection). Are you using github.com/cakephp/codeception? If not, you might want to give it a try, as CakePHP doesn't integrate with Codeception on its own.
– ndm
Nov 22 at 15:12
Yes I use github.com/cakephp/codeception - main problem is the week documentation...
– Tobias Gaertner
Nov 23 at 8:33
Yes I use github.com/cakephp/codeception - main problem is the week documentation...
– Tobias Gaertner
Nov 23 at 8:33
What does your
tests/Unit.suite.yml
file look like? Specifically, does it enable the CakeCodeceptionFramework
module?– ndm
Nov 23 at 14:55
What does your
tests/Unit.suite.yml
file look like? Specifically, does it enable the CakeCodeceptionFramework
module?– ndm
Nov 23 at 14:55
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
After checking all the code again I found in tests/bootstrap.php this lines:
if (getenv('db_dsn')) {
putenv('DATABASE_URL=' . getenv('db_dsn'));
putenv('DATABASE_TEST_URL=' . getenv('db_dsn'));
}
Even I could not find the place where the variable DATABASE_TEST_URL is used, adding this to my .env file solved it:
export db_dsn="mysql://db_usr:db_password@db_host/db_name?encoding=utf8&timezone=UTC&cacheMetadata=true"eIdentifiers=false&persistent=false"
Variable DATABASE_TEST_URL is in .env but has no effect on this... I think its that cakePHP and Codeception doesn't magically integrate too well.
Still my fixtures are not loaded in UnitTests (class MyTest extends CodeceptionTestUnit) - in but the question is solved.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
After checking all the code again I found in tests/bootstrap.php this lines:
if (getenv('db_dsn')) {
putenv('DATABASE_URL=' . getenv('db_dsn'));
putenv('DATABASE_TEST_URL=' . getenv('db_dsn'));
}
Even I could not find the place where the variable DATABASE_TEST_URL is used, adding this to my .env file solved it:
export db_dsn="mysql://db_usr:db_password@db_host/db_name?encoding=utf8&timezone=UTC&cacheMetadata=true"eIdentifiers=false&persistent=false"
Variable DATABASE_TEST_URL is in .env but has no effect on this... I think its that cakePHP and Codeception doesn't magically integrate too well.
Still my fixtures are not loaded in UnitTests (class MyTest extends CodeceptionTestUnit) - in but the question is solved.
add a comment |
up vote
0
down vote
After checking all the code again I found in tests/bootstrap.php this lines:
if (getenv('db_dsn')) {
putenv('DATABASE_URL=' . getenv('db_dsn'));
putenv('DATABASE_TEST_URL=' . getenv('db_dsn'));
}
Even I could not find the place where the variable DATABASE_TEST_URL is used, adding this to my .env file solved it:
export db_dsn="mysql://db_usr:db_password@db_host/db_name?encoding=utf8&timezone=UTC&cacheMetadata=true"eIdentifiers=false&persistent=false"
Variable DATABASE_TEST_URL is in .env but has no effect on this... I think its that cakePHP and Codeception doesn't magically integrate too well.
Still my fixtures are not loaded in UnitTests (class MyTest extends CodeceptionTestUnit) - in but the question is solved.
add a comment |
up vote
0
down vote
up vote
0
down vote
After checking all the code again I found in tests/bootstrap.php this lines:
if (getenv('db_dsn')) {
putenv('DATABASE_URL=' . getenv('db_dsn'));
putenv('DATABASE_TEST_URL=' . getenv('db_dsn'));
}
Even I could not find the place where the variable DATABASE_TEST_URL is used, adding this to my .env file solved it:
export db_dsn="mysql://db_usr:db_password@db_host/db_name?encoding=utf8&timezone=UTC&cacheMetadata=true"eIdentifiers=false&persistent=false"
Variable DATABASE_TEST_URL is in .env but has no effect on this... I think its that cakePHP and Codeception doesn't magically integrate too well.
Still my fixtures are not loaded in UnitTests (class MyTest extends CodeceptionTestUnit) - in but the question is solved.
After checking all the code again I found in tests/bootstrap.php this lines:
if (getenv('db_dsn')) {
putenv('DATABASE_URL=' . getenv('db_dsn'));
putenv('DATABASE_TEST_URL=' . getenv('db_dsn'));
}
Even I could not find the place where the variable DATABASE_TEST_URL is used, adding this to my .env file solved it:
export db_dsn="mysql://db_usr:db_password@db_host/db_name?encoding=utf8&timezone=UTC&cacheMetadata=true"eIdentifiers=false&persistent=false"
Variable DATABASE_TEST_URL is in .env but has no effect on this... I think its that cakePHP and Codeception doesn't magically integrate too well.
Still my fixtures are not loaded in UnitTests (class MyTest extends CodeceptionTestUnit) - in but the question is solved.
answered Nov 23 at 8:40
Tobias Gaertner
590622
590622
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433003%2fcodeception-tests-take-default-datasource-instead-of-test-in-cakephp-3%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Sounds like CakePHPs fixture manager isn't running (that's where the default connection is exchanged for the test connection). Are you using github.com/cakephp/codeception? If not, you might want to give it a try, as CakePHP doesn't integrate with Codeception on its own.
– ndm
Nov 22 at 15:12
Yes I use github.com/cakephp/codeception - main problem is the week documentation...
– Tobias Gaertner
Nov 23 at 8:33
What does your
tests/Unit.suite.yml
file look like? Specifically, does it enable theCakeCodeceptionFramework
module?– ndm
Nov 23 at 14:55