Laravel 6 cannot run tests "PHPUnit\Runner\BaseTestRunner::getTest() must be of the type string" - phpunit

I'm trying to write very simple phpunit test
php artisan make:test CarTest
class CarTest extends TestCase
{
/**
* A basic feature test example.
*
* #return void
*/
public function testExample()
{
$response = $this->get('/car');
$response->assertStatus(200);
}
}
Now I run it
phpunit ./tests/Feature/CarTest.php
I get
Fatal error: Uncaught TypeError: Argument 2 passed to PHPUnit\Runner\BaseTestRunner::getTest() must be of the type string, array given, called in C:\Users\yasse\AppData\Roaming\Composer\vendor\phpunit\phpunit\src\TextUI\Command.php on line 125 and defined in C:\xampp\htdocs\lara6\vendor\phpunit\phpunit\src\Runner\BaseTestRunner.php:60
Stack trace:
#0 C:\Users\{username}\AppData\Roaming\Composer\vendor\phpunit\phpunit\src\TextUI\Command.php(125): PHPUnit\Runner\BaseTestRunner->getTest('C:\\xampp\\htdocs...', Array)
#1 C:\Users\{username}\AppData\Roaming\Composer\vendor\phpunit\phpunit\src\TextUI\Command.php(101): PHPUnit\TextUI\Command->run(Array, true)
#2 C:\Users\{username}\AppData\Roaming\Composer\vendor\phpunit\phpunit\phpunit(61): PHPUnit\TextUI\Command::main()
#3 {main}
I installed phpunit globally and installed it locally (v8) using composer and run composer dump-autoload but issue is not resolved. How to fix this and be able to run this test?

Related

expectExceptionMessageRegExp() function is deprecated in phpunit 9 . What's the other alternate function that i can use instead?

Am getting below issue after switching phpunit version from 7.1 to 9.5.28
Failed asserting that exception of type "Error" matches expected exception "Exception".
expectExceptionMessageRegExp() is deprecated.
expectExceptionMessageRegExp() is deprecated in PHPUnit 8 and will be removed in PHPUnit 9. Use expectExceptionMessageMatches() instead.
Example :
function testResponse(){
$this->expectExceptionMessageRegExp("/Unable to emit response/");
}
Replace with :
function testResponse(){
$this->expectExceptionMessageMatches("/Unable to emit response/");
}

The class Mock_* was not found in the chain configured namespaces App\Entity [Symfony 5.3][PHPUnit 8.5]

I am trying to write PHPUnit tests for my Symfony 5.3 project with some Repositories mocked with others real.
$ bin/console -v
Symfony 5.3.10 (env: dev, debug: true)
$ bin/phpunit -V
PHPUnit 8.5.19 by Sebastian Bergmann and contributors.
Doctrine\Persistence\Mapping\MappingException: The class 'Mock_SType_b1b7aee4' was not found in the chain configured namespaces App\Entity
/var/www/cir/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/MappingException.php:23
/var/www/cir/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/Driver/MappingDriverChain.php:91
/var/www/cir/vendor/doctrine/doctrine-bundle/Mapping/MappingDriver.php:45
/var/www/cir/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:156
/var/www/cir/vendor/doctrine/doctrine-bundle/Mapping/ClassMetadataFactory.php:19
/var/www/cir/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php:382
/var/www/cir/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php:85
/var/www/cir/vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php:251
/var/www/cir/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:293
/var/www/cir/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1789
/var/www/cir/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1764
/var/www/cir/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:629
/var/www/cir/src/Component/User/UserManager.php:1059
/var/www/cir/src/Component/User/UserManager.php:383
/var/www/cir/tests/Component/User/Manager/Creation/ATCPTest.php:146
I have created a KernelTestCase where:
$sTypeRepository = $this->createStub(STypeRepository::class);
So I tried to use the actual Repository:
$sTypeRepository = static::getContainer()->get('doctrine')->getRepository(SType::class);
and I get error:
TypeError: Argument 2 passed to App\Component\User\UserManager::__construct() must be an instance of App\Repository\STypeRepository, instance of Doctrine\ORM\EntityRepository given, called in /var/www/cir/tests/Component/User/Manager/Creation/ATCPTest.php on line 122
/var/www/cir/src/Component/User/UserManager.php:214
/var/www/cir/tests/Component/User/Manager/Creation/ATCPTest.php:122
Other tests run fine for other mocked classes, like here:
$this->SUserRoleRepository = $this->createStub(SUserRoleRepository::class);
So, why do I get a MappingException or TypeError, respectively, for some repositories but not others? Perhaps help me to understand the errors better? Thanks!
The issue was that my SType entity did not have an accurate #ORM\Entity(...) annotation. I had:
/**
* SType.
*
* #ORM\Table(name="s_type", indexes={#ORM\Index(name="idx_s_type_1", columns={"type_group"}), #ORM\Index(name="idx_s_type_2", columns={"code"})})
* #ORM\Entity
*/
class SType {}
Needed the repositoryClass relation:
/**
* SType.
*
* #ORM\Table(name="s_type", indexes={#ORM\Index(name="idx_s_type_1", columns={"type_group"}), #ORM\Index(name="idx_s_type_2", columns={"code"})})
* #ORM\Entity(repositoryClass="App\Repository\STypeRepository")
*/
class SType {}

