I'm trying to run Behat (first time for me) and it worked.
But i have a configuration problem. I tried to change the paths of features and bootstrap like so:
#behat.yml
default:
paths:
features: app/tests/features
bootstrap: %behat.paths.features%/bootstrap
Now i'm getting an exception:
[Symfony\Component\Config\Definition\Exception\InvalidConfigurationException] Unrecognized options "paths" under "testwork"
What did i do wrong?
Behat 3 is out by now. You configure paths as follows:
#behat.yml
default:
autoload:
'': %paths.base%/tests/features/bootstrap
suites:
default:
paths:
- %paths.base%/tests/features
The path given in the autoload section sets the path where Behat looks for context classes. The paths in the suites section are where the feature definitions (of the default suite in this case) live in.
You're trying to use Behat 3, which is not released yet - use version 2.5 instead.
Related
I'm having autowiring issues within symfony since I have updated my Docker to the latest version.
I am not entirely sure of the causality here but certainly, my issues started appearing only after updating my docker. Since this was the case I obviously tried to revert back to a previous docker version but still had the same issues as on the new build. That's why I am doubtful of the causality.
I'm now on: Docker version 3.5.2 with Docker Engine v20.10.7
docker-engine config is the unchanged config:
{
"registry-mirrors": [],
"insecure-registries": [],
"debug": false,
"experimental": false,
"features": {
"buildkit": true
},
"builder": {
"gc": {
"enabled": true,
"defaultKeepStorage": "20GB"
}
}
}
when I started my first project I had an issue with the composer not reading my lib-folder. This was solved as described below but it might provide some more insight, context and things I have checked/done
At first my mind went to this Windows env variable. Alas, it was set so it was not the problem.
Secondly, my mind went to file ownership or file permissions but also not the problem. Everything in my containers was owned by root and had rwx permissions.
Thirdly I thought it might be because of the new WSL2 backend I had to install. Until the day of the update I had still been running on the Hyper-V backend. Luckily there is a setting in docker where you can switch off the use of WSL-2 and revert to Hyper-V but this was also not the solution to my problem.
My composer.json looked like this:
"autoload": {
"classmap": [
"scripts/composer/ScriptHandler.php",
"lib/",
...
],
"psr-4": {
...
}
},
Everything but (some) files of lib were present in my autoload_classmap.php
After fiddling about for too long I finally tried the following and suddenly my problems on my first project were gone
"autoload": {
"classmap": [
"scripts/composer/ScriptHandler.php",
"lib/*",
...
],
"psr-4": {
...
}
},
I still don't understand why the first one should not work. It has worked until this thursday since the infamous update.
Now on to the second project and I really can't figure out what's going on and I'm at a loss for words. My colleagues can't seem to figure out what's wrong either. This second project is something more recent so not loading a classmap anymore and everything follows PSR-4 standards.
This is the autoload part of the composer.json:
"autoload": {
"psr-4": {
"App\\": "src/",
"Project\\": "scripts/"
}
},
This is my services.yaml (These are just the standard symfony settings AFAIK):
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
public: false # Allows optimizing the container by removing unused services; this also means
# fetching services directly from the container via $container->get() won't work.
# The best practice is to be explicit about your dependencies anyway.
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/*'
exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'
This is my docker PHP container:
php:
container_name: "${DOCKER_PROJECT_NAME}_php"
environment:
COLUMNS: 80
PHP_FPM_GROUP: wodby
PHP_FPM_USER: wodby
PHP_SENDMAIL_PATH: "/usr/sbin/sendmail -t -i -S mailhog:1025"
PHP_DEFAULT_CHARSET: 'utf-8'
PHP_DATE_TIMEZONE: 'UTC'
PHP_UPLOAD_MAX_FILESIZE: '10M'
PHP_POST_MAX_SIZE: '10M'
PHP_DISPLAY_ERRORS: 'On'
PHP_DISPLAY_STARTUP_ERRORS: 'On'
PHP_MAX_EXECUTION_TIME: '30000'
PHP_MAX_INPUT_TIME: '60'
PHP_MAX_INPUT_VARS: '2000'
PHP_ERROR_REPORTING: 'E_ALL'
PHP_LOG_ERRORS: 'On'
PHP_LOG_ERRORS_MAX_LEN: '0'
PHP_MEMORY_LIMIT: '512M'
PHP_SESSION_GC_MAXLIFETIME: '700000'
PHP_REALPATH_CACHE_SIZE: '4096K'
PHP_REALPATH_CACHE_TTL: '3600'
PHP_XHPROF: $PROFILING_ENABLED
env_file:
- .env
image: "wodby/php:7.4-dev-4.16.2"
volumes:
- "./:/var/www/html"
## For php profiler traces
- "files:/mnt/files"
On startup I am getting the following Runtime Exception:
Cannot autowire service "App\Form\Wizard\DaysOffType": argument "$daycareTransformer" of method "__construct()" references class "App\Form\DaycareTransformer" but no such service exists.
This class is not new, it's been in code for 2 years. It's in the right folder and the namespacing is correct. Also the src folder is included in the autoload_psr4.php.
The DaycareTransformer that "can't be found" is located in src/Form under the namespace App\Form as per psr4 namespace convention
<?php
namespace App\Form;
use App\Entity\Daycare;
use App\Service\DaycareService;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
class DaycareTransformer implements DataTransformerInterface
{
private $daycareService;
public function __construct(DaycareService $daycareService)
{
$this->daycareService = $daycareService;
}
...
}
There have not been any recent changes to this code. Not to the DaysOffType which has the DaycareTransformer injected and not to the DaycareTransformer.
I am the only person having these issues as I am the only one who runs Docker on a Windows machine. Production and QA environments are all up and running so there is no structural problem in the code.
It could be because the project running symphony in this case was locate at Windows partition (C: or D: or whatever inside your window) and not Linux distro.
If you are using WSL (most likely), you should always store Linux related project inside your distro (Ubuntu in my case, I store it it ~/...). You should not store Linux related project in /mnt/... when using WSL.
When I said "Linux related" I mean whatever project usually want to run on Linux environment, or using docker
Don't know if this could solve it for others because there could be a wide range of reason for this problem. But this is how I solve mine.
We've got the same problem on a Windows machine with Docker and Symfony 5.x.
We continued to work on this machine, but we changed our configuration.
For example, for your error I would use the following config.
Cannot autowire service "App\Form\Wizard\DaysOffType": argument "$daycareTransformer" of method "__construct()" references class "App\Form\DaycareTransformer" but no such service exists.
config/services.yml or config/services_dev.yml
services:
App\Form\DaycareTransformer: ~
We defined all the required classes from our errors and it solved our problem. But it solved problem only for one URL instead of all. You need to check each your routes or classes that use Symfony's autowire.
Unfortunately, the solution like declaration of resources didn't for us. We had to define all the required classes from our errors.
services:
App\DataTransformer\:
resource: '../src/DataTransformer/'
Don't redefined your classes that have already defined in your config, because it replaces your configuration and may make other errors.
You can do it in config/services.yml and commit it or you can use config/service_dev.yml and don't commit it if you haven't use this config before. In the second way you also can change .gitignore and add there the filename.
# Ignore changes for the file and don't track it.
config/services_dev.yml
WARNING! If you still have problems with it define all the required classes in services.yml instead of two or more config files.
We don't have this problem on Linux and MacOS machines.
I have an application with different modules. For each module, there are separate tests written which are divided in different suites.
For example in tests/Custom/codeception.yml
namespace: Test\Custom
paths:
tests: .
data: _data
support: _support
log: _output
coverage:
enabled: true
remote: false
whitelist: { include: ['../../../../src/*'] }
suites:
Business:
path: Business
...
View:
path: View
....
So every module has its own codeception.yml.
To have one "master config" there is a codeception.yml in the first level of the test folder which is including all the subfolder module yml with
namespace: Test
actor: Tester
include:
- tests/*
...
When running a build caused by a branch update in the repository we usually don't want to run other tests than business because they are slow and fragile.
For this i try to run codecept run Business and expect, that just the Business suites from the included files will be executed.
But this will not work, instead i get the error message
Suite 'Business' could not be found
But when i run one module explicit with Business suite
codecept run -c tests/Custom Business
it works.
My first assumption was, that the files get not proper included to the master yml so i tried the general call
codecept run
and it works! But this unfortunately runs all suites.
So, why the suites cant be seen by the master codeception.yml? How can i run all tests Business suites without pointing explicit to the module path?
Thanks
I write you because I have a problem to run my project Symfony 4 DEV environment. Indeed, I have the impression that this one does not turn correctly because:
when I make a modification it is not visible immediately and I usually have to empty the caches to see them
the profiler and the associated "black bar" do not appear and return a 404 (I have the body tags in my base.htm.twig)
he does not see "immediately" the roads that I created
I installed the profiler via the following command composer require profiler --dev and I am in web server (directly on my NDD and my server).
I also have the variable APP_ENV in dev and it shows me well "dev" when I display it directly in a twig via {{app.environment}}
I hope that my explanations are clear enough and that you will be able to help me. thank you in advance
ps: sorry for my English, but I'm on Google Translate ;)
EDIT : I found the solution with the help of someone on Symfony slack ... I did not install the apache-pack bundle which created a .htaccess file in the public folder and manages the rewrites of url ... that's why my roads were not found.
For my defense, I did not see this bundle anywhere in the documentation
The first you have to install symfony/dotenv (dotenv)
In the .env follow this:
APP_ENV=dev
If you have not install symfony/dotenv
composer require symfony/dotenv
You can read about this here https://symfony.com/doc/current/components/dotenv.html
Also you have to follow config/packages/dev/web_profiler.yaml:
web_profiler:
toolbar: true
intercept_redirects: false
framework:
profiler: { only_exceptions: false }
It's not mandatory added APP_ENV=dev to your .env.
In Symfony 4 you can see in public/index.php this
if (!isset($_SERVER['APP_ENV'])) {
if (!class_exists(Dotenv::class)) {
throw new \RuntimeException('APP_ENV environment variable is not defined. You need to define environment variables for configuration or add "symfony/dotenv" as a Composer dependency to load variables from a .env file.');
}
(new Dotenv())->load(__DIR__.'/../.env');
}
$env = $_SERVER['APP_ENV'] ?? 'dev';
So if you APP_ENV is empty also you environment use is dev, but it's better use APP_ENV in .env
I am trying to setup Behat 3.0. I want to change the path of where my features go.
Currently, my behat.yml config looks like this:
default:
autoload:
'': app/tests/acceptance
Running behat --init wil create the acceptance/FeatureContext.php in the app/tests directory.
However, it will create the features folder in the root of my project. I would instead want this features folder to be placed in the app/tests/acceptance folder.
How can I do this?
Behat 3 has support for suites and profiles.
Only thing you have to do is to add custom paths to the default profile:
default:
autoload:
'': %paths.base%/app/tests/acceptance
suites:
default:
paths: [ %paths.base%/app/tests/acceptance/features ]
Tip 1
Always use the %paths.base% variable to be able to run your Behat tests from a different directory.
Tip 2
Depending on Behat's PSR-0 autoload mechanism can be problematic when you'll have more contexts implemented.
Good practice is to use the composer's PSR-4 autoload mechanism to be able to run namespaced Behat features.
After you have setup Behat as in the example above you need to delete the autoload section in the bahat.yml and add contexts to the default profile:
default:
suites:
default:
paths: [ %paths.base%/app/tests/acceptance/features ]
contexts: [ MyApp\Tests\Acceptance\FeatureContext ]
Add autoloading configuration to composer.json:
{
[...]
"autoload-dev": {
"psr-4": {
"MyApp\\Tests\\Acceptance\\": "app/tests/acceptance"
}
}
[...]
}
And then simply dump the autoloader with composer dump-autoload.
I suffered a lot with it, so here is my (simple) solution, in the hope it will save time to someone oneday. Using behat 3.8.1.
My files layout:
behat.yml
test/acceptance/bootstrap/FeatureContext.php
test/acceptance/features/myAcceptanceTest.feature
My behat.yml file:
default:
autoload: [ '%paths.base%/test/acceptance/bootstrap' ]
suites:
default:
paths: [ '%paths.base%/test/acceptance/features' ]
contexts: [ test\acceptance\bootstrap\FeatureContext ]
Unlike everything I read, including the documentation, I had to add quotes to be able to use the %paths.base% variable.
The autoload parameter tells Behat where to find your context php file(s).
The paths parameter tells Behat where to find your features file(s).
The tricky part is the contexts parameter: it gives the namespace path of your FeatureContext class. You need to make sure that it matches what you have at the top of your FeatureContext.php file:
<?php
namespace test\acceptance\bootstrap;
...
class FeatureContext implements Context
{
...
}
With all these planets properly aligned, it works beautifully. From the project folder:
$ vendor/bin/behat
HTH
I installed behat with mink and selenium2-driver for my Symfony2 project.
Is it possible to use the /app/config/behat.yml instead of the /behat.yml file?
I searched on google but I can't find anything else this command.
php bin/behat --config app/config/behat.yml
But the command isn't working either.
I think there must be a config-path in composer.json.
Yes, you can configure which config file you want to use. Look at this part of the doc.
http://docs.behat.org/guides/7.config.html#paths
What error do you get when running your command?
php bin/behat --config app/config/behat.yml
This error?
[RuntimeException]
Context class not found.
Maybe you have provided a wrong or no `bootstrap` path in your behat.yml:
http://docs.behat.org/guides/7.config.html#paths
If that's the case, I think if might be because you need to specify where to find the features in your behat.yml file.
Now you moved the file to /app/config/behat.yml, the related path from behat.yml to the feature directory has changed, so you should add the following to the file:
default:
paths:
features: ../features/
bootstrap: ../features/bootstrap