For a given project I have two php files to run php unittests, which are located in subdirectories.
The first file (AllTestSuite.php) looks like this:
<?php
require_once dirname(__FILE__).'/unittest.inc.php';
class AllTestsSuite {
public static function suite(){
return new GlobTestsSuite(dirname(__FILE__), "/*/*Suite.php");
}
}
?>
and the second file (unittest.inc.php) looks like this:
<?
class GlobTestsSuite extends PHPUnit_Framework_TestSuite {
public function __construct($sDirectory, $sExpression){
parent::__construct();
foreach (glob("$sDirectory/$sExpression") as $sTest){
require_once($sTest);
$this->addTestSuite(basename($sTest, ".php"));
}
}
}
?>
The tests itself are invoked (on Ubuntu, phpunit Version 3.7.27) as follows:
phpunit AllTestsSuite ./AllTestsSuite.php
which gives the error message
PHP Fatal error: Class 'GlobTestsSuite' not found in /home/.../AllTestsSuite.php on line 6
I do not understand what is going here. The file AllTestsSuite.php imports the other file in which the class GlobTestSuite is defined. How to fix this problem?
Related
I have a folder test. In this folder I have a two classes, TestHeader and TestHeaders.
This is TestHeader class
<?php
class TestHeader
{
public function send(): void
{
}
}
This is TestHeaders class
<?php
class TestHeaders
{
public function profile(int $userId): TestHeader
{
return new TestHeader();
}
}
And when I run tests, show me error Fatal error: Uncaught Error: Class "TestHeader" not found in C:\project\test\TestHeaders.php:7 I don't know why show this error. Someone could me explain why this error is and how I can fix it ?
if you'r not using a php framework that implement the autoload and if you not set up an autoload strategy you have to include all file you'r trying to use in another file.
add include("TestHeader.php"); at start of TestHeaders class, on the line immediately after the <?php tag
In the same direcory I have:
html/app
Plugin.php
PluginContract.php
GetUid.php
On GetUid.php i have this code:
<?php
namespace App;
include_once "Plugin.php";
include_once "PluginContract.php";
use Carbon\Carbon;
use TeamSpeak3\Ts3Exception;
class GetUid extends Plugin implements PluginContract
{
public function isTriggered()
{
...
}
}
And this is PluginContract.php:
<?php
namespace App;
interface PluginContract
{
public function isTriggered();
}
It seems all ok, but I got this error:
PHP Fatal error: Interface 'App\PluginContract' not found in /var/www/html/app/GetUid.php on line 11
The weird thing is that it can load Plugin.php without problem but it gets this error for PluginContract.php which is in the same folder.
What I'm doing wrong?
What I was tring to do is add a web interface to a PHP application, I though that I don't need the autoload because it automatically start with the server, but I discovered that adding again the autoload to my index.php I resolved the problem.
A Simpletest test in MyTest.php that runs with no errors:
require_once ('simpletest/autorun.php');
class MyTest extends UnitTestCase {
function test() {
//Test...
}
}
A SimpleTest test suite
require_once ('simpletest/autorun.php');
class AllTests extends TestSuite {
function __construct() {
$this->TestSuite('All tests');
$this->addFile('MyTest.php');
}
}
The error:
Fatal error: Cannot redeclare simpletest_autorun() (previously declared in /Library/WebServer/Documents/Option/tests/simpletest/autorun.php:26) in /Library/WebServer/Documents/option/tests/simpletest/autorun.php on line 34
The (horrible) solution, change MyTest.php to:
if (!function_exists("simpletest_autorun")) {
require_once ('simpletest/autorun.php');
}
class MyTest extends UnitTestCase {
function test() {
//Test...
}
}
It appears that this example follows the documentation, it doesn't or SimpleTest has this bug?
I can only assume you have multiple copies of the SimpleTest autorun.php script.
Performing the require in your test case file attempts to include a second autorun.php file (different to the one included in your test suite) which defines simpletest_autorun() for the second time.
The fact that the line numbers don't match up adds credence to my assumption.
Looking at the full paths in the error messages should show you what's up.
Determine which copy you want to keep and remove the other, updating any incorrect paths in your code or configuration, including the include_path in php.ini.
I use symfony 2.4.0. I want to use my custom class as discussed here: Autoloading a class in Symfony 2.1. I have created subfolder in src:
namespace Yur;
class MyClass {
public go() {
var_dump('hello!! 32');
}
}
In my controller, I made this:
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Yur\MyClass;
class WelcomeController extends Controller
{
public function indexAction()
{
$my = new MyClass();
$my->go();
die();
...
but it makes an exception:
ClassNotFoundException: Attempted to load class "MyClass" from namespace "Yur" in /var/www/shop.loc/src/Acme/DemoBundle/Controller/WelcomeController.php line 12. Do you need to "use" it from another namespace?
After I have got this exception, I decided consciously to make syntax error exception in my class to see if it loaded. I changed class MyClass ... to class4 MyClass ..., but doesnt got asyntax error` exception. And I decided, that my class is not loaded.
Is anyone knows why? And what I must to do to resolve?
A few things. First, in your code sample above, you have
public go() {
var_dump('hello!! 32');
}
which should be
public function go() {
var_dump('hello!! 32');
}
The former raises a parser error in PHP. and probably isn't what you want.
Second, the error
ClassNotFoundException: Attempted to load class "MyClass" from namespace "Yur" in /var/www/shop.loc/src/Acme/DemoBundle/Controller/WelcomeController.php line 12. Do you need to "use" it from another namespace?
is the error Symfony uses when it attempt to autoload a class, but can't find the file. This usually means your file is named incorrectly, or in the wrong folder. I'd tripped check that you have a file in the directory you think you do.
$ ls src/Yur/MyClass.php
You can also add some debugging to the composer autoload code to see what path it's cooking up for your custom class
#File: vendor/composer/ClassLoader.php
public function findFile($class)
{
//...
$classPath .= strtr($className, '_', DIRECTORY_SEPARATOR) . '.php';
//start your debugging code
if($class == 'Yur\MyClass')
{
//dump the generated path
var_dump($classPath);
//dump the default include paths
var_dump($this->fallbackDirs);
}
//...
}
I have the following Selenium test in PHP:
<?php
require_once('PHPUnit/Extensions/SeleniumTestCase.php');
class Example extends PHPUnit_Extensions_SeleniumTestCase
{
protected function setUp()
{
$this->setBrowser("*chrome");
$this->setBrowserUrl("http://change-this-to-the-site-you-are-testing/");
}
public function testMyTestCase()
{
$this->open("/frontend_dev.php");
try {
$this->assertTrue($this->isTextPresent("Local Coupons"));
} catch (PHPUnit_Framework_AssertionFailedError $e) {
array_push($this->verificationErrors, $e->toString());
}
}
}
When I try to run it (by running "php filename.php") I get the error:
PHP Fatal error: Class 'PHPUnit_Framework_TestCase' not found in /usr/share/php/PHPUnit/Extensions/SeleniumTestCase.php on line 60
Which makes sense because the class isn't defined anywhere, but why not? I installed PHPUnit. It seems a little weird that a class called PHPUnit_Framework_TestCase wouldn't be included with PHPUnit. Any ideas?
You should read the docs for PHPUnit for the command line test runner. You can also just run 'phpunit --help' from the command line. I believe you'll find that you need to instead run something like
phpunit path/to/filename.php