error [ModuleException] Doctrine2: Module can't be accessed - codeception

I am having trouble to follow the doc in https://codeception.com/docs/modules/Doctrine2. This the error
[ModuleException] Doctrine2: Module can't be accessed
I want to access $entityManger in the unit test. The doctrine bootstrap.php have a createEntityManager function that returns entityManager.
#codeception.yml
suites:
unit:
path: .
actor: UnitTester
modules:
enabled:
# add more modules here
- Asserts
settings:
bootstrap: ../config/bootstrap.php
shuffle: true
lint: true
paths:
tests: tests
output: tests/_output
support: tests/_support
data: tests
# unit.suite.yml
actor: UnitTester
modules:
enabled:
- Asserts
- Doctrine2:
connection_callback: ['createEntityManager']
cleanup: true # All doctrine queries will be wrapped in a transaction, which will be rolled back at the end of each test
- \Helper\Unit

Your problem is that you have unit suite configured in codeception.yml file, so your unit.suite.yml is ignored.
Remove this section from codeception.yml or remove unit.suite.yml and update codeception.yml
suites:
unit:
path: .
actor: UnitTester
modules:
enabled:
# add more modules here
- Asserts

Related

How to eliminate serverless framework error Template format error

Testing, learning serverless framework. I'm trying to deploy simple/basic state machine with two simple lambda functions.
Serverless definition as follows:
frameworkVersion: '2'
app: state-machine
org: macdrorepo
service: state-machine
plugins:
- serverless-python-requirements
- serverless-iam-roles-per-function
- serverless-step-functions
- serverless-pseudo-parameters
custom:
pythonRequirements:
dockerizePip: non-linux
slim: true
zip: true
provider:
name: aws
runtime: python3.8
region: eu-central-1
stage: ${opt:stage, 'testing'}
timeout: 30
package:
individually: true
exclude:
- node_modules/**
- .git/**
- .venv/**
functions:
processpurchase:
module: state-machine
memorySize: 128
stages:
- testing
- dev
handler: ProcessPurchase.process_purchase
processrefund:
module: state-machine
memorySize: 128
stages:
- testing
- dev
handler: ProcessRefund.process_refund
stepFunctions:
validate: true
stateMachines:
TransactionChoiceMachine:
name: ChoiceMachineTest-${self:provider.stage}
dependsOn: CustomIamRole
definition:
Comment: "Purchase refund choice"
StartAt: ProcessTransaction
States:
ProcessTransaction:
Type: Choice
Choices:
- Variable: "$.TransactionType"
StringEquals: PURCHASE
Next: PurchaseState
- Variable: "$.TransactionType"
StringEquals: REFUND
Next: RefundState
PurchaseState:
Type: Task
Resource:
Fn::GetAtt: [processpurchase, Arn]
End: true
RefundState:
Type: Task
Resource:
Fn::GetAtt: [processrefund, Arn]
End: true
During deploy, sls is saying my state machine definition is ok: State machine "TransactionChoiceMachine" definition is valid
My environment information:
Your Environment Information ---------------------------
Operating System: linux
Node Version: 12.20.0
Framework Version: 2.14.0
Plugin Version: 4.1.2
SDK Version: 2.3.2
Components Version: 3.4.3
Setup SLS_DEBUG=* is not helping me much as I do not know js unfortunately.
After serverless deploy command, I'm getting error:
Error: The CloudFormation template is invalid: Template format error: Unresolved resource dependencies [CustomIamRole] in the Resources block of the template
Looks like you are referencing something called CustomIamRole in your state machine creation but I cannot see it being created anywhere in the yaml file. Either create the role and use it in creation or remove the depends on part.

Codeception, how override url from command line for a specific (or not specific) suite test?

I'm looking for a way to override the base url of my tests from the command line. In the future, I'll tests a lot of websites, so it's very unwieldy if I must add each website in a new environment in the acceptance.suite.yml file.
Currently, my acceptance.suite.yml is:
actor: AcceptanceTester
modules:
enabled:
- WebDriver:
url: http://foo.com
browser: chrome
I see I can use the option 'override' with the run command, but even if I read the codeception document and navigate through help website (like stack overflox..), I can't find a good way for override. Can someone help me?
When I prints all config (via ./vendor/bin/concept), I get:
Array
(
actor => AcceptanceTester
modules => Array
(
enabled => Array
(
0 => Array
(
WebDriver => Array
(
url => http://foo.foo
browser => chrome
)
)
1 => \Helper\Acceptance
)
config => Array
(
)
depends => Array
(
)
)
I tried : ./vendor/bin/codecept run acceptance --steps -o 'modules: enabled: 0: WebDriver: url: http://faa.faa, but the test run ever on http://foo.foo
In this codeception issue post, it seems it's impossible to override a config value when we run a specific suite (my english is not very good , so maybe I misunderstood). So I add an env in my acceptance.suite.yml file :
actor: AcceptanceTester
modules:
enabled:
- WebDriver:
url: http://foo.foo
browser: chrome
env:
generic:
modules:
config:
WebDriver:
url: http://faa.faa
And I tried theses commands :
./vendor/bin/codecept run acceptance --env generic --steps -o 'env: generic: modules: config: WebDriver: url: http://faa.faa
And
./vendor/bin/codecept run acceptance --env generic --steps -o 'WebDriver: url: http://faa.faa
And nothing happened. My test are always on http://foo.foo
EDIT AFTER "LEGION" HELP
When I use this acceptance.suite.yml :
actor: AcceptanceTester
modules:
enabled:
- WebDriver
config:
WebDriver:
url: ''
browser: ''
env:
chrome:
modules:
config:
WebDriver: {}
I get an error :
So when I use this acceptance.suite.yml :
actor: AcceptanceTester
modules:
enabled:
- WebDriver
config:
WebDriver:
url: ''
browser: chrome
env:
chrome:
modules:
config:
WebDriver: {}
I get an another error :
And if I use this acceptance.suite.yml :
actor: AcceptanceTester
modules:
enabled:
- WebDriver
config:
WebDriver:
url: ''
browser: chrome
env:
chrome:
modules:
config:
WebDriver:
url: 'http://foo.foo'
browser: 'chrome'
No error ! buuuUUUT ! I'm not on the good url x)
The url I get is "data:", its strange...
For get the url, I add this simple line in my test file :
$this->comment("I am on the url : " . $this->executeJS("return window.location.href") . "\n");
N.B: You need Codeception 1.8 or above! Before that version is bugged.
If the version is > 1.8 it should works... you may try editing your acceptance file like below and see if something is changed:
actor: AcceptanceTester
modules:
enabled:
- WebDriver
config:
WebDriver:
url: 'http://foo.foo/'
browser: 'firefox'
env:
chrome: #or watherver you want
modules:
config:
WebDriver:
url: 'http://fuu.fuu/'
browser: 'chrome'
run it like below:
php vendor/bin/codecept run acceptance --env chrome
** EDIT **
To pass the url from commandline you need empty WebDriver Configuration:
actor: AcceptanceTester
modules:
enabled:
- WebDriver
config:
WebDriver: {}
env:
chrome: #or watherver you want
modules:
config:
WebDriver:
url:'http://foo.foo'
browser:'firefox'
commandline:
./vendor/bin/codecept run acceptance --env chrome --steps -o 'WebDriver: url: \'http://faa.faa\' browser: \'chrome\''
(I usually use apex for url and browser... but not sure are really neded).

Codeception acceptance tests run before browser starts

When I run Codeception tests, sometimes on acceptance tests a browser starts too late, tests do not wait for it to start, and I get errors for tests that passed before a browser start:
[ConnectionException] Can't connect to Webdriver at http://127.0.0.1:4444/wd/hub. Please make sure that Selenium Server or PhantomJS is running.
#1 Codeception\Subscriber\Module->before
#2 D:\path\Yii\basic\vendor\symfony\event-dispatcher\EventDispatcher.php:212
#3 D:\path\Yii\basic\vendor\symfony\event-dispatcher\EventDispatcher.php:44
One time a browser didn't start at all.
My command:
C:\Windows\System32\cmd.exe /K "cd /D D:\path\Yii\basic && vendor\bin\codecept run"
acceptance.suite.yml:
class_name: AcceptanceTester
extensions:
enabled:
- Codeception\Extension\RunProcess:
- java -jar -Dwebdriver.chrome.driver="D:/Selenium/chromedriver.exe" -Dwebdriver.gecko.driver="D:/Selenium/geckodriver.exe" "D:/Selenium/selenium-server-standalone-3.6.0.jar"
modules:
enabled:
- WebDriver:
url: https://hotel.localhost/
browser: chrome
- Yii2:
part: orm
entryScript: index-test.php
cleanup: false
codeception.yml:
actor: Tester
paths:
tests: tests
log: tests/_output
data: tests/_data
helpers: tests/_support
settings:
bootstrap: _bootstrap.php
memory_limit: 1024M
colors: true
modules:
config:
Yii2:
configFile: 'config/test.php'
cleanup: false
coverage:
enabled: true
whitelist:
include:
- models/*
- controllers/*
Thank you in advance.
Add some sleep to RunProcess configuration as documented here.
To wait for the process to be launched use sleep option. In this case you need configuration to be specified as object:
extensions:
enabled:
- Codeception\Extension\RunProcess:
0: java -jar ~/selenium-server.jar
1: mailcatcher
sleep: 5 # wait 5 seconds for processes to boot

Codeception Content-disposition assert file

I want to assert a xls file. I get this back:
"attachment; filename="testify.xlsx""
How can I do assertions on the file?
I have the tmp Path from the $_SERVER['TMPDIR'] and a clean filename, but there is no file in directory.
There are no built-in methods for this purpose, you will have to write your own helper.
If you are using PhpBrowser or one of framework modules, they have two useful hidden methods:
_getResponseContent returns page(or file) content,
_savePageSource saves it to file.
So your helper method would look like this:
function seeXlsFileIsValid()
{
$fileContent = $this->getModule('PhpBrowser)->_getResponseContent();
$this->assertTrue(..., 'returned xls file is not valid');
}
If you want to assert response headers, copy seeHttpHeader method from REST module.
codeception.yml
actor: Tester
paths:
tests: tests
log: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
settings:
bootstrap: _bootstrap.php
colors: true
memory_limit: 1024M
shuffle: true
extensions:
enabled:
- Codeception\Extension\RunFailed
coverage:
enabled: true
c3_url: 'http://www-dev.testify.com:8080/c3.php'
remote: false
whitelist:
include:
- _php/*
api.suite.yml
class_name: ApiTester
modules:
enabled:
- PhpBrowser:
url: http://www-dev.testify.com:8080
clear_cookies: false
restart: false
curl:
CURLOPT_RETURNTRANSFER: true
- REST:
url: http://www-dev.testify.com:8080
depends: PhpBrowser
part: Json
- Asserts
- Helper\Api
- Db:
dsn: 'sqlite:tests/_output/database.sqlite'
user: ''
password: ''
env:
fast:

Behat 3.1 - multiple contexts, second context class not found

I am having such behat.yml
default:
autoload:
#'': %paths.base%/tests/AppBundle/features/user_registration/bootstrap
#'': %paths.base%/tests/AppBundle/features/user_login/bootstrap
[ %paths.base%/tests/AppBundle/features/user_registration/bootstrap, %paths.base%/tests/AppBundle/features/user_login/bootstrap ]
formatters:
progress: ~
suites:
app_features:
#paths:
#features: %paths.base%/tests/AppBundle/features
paths: [ %paths.base%//tests/AppBundle/features ]
contexts:
#- tests\AppBundle\user_registration\UserRegistrationContext
- UserRegistrationContext
- UserLoginContext
#suites:
# default:
# contexts:
# - FeatureContext:
# session: '#session'
extensions:
Behat\Symfony2Extension: ~
Also having this folder structure:
With UserRegistrationContext class its ok, it does not throw errors. Then I added UserLoginContext and it cannot find.
I am using "behat/behat": "^3.1" in composer.json.
behat.yml file is in same level as 'tests' directory.
UserLoginContext looks like this:
<?php
use Behat\Behat\Tester\Exception\PendingException;
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Behat\Mink\Driver\GoutteDriver;
use Behat\Mink\Session;
use AppBundle\Controller\UserController;
/**
* Defines application features from the specific context.
*/
class UserLoginContext implements Context, SnippetAcceptingContext
{
private $session;
/**
* Initializes context.
*
* Every scenario gets its own context instance.
* You can also pass arbitrary arguments to the
* context constructor through behat.yml.
*/
public function __construct()
{
// Choose a Mink driver. More about it in later chapters.
$driver = new GoutteDriver();
$this->session = new Session($driver);
}
}
Why the UserLoginContext is not found?
Update
I noticed that if I do like this - leave one contexts, then UserLoginContext is found:
default:
autoload:
#'': %paths.base%/tests/AppBundle/features/user_registration/bootstrap
'': %paths.base%/tests/AppBundle/features/user_login/bootstrap
#[ %paths.base%/tests/AppBundle/features/user_registration/bootstrap, %paths.base%/tests/AppBundle/features/user_login/bootstrap ]
formatters:
progress: ~
suites:
app_features:
#paths:
#features: %paths.base%/tests/AppBundle/features
paths: [ %paths.base%//tests/AppBundle/features ]
contexts:
#- tests\AppBundle\user_registration\UserRegistrationContext
#- UserRegistrationContext
- UserLoginContext
#suites:
# default:
# contexts:
# - FeatureContext:
# session: '#session'
extensions:
Behat\Symfony2Extension: ~
Update
Temporary solution I found - to create a new profile for each context. But I guess its not how it should be, but still will show how I have done:
user_login:
autoload:
'': %paths.base%/tests/AppBundle/features/user_login/bootstrap
formatters:
progress: ~
suites:
app_features:
paths: [ %paths.base%//tests/AppBundle/features ]
contexts:
- UserLoginContext
extensions:
Behat\Symfony2Extension: ~
And then run with --profile parameter:
sudo vendor/behat/behat/bin/behat --profile user_login
The small problem is that when I will want to run all tests, I will have to run many commands. Also config repetition. Still waiting for better solution.
Here I noticed small text which helped a bit.
http://docs.behat.org/en/v3.0/guides/6.profiles.html
Using behat.yml to autoload will only allow for PSR-0. You can also
use composer.json to autoload, which will also allow for PSR-4
Shame for behat - they really would need to write more about this, cause new user would not understand at all how to use it together with composer.json.
What I have done:
added namespace for the UserLoginContext class same as folder structure
namespace Tests\AppBundle\features\user_login\bootstrap;
Added this namespace to behat.yml near contexts:
.
# for each context class - new profile.
default:
autoload:
'': %paths.base%/tests/AppBundle/features/user_registration/bootstrap
formatters:
progress: ~
suites:
app_features:
#paths:
#features: %paths.base%/tests/AppBundle/features
paths: [ %paths.base%//tests/AppBundle/features ]
contexts:
- UserRegistrationContext
- Tests\AppBundle\features\user_login\bootstrap\UserLoginContext
#suites:
# default:
# contexts:
# - FeatureContext:
# session: '#session'
extensions:
Behat\Symfony2Extension: ~
Also those who have not in their composer.json, need to add this:
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
Ran
composer dump-autoload -o
I am lucky that I have experience with PSR-4 and could guess the solution and that I had time to think and experiment.