PhpStorm with PHPUnit 8.4 gives exception Uncaught PHPUnit\Runner\Exception class ... could not be found

I tried to use PHPUnit v8. However I was not succeeded with PhpStorm. When I run simple test (class method) in PhpStorm I got the following message:
PHP Fatal error: Uncaught PHPUnit\Runner\Exception: Class 'Mrself\\TreeType\\Tests\\Functional\\BuildingTest' could not be found in '/vagrant/symfony-tree-type/tests/Functional/BuildingTest.php'. in /vagrant/symfony-tree-type/vendor/phpunit/phpunit/src/Runner/StandardTestSuiteLoader.php:65
Yes, I have that class and yes I have psr configured properly:
"autoload": {
"psr-4": {
"Mrself\\TreeType\\": "./src/"
}
},
"autoload-dev": {
"psr-4": {
"Mrself\\TreeType\\Tests\\": "./tests/"
}
}
The proof the I have everything correctly setup is that when I run vendor/bin/phpunit it gives me correct result.
When I run method in PhpStorm I got the following call:
/usr/bin/php /vagrant/symfony-tree-type/vendor/phpunit/phpunit/phpunit --configuration /vagrant/symfony-tree-type/phpunit.xml --filter "/(::testFormCanBeBuild)( .*)?$/" Mrself\\TreeType\\Tests\\Functional\\BuildingTest /vagrant/symfony-tree-type/tests/Functional/BuildingTest.php --teamcity
However if I prepend class namespace with \\ everything works correctly as well. I can not get a clue what's going on. PHPUnit version 7 works as well.
Same thing happened to me. All of the sudden I started getting the following error:
PHP Fatal error: Uncaught PHPUnit\Runner\Exception: Class 'Tests\\Feature\\ExampleTest' could not be found
And after I have read #frank-vue's comment I noticed the same thing and he did: If I run tests on the entire folder it runs normally, but if I run test on a specific class/method I get that error.
I tried earlier version of PHPStorm, downgraded PHP plugin etc... and nothing worked.
In my case, when I checked the stacktrace looks like:
#0 /var/www/vendor/phpunit/phpunit/src/Runner/BaseTestRunner.php(145): PHPUnit\Runner\StandardTestSuiteLoader->load('Tests\\\\Unit\\\\Ex...', '/var/www/tests/...')
#1 /var/www/vendor/phpunit/phpunit/src/Runner/BaseTestRunner.php(105): PHPUnit\Runner\BaseTestRunner->loadSuiteClass('Tests\\\\Unit\\\\Ex...', '/var/www/tests/...')
#2 /var/www/vendor/phpunit/phpunit/src/TextUI/Command.php(177): PHPUnit\Runner\BaseTestRunner->getTest('Tests\\\\Unit\\\\Ex...', '/var/www/tests/...', Array)
#3 /var/www/vendor/phpunit/phpunit/src/TextUI/Command.php(159): PHPUnit\TextUI\Command->run(Array, true)
#4 /var/www/vendor/phpunit/phpunit/phpunit(61): PHPUnit\TextUI\Command::main()
#5 {main}
thrown in /var/www/vendor/phpunit/phpunit/src/Runner/StandardTestSuiteLoader.php on line 69
Notice Tests\\\\Unit\\\\Ex... instead of Tests\\Unit\\Ex....
So in the end I broke the rule and I've modified vendor file, which should be avoided at any cost, but as a temporary solution it solves my problem.
So I added 1 line to the vendor/phpunit/phpunit/src/Runner/BaseTestRunner.php on line 98 (PHPUnit version 8.4.1), which replaces unnecessary '\'s.
if (empty($suiteClassFile) && \is_dir($suiteClassName) && !\is_file($suiteClassName . '.php')) {
/** #var string[] $files */
$files = (new FileIteratorFacade)->getFilesAsArray(
$suiteClassName,
$suffixes
);
$suite = new TestSuite($suiteClassName);
$suite->addTestFiles($files);
return $suite;
}
$suiteClassName = str_replace('\\\\', '\\', $suiteClassName); // THIS IS THE LINE I ADDED
try {
$testClass = $this->loadSuiteClass(
$suiteClassName,
$suiteClassFile
);
} catch (Exception $e) {
$this->runFailed($e->getMessage());
return null;
}

