Class '..\Unit\UnitTestCase' not found - php

I am upgrading laravel from 5.5.* to 5.6.0. Composer install ran fine. I am trying to run unit tests upon which this error is popping up.
Class '..\Unit\UnitTestCase' not found
This is the stack trace of the error:
Fatal error: Uncaught Error: Class 'PhiraterTest\Unit\UnitTestCase'
not found in
/home/vagrant/code/phirater-l51/tests/unit/Phirater/AdditionalCurrencies/CreateAdditionalCurrencyCommandHandlerTest.php:11
Stack trace:
#0 /home/vagrant/code/phirater-l51/vendor/phpunit/phpunit/src/Util/Fileloader.php(64):
include_once()
#1 /home/vagrant/code/phirater-l51/vendor/phpunit/phpunit/src/Util/Fileloader.php(48):
PHPUnit\Util\Fileloader::load('/home/vagrant/c...')
#2 /home/vagrant/code/phirater-l51/vendor/phpunit/phpunit/src/Framework/TestSuite.php(325):
PHPUnit\Util\Fileloader::checkAndLoad('/home/vagrant/c...')
#3 /home/vagrant/code/phirater-l51/vendor/phpunit/phpunit/src/Framework/TestSuite.php(403):
PHPUnit\Framework\TestSuite->addTestFile('/home/vagrant/c...')
#4 /home/vagrant/code/phirater-l51/vendor/phpunit/phpunit/src/Runner/BaseTestRunner.php(65):
PHPUnit\Framework\TestSuite->addTestFiles(Array)
#5 /home/vagrant/code/phirater-l51/vendor/phpunit/phpunit/src/TextUI/Command.php(169):
PHPUnit\Runner\BaseTestRunner->getTest('tes in
/home/vagrant/code/phirater-l51/tests/unit/Phirater/AdditionalCurrencies/CreateAdditionalCurrencyCommandHandlerTest.php
on line 11
My unit tests are in tests/ directory. My UnitTestCase class in extended by \TestCase class and TestCase class is extended by BrowserKitTestCase. What am i doing wrong here? What could be the solution?

If your dependency class is really present and has proper namespace, then the most probable reasons of mentioned error are (avoid copying below examples to your project, that will definetely not work):
composer.json file (root directory) has no relevant record of the dependency (do not forget composer.lock file also), smth like:
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
as a result of above, vendor/autoload.php file (or any of its merged siblings, e.g. vendor/composer/autoload_classmap.php, depending on your case) can miss relevant record of the dependency, smth like:
return array(
'PHPUnit\\Framework\\TestCase' => '/vendor/phpunit/phpunit/src/Framework/TestCase.php',
In most such cases it is really a missed dependency delivered by autoload.php functionality, which in its turn caused by composer malfunctioning. So taking in account all that, try to update composer itself by running composer self-update, and then updating your dependencies by composer update.

Related

How to move dependency out of vendor in symfony 3?

I have "inherited" a project that has a git dependency I do not have access to. I have the contents of the whole /vendor directory and want to move the dependency out of it so that I do not need to commit the whole /vendor directory to git. I have moved the directory with the dependency to /my-vendor directory (at the same level as vendor), but have no idea how to include it into the project.
When running composer install I get:
[RuntimeException]
An error occurred when executing the "'cache:clear --no-warmup'" command:
Fatal error: Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "MY_BUNDLE_CLASS" from namespace "DEP_NAME\MY_BUNDLE".
Did you forget a "use" statement for another namespace? in /app/app/AppKernel.php:40
Stack trace:
#0 /app/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php(406): AppKernel->registerBundles()
#1 /app/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php(113): Symfony\Component\HttpKernel\Kernel->initializeBundles()
#2 /app/app/AppKernel.php(21): Symfony\Component\HttpKernel\Kernel->boot()
#3 /app/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php(137): AppKernel->boot()
#4 /app/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php(124): Symfony\Bundle\FrameworkBundle\Console\Application->registerCommands()
#5 /app/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php(89): Symfony\Bundle\FrameworkBundle\Console\Application->add(Object(Symfony\Component\Console in /app/app/AppKernel.
php on line 40
MY_BUNDLE_CLASS, DEP_NAME, and MY_BUNDLE being edited.
You can place those bundles in bundle directory, for example, and then add to composer.json following lines:
"repositories": [
{
"type" : "path",
"url" : "./bundles/your-bundle"
}
]

Namespace autoloading with composer and PSR-0 not working as expected

I have made a test project to understand how composer and packagist works. The project is also on packagist.
A simple composer require rakibtg/gowin will install the package from packagist.
But for some reason the namespacing is not working as expected.
Here is my directory structure and the composer file.
Here is the GoWin.php file:
<?php
namespace GoWin;
class GoWin {
public function serve() {
echo 'Lets Win Everybody!';
}
}
Here is the test.php file where i am trying to use the serve() method from the GoWin class.
<?php
require_once './vendor/autoload.php';
// use GoWin;
( new GoWin\GoWin() )->serve();
But it fails to execute the serve method with this error:
Fatal error: Uncaught Error: Class 'GoWin\GoWin' not found in
/Users/usr/Desktop/t estGoWin/index.php:7 Stack trace:
0 {main} thrown in /Users/usr/Desktop/testGoWin/index.php on line 7
At this moment i cant understand what i am missing! Also should i use psr-0 or psr-4?
I solved it by switching to PSR-4, simply update the composer.json autolaod property as this:
"autoload": {
"psr-4": {
"GoWin\\": "src/"
}
},

How to require a package installed via Composer

I installed emanueleminotto/simple-html-dom via composer.
How can I use classes from the package without getting an error?
Note: I use XAMPP to run PHP scripts.
Error Message:
PHP Fatal error: Uncaught Error: Class 'simple_html_dom' not found in C:\xampp\htdocs\Practice\PHP\scrape_1.php:3
Stack trace:
0 {main}
thrown in C:\xampp\htdocs\Practice\PHP\scrape_1.php on line 3
Fatal error: Uncaught Error: Class 'simple_html_dom' not found in C:\xampp\htdocs\Practice\PHP\scrape_1.php:3
Stack trace:
0 {main}
thrown in C:\xampp\htdocs\Practice\PHP\scrape_1.php on line 3
After running
$ composer install
require the autoloader generated in vendor/autoload.php at the top of your script file (or, for a web application, in the front controller).
Then you will have all autoloaded classes available in your script.
<?php
require_once __DIR__ . '/vendor/autoload.php';
$htmlDom = new simple_html_dom_node();
For reference, see https://getcomposer.org/doc/01-basic-usage.md#autoloading.
apparently emanueleminotto/simple-html-dom doesn't use a namespace so by default uses the global namespace. the clean solution would be to include the vendor/autoload.php (created/generated/updated by composer) and use the classes/functions by prepending \, to indicate the global namespace ... unless you work in the global namespace yourself, in which case you don't have to prepend.
You should be able to just use them. If I see that right, the whole package is really only one file which is autoloader by composer.
If you include the vendor/autoload.php file in your PHP Script, you should be good to go with the classes in the package.

Symfony3: Fatal error: Class 'AppKernel' not found in .\bin\console

just started working on a project, I ran composer update and was greeted with a exception when trying to clear cache.
When I try to run php bin\console server:run I am greeted with this message:
php bin\console server:run
PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: Fatal error: Class 'AppKernel' not found in CoreBundle\bin\console:27
Stack trace:
#0 {main}
thrown in CoreBundle\bin\console on line 27
Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: Fatal error: Class 'AppKernel' not found in CoreBundle\bin\console on line 27
Symfony\Component\Debug\Exception\FatalThrowableError: Fatal error: Class 'AppKernel' not found in CoreBundle\bin\console on line 27
Call Stack:
0.0112 427536 1. Symfony\Component\Debug\ErrorHandler->handleException() CoreBundle\vendor\symfony\symfony\src\Symfony\Component\Debug\ErrorHandler.php:0
Another strong possibility, particularly if the project has been updated from a v2.7 project (or before) is that the AppKernel is just not known to Composer. It is now best practice to not manually require/include the file, (so those lines are removed from web/app_*.php, and bin/console.php). but instead have it autoloaded. However, this requires a line in Composer so that it can be found. Only the composer autoloader would ever be included manually, which can then load everything else.
"autoload": {
"files": ["app/AppKernel.php"],
"psr-4": { // etc...
There are two possible reasons for this.
First your autoloader is trashed or can't find files
composer dump-autoload
Second reason could be, that your var directory isn't writable in which the cache file for symfony is placed.
Just check the permissions.
Also take a close look into the log files.
Maybe it tells you what the real Problem is.
(Like an issue with syntax etc.)
For some reason, I had to explicitly add src/Kernel.php to autoload. So my composer.json autoload section became:
"autoload" : {
"files": ["app/AppKernel.php",
"src/Kernel.php"],
"psr-4": { "": "src/"},
"classmap": [
"app/AppKernel.php",
"app/AppCache.php"
]
}
After adding this, running composer dump-autoload did the trick.

PHPUnit losing class name (ReflectionClass class does not exist error)

I'm trying to get PHPUnit working; I haven't used it before. If I run phpunit in the CLI, it works, but via a script (which is what I'm looking for) it doesn't. This is the output I get (note the two spaces after Class):
Fatal error: Uncaught exception 'ReflectionException' with message 'Class does not exist' in /usr/local/share/pear/PHPUnit/Util/Test.php:295
Stack trace:
#0 /usr/local/share/pear/PHPUnit/Util/Test.php(295): ReflectionClass->__construct('')
#1 /usr/local/share/pear/PHPUnit/Util/Test.php(576): PHPUnit_Util_Test::parseTestMethodAnnotations(false, false)
#2 /usr/local/share/pear/PHPUnit/Util/Test.php(350): PHPUnit_Util_Test::getBooleanAnnotationSetting(false, false, 'backupGlobals')
#3 /usr/local/share/pear/PHPUnit/Framework/TestSuite.php(458): PHPUnit_Util_Test::getBackupSettings(false, false)
#4 /usr/local/share/pear/PHPUnit/Framework/TestSuite.php(834): PHPUnit_Framework_TestSuite::createTest(Object(ReflectionClass), false)
#5 /usr/local/share/pear/PHPUnit/Framework/TestSuite.php(212): PHPUnit_Framework_TestSuite->addTestMethod(Object(ReflectionClass), Object(ReflectionMethod))
#6 /usr/local/share/pear/PHPUnit/Framework/TestSuite.php(315): PHPUnit_Framework_TestSuite->__construct(Object(ReflectionClass))
#7 /var/www/www.s in /usr/local/share/pear/PHPUnit/Util/Test.php on line 295
It appears that the class name is being lost in addTestMethod. This is my code:
<?php
require_once 'PHPUnit/Autoload.php';
class MyTestCase extends PHPUnit_Framework_TestCase {
public function testSubtraction() {
$this->assertEquals(2 - 2, 0);
}
public function testAddition() {
$this->assertEquals(2 + 2, 4);
}
}
$c = new MyTestCase();
$suite = new PHPUnit_Framework_TestSuite();
$suite->addTestSuite('MyTestCase');
PHPUnit_TextUI_TestRunner::run($suite);
Am I setting things up wrong?
Here are the versions:
Installed packages, channel pear.phpunit.de:
============================================
Package Version State
File_Iterator 1.3.3 stable
PHPUnit 3.7.21 stable
PHPUnit_MockObject 1.2.3 stable
PHP_CodeCoverage 1.2.11 stable
PHP_Timer 1.0.4 stable
PHP_TokenStream 1.1.5 stable
Text_Template 1.1.4 stable
PHP 5.4
Reflection does work, I use that in my own scripts. Hopefully it's something with my setup, I really don't want to have to modify PHPUnit...surely it works elsewhere.
Something is wrong:
'Class does not exist'
^
`- normally the classname is given here
As you can see your error message is either missing something or PHPUnit tried to create a testcase out of nothing. Check you don't have some superfluous / incomplete files in your tests folder. Or something like that along the line. It's just a guess based on the error message, if I were you I would tackle this down in a debug session to actually find out what happens. This includes remove-debugging the testing session and stepping through and probably adding debug code into the phpunit install. Take care and backups.

Categories