Doctrine annotations are not loaded during execution of Behat context - symfony

I use Symfony 2.8 and Behat 3.3. I have standard FeatureContext class in project_root/features/bootstrap directory. Before scenario execution I want purge DB like that:
/**
* #BeforeScenario
*/
public function beforeScenario()
{
// use Doctrine\Common\DataFixtures\Purger\ORMPurger
$purger = new ORMPurger($this->em);
$purger->purge();
}
But when I execute test I receive error:
The annotation "#Doctrine\ORM\Mapping\Entity" in class XXX does not
exist, or could not be auto-loaded.
My behat.yml is:
default:
suites:
default:
contexts:
- FeatureContext:
em: '#doctrine.orm.entity_manager'
extensions:
Behat\Symfony2Extension:
kernel:
bootstrap: "vendor/autoload.php"
env: "test"
debug: "true"
composer.json autoload section:
"autoload": {
"psr-4": {
"": "src/"
},
"classmap": [
"app/AppKernel.php",
"app/AppCache.php"
]
}

If remove line bootstrap: "vendor/autoload.php" from behat.yml, everything will work as needed.

Related

Mink / behat javascript error not found element for simple scenario

can you help me please?
I have a problem for simple scenario gerkins that don't find fields with error
Form field with id|name|label|value|placeholder "searchform-search" not found. (Behat\Mink\Exception\ElementNotFoundException)
but selecium launches chrome correctly.
I use mink/behat symfony, selecium, chrome-driver.
For example my scenario i use https://hotexamples.com/.
behat.yml
default:
autoload:
- "%paths.base%/tests/api/bootstrap"
extensions:
Behat\Symfony2Extension:
kernel:
bootstrap: tests/api/bootstrap/bootstrap.php
class: App\Kernel
Behat\MinkExtension:
base_url: https://hotexamples.com
goutte: ~
selenium2: ~
browser_name: chrome
suites:
default:
contexts:
- Context\FeatureContext:
kernel: '#kernel'
paths: [ "%paths.base%/tests/api", "%paths.base%/tests/behat"]
FeatureContext:
<?php
declare(strict_types=1);
namespace Context;
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\MinkExtension\Context\MinkContext;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;
/**
* This context class contains the definitions of the steps used by the demo
* feature file. Learn how to get started with Behat and BDD on Behat's website.
*
* #see http://behat.org/en/latest/quick_start.html
*/
class FeatureContext extends MinkContext implements Context, SnippetAcceptingContext
{
/**
* #var KernelInterface
*/
private $kernel;
/**
* #var Response|null
*/
private $response;
public function __construct(KernelInterface $kernel)
{
$this->kernel = $kernel;
}
/**
* #Given I wait for the page to be loaded
*/
public function iWaitForThePageToBeLoaded()
{
$this->getSession()->wait(5000, "document.readyState === 'complete'");
}
/**
* #Given I wait :time seconds
*/
public function iWaitSeconds(int $time)
{
sleep($time);
}
}
Scenario:
Feature: TestBehat
#javascript #api
Scenario: Search
Given I am on the homepage
When I fill in "searchform-search" with "behat"
composer.json
"require-dev": {
"behat/behat": "^3.5",
"behat/mink": "~1.7#dev",
"behat/mink-browserkit-driver": "^1.3",
"behat/mink-extension": "^2.3",
"behat/mink-goutte-driver": "^1.2",
"behat/mink-selenium2-driver": "^1.3",
"behat/symfony2-extension": "^2.1",
"behatch/contexts": "^3.2",
"doctrine/instantiator": "^1.3",
"hautelook/alice-bundle": "^2.5",
"nikic/php-parser": "^4.3",
"phpstan/phpstan": "^0.12.3",
"phpunit/phpunit": "^8.5",
"symfony/maker-bundle": "^1.11",
"symfony/var-dumper": "4.2.*"
}
And output of console
vendor/bin/behat
Feature: TestBehat
#javascript #api
Scenario: Search # tests/behat/authentication.feature:4
12:09:24.452 INFO [ActiveSessionFactory.apply] - Capabilities are: {
"browser": "firefox",
"browserName": "chrome",
"ignoreZoomSetting": false,
"name": "Behat feature suite",
"tags": [
"debian",
"PHP 7.3.12-1+0~20191128.49+debian9~1.gbp24559b"
]
}
12:09:24.453 INFO [ActiveSessionFactory.lambda$apply$11] - Matched factory org.openqa.selenium.grid.session.remote.ServicedSession$Factory (provider: org.openqa.selenium.chrome.ChromeDriverService)
Starting ChromeDriver 79.0.3945.36 (3582db32b33893869b8c1339e8f4d9ed1816f143-refs/branch-heads/3945#{#614}) on port 31246
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
12:09:24.796 INFO [ProtocolHandshake.createSession] - Detected dialect: W3C
12:09:24.797 INFO [RemoteSession$Factory.lambda$performHandshake$0] - Started new session db80a68bb2beb001693480d14ffa7b1f (org.openqa.selenium.chrome.ChromeDriverService)
Given I am on the homepage # Context\FeatureContext::iAmOnHomepage()
When I fill in "searchform-search" with "behat" # Context\FeatureContext::fillField()
Form field with id|name|label|value|placeholder "searchform-search" not found. (Behat\Mink\Exception\ElementNotFoundException)
--- Scénarios échoués:
tests/behat/authentication.feature:4
1 scénario (1 échecs)
2 étapes (1 succès, 1 échecs)
0m2.11s (19.49Mb)
12:09:26.573 INFO [ActiveSessions$1.onStop] - Removing session db80a68bb2beb001693480d14ffa7b1f (org.openqa.selenium.chrome.ChromeDriverService)
Thanks
Problem solved by changing the configuration file for chrome options with mink
default:
autoload:
- "%paths.base%/tests/api/bootstrap"
calls:
error_reporting: 16383 # E_ALL & ~E_USER_DREPRECATED
extensions:
Behat\Symfony2Extension:
kernel:
bootstrap: tests/api/bootstrap/bootstrap.php
class: App\Kernel
Behat\MinkExtension:
browser_name: chrome
base_url: 'http://127.0.0.1:8080/'
goutte: ~
selenium2: ~
sessions:
default:
selenium2:
capabilities:
extra_capabilities:
chromeOptions:
args:
- "--disable-gpu"
- "--window-size=1920,1080"
w3c: false
suites:
default:
contexts:
- Context\FeatureContext:
kernel: '#kernel'
paths: [ "%paths.base%/tests/api", "%paths.base%/tests/behat"]

Symfony 4 without top level namespace

I want to migrate a Silex project to Symfony 4. I'm working on the skeleton first so I executed:
composer create-project symfony/skeleton myProject
and then required the bundles with composer require bundle for each one.
One of them needs some configuration to be installed, so I added a file config/packages/custom_bundle.yaml with such configuration.
custom_bundle.yaml needs a custom user provider service which I copied from the Silex project to the same namespace (Security) with the same name (UserProvider.php) so I created a service at config/services.yaml:
security.users_provider:
class: Security\UserProvider
Then I tried to require the bundle again and I get this error:
The autoloader expected class "App\Security\UserProvider" to be defined in file "/vagrant/myProject/vendor/composer/../../src/Security/UserProvider.php".
The file was found but the class was not in it, the class name or namespace probably has a typo in /vagrant/myProject/config/services.yaml (which is loaded in resource "/vagrant/myProject/config/services.yaml").
Of course, I have to tell composer to load classes from non-top-level-namespace so I replaced:
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
in composer.json with:
"autoload": {
"psr-4": {
"": "src/"
}
},
But now I get this error because the Kernel.php namespace is App:
PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "Kernel" from namespace "App".
I have the entire Silex project without top-level-namespace, so adding App or whatever as top-level-namespace would mean renaming the whole project classes.
By other hand, removing the App namespace of Kernel.php would mean using this class as global, which might won't be a good idea.
Any workaround to avoid rewriting all classes namespaces?
PS: I also tried with:
"autoload": {
"psr-4": {
"App\\": "src/",
"": "src/",
}
},
but same happens.
Edit:
The problem was the default definition of services:
App\:
resource: '../src/*'
exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'
As I don't have the App namespace I've had to comment it by now. Later I'll have to add any services to be auto-defined with their namespaces. E.g.
Service\:
resource: '../src/Service/*'
So it works with commenting the default App services definition and adding:
"autoload": {
"psr-4": {
"App\\": "src/",
"Secutiry\\": "src/Secutiry/",
}
},
and any other namespaces to composer.json. Thanks #Cerad for the tips.
Edit2:
Even works with:
"autoload": {
"psr-4": {
"App\\": "src/",
"": "src/"
}
},

behat Selenium2Driver "could not open connection"

i'm am trying tu use Behat + Mink + Selenium2Driver into Symfony2.
i ran Selenium Server.
my behat.yml is:
default:
extensions:
Behat\Symfony2Extension\Extension:
mink_driver: true
Behat\MinkExtension\Extension:
default_session: 'symfony2'
base_url: http://localhost/fhm_mind_solution/app_dev.php/
selenium2:
wd_host: 'http://127.0.0.1:4444/wd/hub'
capabilities: { "browser": "firefox", "version": "23"}
in my FeatureContext.php:
class FeatureContext extends RawMinkContext
implements KernelAwareInterface
{
private $kernel;
private $parameters;
public function __construct(array $parameters)
{
$this->useContext('mink', new MinkContext);
}
/* [...] */
public function iCanAuthenticateAsAnAdmin()
{
$this->getSession();
}
when i lauch Behat, i have always this: "could not open connection". What is missing?
Firstly, you didn't configure selenium driver properly. It should be:
default:
extensions:
Behat\Symfony2Extension\Extension:
mink_driver: true
Behat\MinkExtension\Extension:
default_session: 'symfony2'
base_url: http://localhost/fhm_mind_solution/app_dev.php/
selenium2:
wd_host: 'http://127.0.0.1:4444/wd/hub'
capabilities: { "browser": "firefox", "version": "23"}
Secondly, you don't have to initialize the driver nor the session yourself. It's done for you by the MinkExtension.
Simply make that your context extends the Behat\MinkExtension\Context\RawMinkContext and you'll get access to the $this->getSession() method.
Read more in the official docs: http://extensions.behat.org/mink/
You can see available capabilities in the selenium2 configuration: https://github.com/Behat/MinkExtension/blob/2.0/src/Behat/MinkExtension/services/sessions/selenium2.xml#L10

Adding CoffeeScript support for Grunt Delta?

I was able to edit my Gruntfile to process my CoffeeScript files using the grunt command by adding:
grunt.initConfig({
coffee: {
compile: {
options: {
bare: true,
join: true,
sourceMap: true
},
files: {
'src/coffee.js' : ['src/app/**/*.coffee', 'src/components/**/*.coffee']
}
}
},
...
But I can't seem to figure out how to add CoffeeScript processing to the Grunt Delta command as I don't want to have to manually "grunt" every time I make a change and test. My current delta file. Would love some direction on how to get the delta working with CoffeeScript.
delta: {
/**
* By default, we want the Live Reload to work for all tasks; this is
* overridden in some tasks (like this file) where browser resources are
* unaffected. It runs by default on port 35729.
*/
options: {
livereload: true
},
/**
* When the Gruntfile changes, we just want to lint it. That said, the
* watch will have to be restarted if it should take advantage of any of
* the changes.
*/
gruntfile: {
files: 'Gruntfile.js',
tasks: [ 'jshint:gruntfile' ],
options: {
livereload: false
}
},
/**
* When our source files change, we want to run most of our build tasks
* (excepting uglification).
*/
src: {
files: [
'<%= src.js %>'
],
tasks: [ 'jshint:src', 'karma:unit:run', 'concat:dist', 'ngmin:dist', 'uglify:dist' ]
},
/**
* When assets are changed, copy them. Note that this will *not* copy new
* files, so this is probably not very useful.
*/
assets: {
files: [
'src/assets/**/*'
],
tasks: [ 'copy' ]
},
/**
* When index.html changes, we need to compile just it.
*/
html: {
files: [ '<%= src.html %>' ],
tasks: [ 'index' ]
},
/**
* When our templates change, we only add them to the template cache.
*/
tpls: {
files: [
'<%= src.atpl %>',
'<%= src.ctpl %>'
],
tasks: [ 'html2js', 'concat:dist', 'ngmin:dist', 'uglify:dist' ]
},
/**
* When the CSS files change, we need to compile and minify just them.
*/
sass: {
files: [ 'src/**/*.scss' ],
tasks: 'compassCompile'
},
envs: {
files: [
'environment.json'
],
tasks: ['build']
},
/**
* When a unit test file changes, we only want to linit it and run the
* unit tests. However, since the `app` module requires the compiled
* templates, we must also run the `html2js` task.
*/
unittest: {
files: [
'<%= src.unit %>'
],
tasks: [ 'jshint:test', 'karma:unit:run' ],
options: {
livereload: false
}
}
}
This is what I came up with:
coffeesrc: {
files: [
'src/app/**/*.coffee', 'src/components/**/*.coffee'
],
tasks: [ 'coffee' ]
},
Seems to work.

Testing a symfony 2.1 project using behat and mink

I'm trying to use Behat and Mink to test a symfony 2.1 project.
My FeatureContext.php:
<?php
use Behat\Behat\Context\ClosuredContextInterface,
Behat\Behat\Context\BehatContext,
Behat\Behat\Exception\PendingException,
Behat\Behat\Context\Step;
use Behat\Gherkin\Node\PyStringNode,
Behat\Gherkin\Node\TableNode;
use Behat\MinkExtension\Context\MinkContext;
//
// Require 3rd-party libraries here:
//
// require_once 'PHPUnit/Autoload.php';
// require_once 'PHPUnit/Framework/Assert/Functions.php';
//
/**
* Features context.
*/
class FeatureContext extends Behat\MinkExtension\Context\MinkContext {
/**
* Initializes context.
* Every scenario gets it's own context object.
*
* #param array $parameters context parameters (set them up through behat.yml)
*/
public function __construct(array $parameters)
{
// Initialize your context here
}
//
// Place your definition and hook methods here:
//
// /**
// * #Given /^I have done something with "([^"]*)"$/
// */
// public function iHaveDoneSomethingWith($argument)
// {
// doSomethingWith($argument);
// }
//
}
composer.json
"behat/behat": ">=2.2.2",
"behat/mink": ">=1.3.2",
"behat/symfony2-extension": "*",
"behat/mink-extension": "*",
"behat/mink-browserkit-driver": "*",
"behat/mink-selenium-driver": "*"
app/config/behat.yml
default:
extensions:
Behat\Symfony2Extension\Extension:
mink_driver: true
kernel:
env: test
debug: true
Behat\MinkExtension\Extension:
base_url: 'http://localhost:8080/app_test.php/'
default_session: symfony2
javascript_session: selenium
selenium:
host: 127.0.0.1
port: 4444
When I do ./bin/behat I get:
Feature: Login
In order to login
As a user
I need to be able to validate the username and password
Scenario: Link to login page # features/login.feature:7
PHP Fatal error: Call to a member function getSession() on a non-object in vendor/behat/mink-extension/src/Behat/MinkExtension/Context/RawMinkContext.php on line 81
Fatal error: Call to a member function getSession() on a non-object in vendor/behat/mink-extension/src/Behat/MinkExtension/Context/RawMinkContext.php on line 81
Any idea?
v.
Your behat.yml should not be located in app/config/behat.yml , but in your/project/root/behat.yml
Thanks for #Stuart and #spiritoo answers here.
Firstly, move the behat.yml from /config to project root.
Secondly, content for the behat.yml:
default:
suites:
my_suite:
contexts:
- FeatureContext
extensions:
Behat\Symfony2Extension: ~
Behat\MinkExtension:
base_url: http://en.wikipedia.org
goutte: ~
sessions:
default:
symfony2: ~
Finally, in project root, run:
vendor/bin/behat features/{YOUR TEST FILE HERE}
My composer.json file:
"require-dev": {
"behat/behat": "^3.3",
"behat/mink": "^1.7",
"behat/mink-extension": "^2.2",
"behat/mink-browserkit-driver": "^1.3",
"behat/mink-goutte-driver": "^1.2",
"behat/symfony2-extension": "^2.1",
"behat/mink-selenium2-driver": "^1.3"
}

Resources