Unable to find config file when generating test - php

I'm using the absolute path to my yml file... is that not the correct file I should be providing a path for? I've verified that the path I'm using is correct...
$ vendor/bin/codecept generate:cept -config="~/path/to/codeception.yml" acceptance createUser
[Codeception\Exception\Configuration]
Configuration file could not be found
generate:cept [-c|--config[="..."]] suite test

I'm not sure what the problem was, I must've corrupted something. but rebuilding my codeception setup using vendor/bin/codecept bootstrap resolved the issue.

Related

How do I set the working directory for phpunit using the XML file?

I'm looking through the documentation, but I'm not seeing any option to change the working directory used when running tests.
I'm using PhpUnit as it's included in Laravel. I want to be able to run vendor/bin/phpunit from my project's root directory, and have it run using the /public directory as the working directory.
I tried running ../vendor/bin/phpunit from the /public, but since the phpunit.xml file isn't in the public directory and I don't want to specify my config file path every time, that won't work.
Is there something I can add to my phpunit.xml file to tell it to run tests using the /public directory as the "cwd" (current working directory)?
Based on the feedback I received in the comments and the documentation, I determined the following:
It's probably not possible to change the cwd that phpunit uses by default (well, it's possible in PhpStorm, but not the command line without writing some kind of wrapper script)
Code that depends on being run from a specific directory is not a good idea.
What I had was some code in one of my classes like this:
$var = file_get_contents("../some_file.json");
This works fine -- until you try to add unit tests. The web server runs using the /public directory as the cwd, while phpunit will run using the root directory.
Rather than trying to force phpunit to always use a particular cwd (/public), I decided it's probably best to remove relative paths from the code that rely on a consistent cwd. So the line above becomes:
$var = file_get_contents(base_path("some_file.json"));
I didn't want to change production code that was already working just to get some tests in place, but this change seemed insignificant enough. (and it's an improvement anyway)
Well, you'd have to do the actual chdir in PHP, but you can define a bootstrap script in the XML (<phpunit bootstrap="./bootstrap.php">) and have that change the working directory.
Alternatively, you can put a setUpBeforeClass function into your test class that changes the working directory.

Codeception into another directory

I created a custom component loaded by Composer.
Here is the structure of my code when my component is loaded.
MyProject
vendor
myComponent
AFTER that, I created the file myComponentTest.php to run an unit test with Codeception.
MyProject
tests
myComponentTest.php
vendor
myComponent
It works very well with the command :
./vendor/bin/codecept run
Alright. Nothing special about it. The Codeception test is ok ! :)
But I guess the procedure is wrong, the file myComponentTest.php should be in to the vendor/myComponent directory, am I right ?
Because, this unit test is only related to the component. For example, If I decide to remove the component, it won't remove my myComponentTet.php file, so I'll have some error when I'll run my unit tests.
BUT, if I move my MyComponentTest.php into the vendor/myComponent directory, I won't be able to run this test, because the Codeception command only execute tests from the tests directory.
So what should I do please ? I'm confused about that. Thanks.
See how testing is implemented in projects with sub-projects in Yii2 framework
codeception.yml in root project directory
include:
- common
- frontend
- backend
paths:
log: console/runtime/logs
settings:
colors: true
Where common|frontend|backend directory with codeception.yml files
I hope this helps.

Laravel 5.4 phpunit vs dusk tests Env App_Url

PHPunit tests and Artisan Dusk tests both use APP_URL from .env File.
But there is something strange, maybe because of my setup but I do not know, it insane.
I have Xampp, my project is on localhost/forum/
When I have
APP_URL=http://localhost
My unit tests are OK.
e.g.
$response = $this->get('sekcja/sadsadsadsadas');
$response->assertStatus(200);
All good.
Like laravel knows it is http://localhost but tests on http://localhost/forum/ because there is this project and looks on http://localhost/forum/sekcja/sadsadsadsadas
But...
Dusk see this wrong.
Dusk is loading me localhost where is welcome XAMPP's Page.
Not my project page at localhost/forum/
Then I change in ENV to APP_URL=http://localhost/forum/
And works good, dusk test are good...
But then, you know what?
My PHPunit tests aren't good, they doesn't work anymore.
They can't find this $this->get('sekcja/sadsadsadsadas'); anymore.
So my question is
What is happening here? In documentation I read for dusk i have to set this as I have set, but them my PHPunit test are broken. Why? if both phpunit and dusk test are using the same variable why they want different value for it? That's kinda silly.
You can create a seperate .env file for dusk specifically.
As stated in the docs:
When running tests, Dusk will back-up your .env file and rename your
Dusk environment to .env. Once the tests have completed, your .env
file will be restored.
This should help you fix your problem.
https://laravel.com/docs/5.4/dusk/#environment-handling

Can't get Symfony 2 to work with PHPstorm

I've installed the symfony plugin to PHPstorm, but when I'm trying to run the project I get the following error
Fatal error: Class 'Doctrine\Tests\Common\Cache\CacheTest' not found in ...
I've checked the path and CacheTest file is in the correct folder. When I run the symfony project from the command line it works just fine.
Is there someway to fix this problem?
What you try is not correct. You execute the php.exe and that is the normal PHP executable. To run your Unit-Tests you need PHPUnit to run.
Go to Preferences -> Languages & Frameworks -> PHP -> PHPUnit and define the phpunit.phar.
When you've done that you create a new configuration to run PHPUnit and you define the configuration which is under app/phpunit.xml.
I figured it out. First of all you need add phpunit.phar as Stony explained in his answer.
After that all I really needed to do was to rename phpunit.xml.dist to phpunit.xml in app/ folder and then just right click it and choose the Run 'phpunit.xml' option. This way the Tests succeed and it doesn't give any errors.
As for running the symfony project you need to add symfony command line tool support to phpstorm. All about that in this link: https://confluence.jetbrains.com/display/PhpStorm/Symfony2+Command+Line+Tool+Integration+-+Symfony+Development+using+PhpStorm
After that you just run it as a command like you would with normal command line. That's also explained in the link.
Try to choose "Use custom autoloader" option in Preferences -> Languages & Frameworks -> PHP -> PHPUnit and specify the path to vendor/autoload.php file.

How do I run unit tests stored in my application directory as opposed to in the PHP directory?

I've just installed PHPUnit and wrote a quick class which I saved to C:\PHP and it worked fine. If however I move the php file containing the test class to the tests directory of my application, it returns the error Class firstTest could not be found in ..
How do I resolve the problem such that it can see the class in the application test directory?
Thanks for the response - it wasn't the solution I used, but it led me to research that produced an alternative.
What I did was to add my PHP directory (C:\PHP) to the PATH environment variable. This allowed me to call phpunit from the tests directory of my application.
Check your config file and ensure that the correct path to the tests dir is given.

Categories