Codeception Tests take 'default' datasource instead of 'test' in CakePHP 3
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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 |
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 '18 at 15:12
Yes I use github.com/cakephp/codeception - main problem is the week documentation...
– Tobias Gaertner
Nov 23 '18 at 8:33
What does yourtests/Unit.suite.yml
file look like? Specifically, does it enable theCakeCodeceptionFramework
module?
– ndm
Nov 23 '18 at 14:55
add a comment |
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 '18 at 14:39
ndm
44.8k75493
44.8k75493
asked Nov 22 '18 at 14:22
Tobias GaertnerTobias Gaertner
612724
612724
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 '18 at 15:12
Yes I use github.com/cakephp/codeception - main problem is the week documentation...
– Tobias Gaertner
Nov 23 '18 at 8:33
What does yourtests/Unit.suite.yml
file look like? Specifically, does it enable theCakeCodeceptionFramework
module?
– ndm
Nov 23 '18 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 '18 at 15:12
Yes I use github.com/cakephp/codeception - main problem is the week documentation...
– Tobias Gaertner
Nov 23 '18 at 8:33
What does yourtests/Unit.suite.yml
file look like? Specifically, does it enable theCakeCodeceptionFramework
module?
– ndm
Nov 23 '18 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 '18 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 '18 at 15:12
Yes I use github.com/cakephp/codeception - main problem is the week documentation...
– Tobias Gaertner
Nov 23 '18 at 8:33
Yes I use github.com/cakephp/codeception - main problem is the week documentation...
– Tobias Gaertner
Nov 23 '18 at 8:33
What does your
tests/Unit.suite.yml
file look like? Specifically, does it enable the CakeCodeceptionFramework
module?– ndm
Nov 23 '18 at 14:55
What does your
tests/Unit.suite.yml
file look like? Specifically, does it enable the CakeCodeceptionFramework
module?– ndm
Nov 23 '18 at 14:55
add a comment |
1 Answer
1
active
oldest
votes
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.
[UPDATE] Fixture loading fixed. I have to manually inject the FixtureManager and load my fxtures in setUp function.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
[UPDATE] Fixture loading fixed. I have to manually inject the FixtureManager and load my fxtures in setUp function.
add a comment |
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.
[UPDATE] Fixture loading fixed. I have to manually inject the FixtureManager and load my fxtures in setUp function.
add a comment |
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.
[UPDATE] Fixture loading fixed. I have to manually inject the FixtureManager and load my fxtures in setUp function.
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.
[UPDATE] Fixture loading fixed. I have to manually inject the FixtureManager and load my fxtures in setUp function.
edited Dec 19 '18 at 9:18
answered Nov 23 '18 at 8:40
Tobias GaertnerTobias Gaertner
612724
612724
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.
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 '18 at 15:12
Yes I use github.com/cakephp/codeception - main problem is the week documentation...
– Tobias Gaertner
Nov 23 '18 at 8:33
What does your
tests/Unit.suite.yml
file look like? Specifically, does it enable theCakeCodeceptionFramework
module?– ndm
Nov 23 '18 at 14:55