wordpress: Uncaught Error: Call to undefined function mb_internal_encoding()

I have just installed equifax-credit-check plugin in my wordpress site.Now this error displayed
Fatal error: Uncaught Error: Call to undefined function mb_internal_encoding() in /home/uplogictecz/public_html/demo/reportlink/wp-content/plugins/equifax-credit-check/vendor/danielstjules/stringy/src/Stringy.php:58 Stack trace: #0 /home/uplogictecz/public_html/demo/reportlink/wp-content/plugins/equifax-credit-check/vendor/danielstjules/stringy/src/Create.php(17): Stringy\Stringy->__construct('/reportlink/das...', NULL) #1 /home/uplogictecz/public_html/demo/reportlink/wp-content/plugins/equifax-credit-check/class/App.php(179): Stringy\create('/reportlink/das...') #2 /home/uplogictecz/public_html/demo/reportlink/wp-content/plugins/equifax-credit-check/class/PluginCore.php(300): Baerr\App\App::is_dashboard() #3 /home/uplogictecz/public_html/demo/reportlink/wp-includes/class-wp-hook.php(298): Baerr\App\PluginCore->dashboard_access_control('') #4 /home/uplogictecz/public_html/demo/reportlink/wp-includes/class-wp-hook.php(323): WP_Hook->apply_filters(false, Array) #5 /home/uplogictecz/public_html/demo/reportlink/wp-includes/pl in /home/uplogictecz/public_html/demo/reportlink/wp-content/plugins/equifax-credit-check/vendor/danielstjules/stringy/src/Stringy.php on line 58
In my /home/uplogictecz/public_html/demo/reportlink/wp-content/plugins/equifax-credit-check/vendor/danielstjules/stringy/src/Stringy.php on line 58
Line function is
public function __construct($str = '', $encoding = null)
{
if (is_array($str)) {
throw new InvalidArgumentException(
'Passed value cannot be an array'
);
} elseif (is_object($str) && !method_exists($str, '__toString')) {
throw new InvalidArgumentException(
'Passed object must have a __toString method'
);
}
$this->str = (string) $str;
$this->encoding = $encoding ?: \mb_internal_encoding();
}
This line is $this->encoding = $encoding ?: \mb_internal_encoding(); no 58.
How to fix this issue ?.Kindly check it.
Your PHP environment is missing MBSTRING extension. It is normal, as mbstring is not built-in default extension in some PHP installations. You can install it if you have an access to your server:
For PHP 5.* and Debian
sudo apt-get install php-mbstring
For PHP 5.* and Fedora
yum install php-mbstring
For PHP 7.0, use
sudo apt-get install php7.0-mbstring
And of course, if you don't have server access, you need to ask your hosting provider to enable this extension for your website server.

symfony 2.1: phpunit -c app tries to run symfony core tests

In my Symfony 2.1 app, I'm unable to run the recommended phpunit -c app/. I get the following error message (I added linebreaks):
PHPUnit 3.6.10 by Sebastian Bergmann.
Configuration read from /symfony2-dual-auth/app/phpunit.xml.dist
F
PHP Fatal error: main(): Failed opening required
'/symfony2-dual-auth/vendor/symfony/symfony/vendor/autoload.php'
(include_path='.:/usr/share/php:/usr/share/pear')
in /symfony2-dual-auth/vendor/symfony/symfony/autoload.php.dist on line 3
Complete source code here: https://github.com/meonkeys/symfony2-dual-auth . Except the test, since it doesn't pass. Here's src/Oh/FOSFacebookUserBundle/Tests/SimpleFunctionalTest.php...
<?php
namespace Oh\FOSFacebookUserBundle\Tests;
use Symfony\Bundle\FrameworkBundle\Tests\Functional\WebTestCase;
class SimpleFunctionTest extends WebTestCase {
public function testIndex() {
$client = static::createClient();
$crawler = $client->request('GET', '/login');
$this->assertEquals(1, $crawler->filter('html:contains("Remember me")')->count());
}
}
I was extending the wong class. Here's the fix:
-use Symfony\Bundle\FrameworkBundle\Tests\Functional\WebTestCase;
+use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

Resources