PHPUnit tests in PhpStorm: Cannot open file - php

Unable to run tests for the specific file, cause PhpStorm adds namespace to the final command
Testing started at 14:36 ...
[sftp://inf#127.0.0.1:22]:/usr/bin/php /mnt/d/MP/mp.kz/phpunit.phar --bootstrap /mnt/d/MP/mp.kz/vendor/autoload.php --configuration /mnt/d/MP/mp.kz/phpunit.xml MP\\Tests\\Auction\\AuctionsListPublicSearchServiceTest /mnt/d/MP/mp.kz/tests/Auction/AuctionsListPublicSearchServiceTest.php --teamcity --cache-result-file=/mnt/d/MP/mp.kz/.phpunit.result.cache
PHPUnit 9.0.1 by Sebastian Bergmann and contributors.
Cannot open file "MP\\Tests\\Auction\\AuctionsListPublicSearchServiceTest".
Process finished with exit code 1
PhpStorm adds namespace to the command line.
If I remove namespace from command line then it works OK:
[SANDBOX]inf#NM0-MP:~$ /usr/bin/php /mnt/d/MP/mp.kz/phpunit.phar --bootstrap /mnt/d/MP/mp.kz/vendor/autoload.php --no-configuration /mnt/d/MP/mp.kz/tests/Auction/AuctionsListPublicSearchServiceTest.php
PHPUnit 9.0.1 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 1.48 seconds, Memory: 18.00 MB
OK (1 test, 3 assertions)
[SANDBOX]inf#NM0-MP:~$

The fix is going to be available tomorrow in 2020.1 EAP: https://youtrack.jetbrains.com/issue/WI-50201
https://www.jetbrains.com/phpstorm/nextversion/

Related

Tests pass or fail depending on method of PHP Unit invocation

I have a large number of tests in a PHP Unit test suite.
All tests will pass when running the complete suite, however when run individually, certain tests may pass or fail depending on how I call PHP Unit:
$ php phpunit --configuration phpunit.xml --filter FooIntegrationTest
PHPUnit 5.7.19 by Sebastian Bergmann and contributors.
................ 16 / 16 (100%)
Time: 3.97 seconds, Memory: 109.50MB
OK (16 tests, 36 assertions)
vs.
$ php phpunit --configuration phpunit.xml tests/Integration/FooIntegrationTest.php
PHPUnit 5.7.21 by Sebastian Bergmann and contributors.
..........F.F... 16 / 16 (100%)
Time: 3.73 seconds, Memory: 111.75MB
FAILURES!
Tests: 16, Assertions: 36, Failures: 2.
The failures in question are unexpected results (i.e. the code runs just fine), no PHP errors or Exceptions.
The majority of tests will pass both ways, and there doesn't seem to be anything special about those tests that produce the above results.
Solved.... namespace conflict from an unrelated test class
phpunit ... FooIntegrationTest.php only opens & runs the single class file specfied
whereas phpunit ... --filter FooIntegrationTest will open every file in the suite to look for matching tests; which lead to a globally namespaced function in another file causing false pass.
Lesson learned: don't declare globally namespaced functions in unit test classes!

I got NotFoundHttpException when I write unit test for an API in 2 functions

I'm working on Laravel 5.4 and trying to write the unit test to cover my function.
But I got a crazy bug. Please have look at my code and help me.
Here is my test file
class UserControllerTest extends TestCase
{
use DatabaseTransactions;
protected $isCreateToken = true;
public function testViewAccountDetailWithFieldsOK()
{
$this->get('/v1.0/me');
$this->assertResponseStatus(200);
}
public function testViewAccountDetailOK()
{
$this->get('/v1.0/me');
$this->assertResponseStatus(200);
}
}
When I try to run command: phpunit
I got this error:
PHPUnit 5.7.21 by Sebastian Bergmann and contributors.
.F 2 / 2 (100%)
Time: 710 ms, Memory: 14.00MB
There was 1 failure:
1) Tests\Controller\Api\UserControllerTest::testViewAccountDetailOK
Expected status code 200, got 404.
Failed asserting that 404 matches expected 200.
/Users/johnnguyen/Workspace/Laravel/mobile/vendor/laravel/browser-kit-testing/src/Concerns/MakesHttpRequests.php:744
/Users/johnnguyen/Workspace/Laravel/mobile/tests/Controller/Api/UserControllerTest.php:24
/Users/johnnguyen/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:186
/Users/johnnguyen/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:116
But when I run separately for each function:
John-Nguyen:mobile johnnguyen$ phpunit --filter=testViewAccountDetailOK
PHPUnit 5.7.21 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 591 ms, Memory: 14.00MB
OK (1 test, 2 assertions)
2 functions are the same code.
but I got the issue 404: NotFoundHttpException
This is the first time I got this issue.
Can anyone help me for this bug?
Many thanks.

Laravel phpunit skipping custom test files

I have created a laravel test file using the command
php artisan make:test EventUserRelations
When I run phpunit, it is as if the file does not even exist. I tried to fail the test just to make sure it is being tested. Here is the code in the file:
class EventUserRelations extends TestCase
{
public function testExample()
{
$this->fail();
}
}
And here is the result.
phpunit --debug
PHPUnit 5.4.6 by Sebastian Bergmann and contributors.
Starting test 'ExampleTest::testBasicExample'.
. 1 / 1 (100%)
Time: 119 ms, Memory: 20.75MB
In Laravel test files need to end with ..test.php. So change EventUserRelations.php to EventUserRelationsTest.php and it should work.

Jenkins PHPUnit bootstrap failing

When I run PHPUnit on the command line it works fine:
$ phpunit --bootstrap Tests/autoload.php Tests/Classes/Database/customer/CreativeTest
PHPUnit 4.1.4 by Sebastian Bergmann.
...
Time: 225 ms, Memory: 12.50Mb
OK (3 tests, 34 assertions)
However when I try to run it with Jenkins the bootstrap autoload is apparently failing because it says it can't find the parent class of CreativeTest:
[workspace] $ /bin/sh -xe /Users/Shared/Jenkins/tmp/hudson3966267216183299087.sh
+ /usr/local/bin/phpunit --bootstrap Tests/autoload.php Tests/Classes/Database/customer/CreativeTest
PHP Fatal error: Class 'LPtools\Tests\LPTest' not found in /Users/Shared/Jenkins/Home/jobs/LPtools_Unit_Tests/workspace/Tests/Classes/Database/customer/CreativeTest.php on line 20
Build step 'Execute shell' marked build as failure
Finished: FAILURE
I'm a Jenkins novice so any help here would be awesome!

PHPUnit returns code -1

Why PHPUnit return -1, when all test run OK.
phpunit application/models2/Sima/UserTest.php
PHPUnit 3.7.29 by Sebastian Bergmann.
Configuration read from /home/bamboo/bamboo-home/xml-data/build-dir/SIMA-MAIN-JOB1/tests/phpunit.xml
.
Time: 866 ms, Memory: 19.25Mb
OK (1 test, 2 assertions)

Categories