PHP Fatal error: Interface 'PHPUnit\Framework\TestListener' not found - testing

I'm working on a CakePHP 3.x project on a Vagrant VM. My project has a single Test for a Controller. Project was setup using composer and phpunit is installed in vendor directory.
Running ./vendor/bin/phpunit Tests throws the following error:
[vagrant#devenv-lamp ~/www]$ ./vendor/bin/phpunit Tests
PHP Fatal error: Interface 'PHPUnit\Framework\TestListener' not found in /vagrant/vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureInjector.php on line 30
Fatal error: Interface 'PHPUnit\Framework\TestListener' not found in /vagrant/vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureInjector.php on line 30
I tried running PHPUnit for a single Controller as well - with the same results...
[vagrant#devenv-lamp ~/www]$ ./vendor/bin/phpunit tests/TestCase/Controller/QuestionsControllerTest.php
PHP Fatal error: Interface 'PHPUnit\Framework\TestListener' not found in /vagrant/vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureInjector.php on line 30
Fatal error: Interface 'PHPUnit\Framework\TestListener' not found in /vagrant/vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureInjector.php on line 30
I'm very new to PHPUnit and am running into a wall even without taking off the ground. Any help here will be greatly appreciated.

It looks like autoloading is not set up correctly.
In your phpunit.xml you need to specify a bootstrap file. This can either be a custom script based on your project or in some cases it's as easy as using composer's autoload.php:
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/3.7/phpunit.xsd"
bootstrap="vendor/autoload.php"
>
...
</phpunit>

Related

Can not run Clojure project with Cursive using Leiningen

I am unable to run a simple test project using the intellij plugin cursive. I am able to open a REPL without any problems and can even call functions defined in the core.clj file.
Running the project directly however yields this error message:
Error running 'new-project.core': failed to access class com.intellij.execution.impl.ProcessStreamsSynchronizer from class com.intellij.execution.impl.ConsoleViewRunningState (com.intellij.execution.impl.ProcessStreamsSynchronizer is in unnamed module of loader com.intellij.util.lang.UrlClassLoader #402f32ff; com.intellij.execution.impl.ConsoleViewRunningState is in unnamed module of loader com.intellij.ide.plugins.cl.PluginClassLoader #c9a79eb)
Intellij run configuration here. Changing How to run it and What to run does nothing.
Starting the project via the Leiningen tab yields the same error message.
There is an issue open about it.
https://github.com/cursive-ide/cursive/issues/2350
Also I tried to do lein run Let me know if this work for you.

Configuring UnetStack in Intellij IDEA

I'm trying to use IntelliJ IDEA for my UnetStack Project.
I'm following this blog
https://blog.unetstack.net/using-idea-with-unetstack
I get this error when done following this:
Error: Could not find or load main class org.codehaus.groovy.tools.GroovyStarter
I had the same issue.
This happened to you because you ran your simulation as a groovy script and not as the configured application. Run it as application and groovyStarter will not even be used.

Fatal error: Class 'PHPUnit_Extensions_Selenium2TestCase' not found in *.php

I am completely new to Seleniu, and am trying to set up PHPUnit with Selenium 2, following this tutorial https://www.youtube.com/watch?v=zva_GETXimI
So far this is what I've done:
Installed PHPUnit using PHAR as instructed here https://phpunit.de/manual/current/en/installation.html
Installed Selenium Server
Upgraded to PHP5.6 (but my Apache is still 2.22 instead of 2.4)
Installed PHPUnit_selenium package using Composer
Created a testLogin.php file:
<?php
class testLogin extends PHPUnit_Extensions_Selenium2TestCase{
public function setUp()
{
$this->setHost('localhost');
$this->setPort(4444);
$this->setBrowser('firefox');
$this->setBrowserUrl('http://localhost/Achievers');
}
}
When I run testLogin.php using 'phpunit testLogin.php' I get an error:
Class 'PHPUnit_Extensions_Selenium2TestCase' not found in /home/osadmin/projects/Sel/testLogin.php on line 5
Since I am just starting out, I have no idea how to fix this. On googling the error, it shows to add this line to my php file:
require_once('PHPUnit/Extensions/Selenium2TestCase.php');
But I'm not sure how I can use this since there is no PHPUnit folder, just a file linked to the PHAR.
Could someone please shed some light on how to fix this issue?
Thanks.
EDIT:
As per Ushakov's suggestion, I tried /path/to/phpunit.phar testLogin.php
But it gives the same error.
If I add this line to the testLogin.php: require_once('/usr/local/bin/phpunit');
and run /path/to/phpunit.phar testLogin.php
I get this:
#!/usr/bin/env php
PHP Notice: Constant __PHPUNIT_PHAR__ already defined in /usr/local/bin/phpunit on line 18
PHP Notice: Constant __PHPUNIT_PHAR_ROOT__ already defined in /usr/local/bin/phpunit on line 19
PHP Fatal error: Cannot redeclare class DeepCopy\DeepCopy in phar:///usr/local/bin/phpunit/myclabs-deep-copy/DeepCopy/DeepCopy.php on line 15
Solved it by adding this line to the PHP file:
require_once 'vendor/autoload.php';
No tutorial I have seen mentions this, but it can be found in github projects
For anyone else coming late to the party - this worked for me:
class testLogin extends \PHPUnit\Extensions\Selenium2TestCase
(Using PHP 7.0.3 and installing Selenium package with Composer ).

Fatal error: Cannot redeclare class WebTestCase in C:\wamp\www\testapp\protected\tests\WebTest ase.php on line 17

i am using phpunit with selenium in yii to test default created testing cases.
when i am trying to run SiteTest.php file from tests folder then its giving error like
Fatal error: Cannot redeclare class WebTestCase in C:\wamp\www\testapp\protected\tests\WebTest ase.php on line 17
and when i am trying to run from functional folder then its showing CWebTestCase not defined
i am running this command from command line
protected\test>phpunit functional/SiteTest.php
system configuration
window 7 64 bit
phpunit is installed
selenium is installed
pear is installed
latest version of all with latest version of yii as well. please help me asap.
regards
anil

Fatal error running unit test in Yii app

This Question is local to my situation and not resolved (yet). But if you are experiencing this problem, the trouble shooting steps may give you a good path to start on.
I want to run a unit test in a Yii web application on localhost, which is running via WampServer 2.1 on Windows 7.
<?php
class LittleTest extends CTestCase
{
public function testApprove()
{
$value1 = "1";
$this->assertEquals($value1,$value1);
}
}
?>
I receive a fatal error when I try to run the test. Here is how I run it, on the Windows command line:
C:\wamp\www\app\protected\tests>
C:\wamp\www\app\protected\tests>cd unit
C:\wamp\www\app\protected\tests\unit>phpunit LittleTest.php
I receive (along with some stack trace lines):
PHP Fatal error:
class 'CTestCase' not found in [path to file]\LittleTest.php on line 4
Trouble shooting steps to this point:
The app runs. The default index page of the app looks good and I have used the gii tool to create a model class.
From command line, I can see php and phpunit are available (and I've been over my pear install to make sure it's all good):
C:\wamp\www\app\protected\tests>
C:\wamp\www\app\protected\tests>phpunit --version
PHPUnit 3.7.13 by Sebastian Bergmann.
C:\wamp\www\app\protected\tests>
C:\wamp\www\app\protected\tests>php --version
PHP 5.3.5 (cli)
... etc
display_errors is turned on. display_startup_errors is turned on.
I tried renaming the class so that name did not match the document name:
class LittleTestTweak extends CTestCase
I'm not sure of the precise command that runs the test, so I have tried variants like:
php LittleTest.php
Also I've tried running it various places in the folder structure. Here is the immediate structure:
/tests
| bootstrap.php
| my_tree.txt
| phpunit.xml
| WebTestCase.php
|
|---- /fixtures
|---- /functional
| SiteTest.php
|
|---- /report
`---- /unit
LittleTest.php
I also checked my php.ini for the path to PEAR; as far as I can tell, it's correct (but how can I test it?):
include_path=".;C:\wamp\bin\php\php5.3.5\PEAR;C:\wamp\www\app
More Info
In response to this:
cd wamp\www\app\protected\tests
phpunit unit\LittleTest.php
I receive this:
Warning: require_once(PHPUnit/Extensions/SeleniumTestCase.php):
failed to open stream: No such file or directory in
C:\wamp\www\yii\framework\test\CWebTestCase.php on line 12
Call Stack:
0.0007 339624 1. {main}() C:\wamp\bin\php\php5.3.5\phpunit:0
0.0164 698440 2. PHPUnit_TextUI_Command::main()
C:\wamp\bin\php\php5.3.5\phpunit:46
0.0164 698856 3. PHPUnit_TextUI_Command->run()
C:\wamp\bin\php\php5.3.5\PEAR\PHPUnit\TextUI\Command.php:129
0.0164 698856 4. PHPUnit_TextUI_Command->handleArguments()
C:\wamp\bin\php\php5.3.5\PEAR\PHPUnit\TextUI\Command.php:138
0.0289 1220944 5. PHPUnit_TextUI_Command->handleBootstrap()
C:\wamp\bin\php\php5.3.5\PEAR\PHPUnit\TextUI\Command.php:606
0.0300 1233328 6. PHPUnit_Util_Fileloader::checkAndLoad()
C:\wamp\bin\php\php5.3.5\PEAR\PHPUnit\TextUI\Command.php:778
0.0330 1233424 7. PHPUnit_Util_Fileloader::load()
C:\wamp\bin\php\php5.3.5\PEAR\PHPUnit\Util\Fileloader.php:76
0.0334 1238096 8. include_once
('C:\wamp\www\app\protected\tests\bootstrap.php')
C:\wamp\bin\php\php5.3.5\PEAR\PHPUnit\Util\Fileloader.php:92
0.0412 1520256 9. require_once
('C:\wamp\www\app\protected\tests\WebTestCase.php')
C:\wamp\www\app\protected\tests\bootstrap.php:8
0.0413 1520520 10. YiiBase::autoload()
C:\wamp\www\yii\framework\YiiBase.php:0
0.0423 1543904 11.
include('C:\wamp\www\yii\framework\test\CWebTestCase.php')
C:\wamp\www\yii\framework\YiiBase.php:395
Fatal error: require_once(): Failed opening required
'PHPUnit/Extensions/SeleniumTestCase.php'
(include_path='.;C:\wamp\bin\php\php5.3.5\PEAR\pear;C:\wamp\bin\php\php5.3.5\pear')
in C:\wamp\www\yii\framework\test\CWebTestCase.php on line 12
Call Stack:
0.0007 339624 1. {main}() C:\wamp\bin\php\php5.3.5\phpunit:0
0.0164 698440 2. PHPUnit_TextUI_Command::main()
...et cetera...
The failed requirement is PHPUnit/Extensions/SeleniumTestCase.php. I wonder if the issue is that PHPUnit is installed locally under C:\wamp.
I opened my php.ini and added to include_path: C:\wamp\bin\php\php5.3.5\PEAR\PHPUnit\Extensions. Restarted wamp. No change in error reporting.
RESOLUTION
My security setup uses DansGuardian and I had neglected to loosen the settings for banned extension types, which blocks file downloads. In fact I don't care to ban any types, and modifying that file allows everything to work. Woops, that's my Linux set. PHPUnit is working there, and it is working on WAMP also. Recreating the steps on WAMP is impossible; but I do know I had to open cmd.exe as administrator and pear update-channels, pear upgrade-all, etc. I also had to clear pear's cache at one point, and I had to overcome an issue with curl recognition to install Selenium:
pear install --force phpunit/PHPUnit_Selenium
To run unit tests in Yii with phpunit, you'll need to let phpunit load the protected/tests/bootstrap.php file which basically sets up a configuration, and autoloads the required classes (mainly pertaining to testing). The bootstrap.php file loads yiit.php which actually autoloads the required classes.
Now we can load all this configuration either by command line options when running phpunit, or let the configuration be read automatically through the protected/tests/phpunit.xml file.
For the latter method, the directory from where phpunit is invoked should have the phpunit.xml file in it, and in Yii default webapp, this directory is protected/tests. Therefore you need to do the following to run your tests:
cd wamp\www\app\protected\tests
phpunit unit\LittleTest.php