Simpletest is running all of my tests twice. Why? - php

I have a simpletest suite I've been working on writing for some of my recent API wrapper code in PHP. But every time I run the test, it runs all of the tests twice.
My calling code:
require_once(dirname(__FILE__) . '/simpletest/autorun.php');
require_once('CompanyNameAPI.php');
$test = new TestSuite('API test');
$test->addFile(dirname(__FILE__) . '/tests/authentication_test.php');
if (TextReporter::inCli()) {
exit ($test->run(new TextReporter()) ? 0 : 1);
} else {
$test->run(new HtmlReporter());
}
authentication_test.php looks like:
class Test_CallLoop_Authentication extends UnitTestCase {
function test_ClassCreate(){
$class = new CallLoopAPI();
$this->assertIsA($class, CallLoopAPI);
}
//More tests
}
There aren't any more includes to autorun.php or other calls to simpletest within authentication_test.php either.
Ideas?

You should change your calling code like this:
require_once(dirname(__FILE__) . '/simpletest/autorun.php');
require_once('CompanyNameAPI.php');
$test = new TestSuite('API test');
$test->addFile(dirname(__FILE__) . '/tests/authentication_test.php');
autorun.php file executes automatically your tests calling run() methods implicitly, when you call run() method you execute tests again.

From simpletests documentation, you should use the static method prefer(REPORTER)
<?php
require_once('show_passes.php');
require_once('simpletest/simpletest.php');
SimpleTest::prefer(new ShowPasses());
require_once('simpletest/autorun.php');
class AllTests extends TestSuite {
function __construct() {
parent::__construct('All tests');
$this->addFile(dirname(__FILE__).'/log_test.php');
$this->addFile(dirname(__FILE__).'/clock_test.php');
}
}
?>

Related

How to make phpunit reload in a console application using php

I'm creating a console application to help me develop my website in php.
The console application runs in a while loop, prints a menu with options and I'll input a number to tell the application what I want it to do. In One of my option I am running phpunit test, when I run the test it works but when I update my test and press my option again, phpunit displays "No tests executed!". When I restart the console app it will work but i still have the same problem when I try to run the unit test second time.
Here is a code example
I have tired instantiating a new phpunit command and then use the run method.
I have tried breaking out of the loop and call begin again.
require_once __DIR__ . '/vendor/autoload.php';
public function printMenu()
{
echo "*******menu*****";
echo "\r\n";
echo "option 1: do something";
echo "\r\n";
echo "option 2: Do unit test:";
echo "\r\n";
echo "option 3: Exit";
echo "\r\n";
}
public function begin(){
while(true){
$this->printMenu();
$input = trim(fgets(STDIN));
if($input == 1){
$this->doSomething();
}else if($input ==2){
PHPUnit\TextUI\Command::main(false);
}else if($input == 3){
break;
}
}
}
//Sample test class (just an example)
use PHPUnit\Framework\TestCase;
class SampleTest extends TestCase
{
public function testTrueAssertsToTrue(): void {
$this->assertTrue(true);
$this->assertTrue(true);
$this->assertTrue(false);
$this->assertTrue(true);
$this->assertTrue(true);
}
}
//update will be
use PHPUnit\Framework\TestCase;
class SampleTest extends TestCase
{
public function testTrueAssertsToTrue(): void
{
$this->assertTrue(true);
$this->assertTrue(true);
$this->assertTrue(true);
$this->assertTrue(true);
$this->assertTrue(true);
}
}
I managed to solve it by changing
PHPUnit\TextUI\Command::main(false);
to
./vendor/bin/phpunit

PHPUnit cannot find source code

I have a PHP project, with the following project structure.
php_test_app
src
Vegetable.php
tests
StackTest.php
VegetableTest.php
The code of these files is shown below. I use PDT and PTI in Eclipse. PHPUnit in Eclipse recognizes that VegetableTest.php belongs to Vegetable.php, because you can toggle between them using the toggle button.
I first try to run the test code by selecting the tests directory in the PHP Explorer and click Run Selected PHPUnit Test. It runs both tests, both the VegetableTest fails with the following trace: Fatal error: Class 'Vegetable' not found in /Users/erwin/Documents/workspace/php_test_app/tests/VegetableTest.php on line 8. A similar issue was posted here: phpunit cannot find Class, PHP Fatal error.
Indeed, I haven't included my source code yet, so now I uncomment the include in VegetableTest.php, shown below. If I now try to run the tests in the same way, PHPUnit does not recognize any test code! Even the StackTest, which is unaltered, is not recognized.
How should I make the include such that the unit tests are
recognized?
Do I need to specify the full path, or just the name of
the file in which the class is defined?
Changing the include statement also doesn't work; I have tried the following.
include 'Vegetable.php';
include 'src/Vegetable.php';
include '../src/Vegetable.php';
Vegetable.php
<?php
// base class with member properties and methods
class Vegetable {
var $edible;
var $color;
function Vegetable($edible, $color="green")
{
$this->edible = $edible;
$this->color = $color;
}
function is_edible()
{
return $this->edible;
}
function what_color()
{
return $this->color;
}
} // end of class Vegetable
// extends the base class
class Spinach extends Vegetable {
var $cooked = false;
function Spinach()
{
$this->Vegetable(true, "green");
}
function cook_it()
{
$this->cooked = true;
}
function is_cooked()
{
return $this->cooked;
}
} // end of class Spinach
StackTest.php
<?php
class StackTest extends PHPUnit_Framework_TestCase
{
public function testPushAndPop()
{
$stack = array();
$this->assertEquals(0, count($stack));
array_push($stack, 'foo');
$this->assertEquals('foo', $stack[count($stack)-1]);
$this->assertEquals(1, count($stack));
$this->assertEquals('foo', array_pop($stack));
$this->assertEquals(0, count($stack));
}
}
?>
VegetableTest.php
<?php
// require_once ('../src/Vegetable.php');
class VegetableTest extends PHPUnit_Framework_TestCase
{
public function test_constructor_two_arguments()
{
$tomato = new Vegetable($edible=True, $color="red");
$r = $tomato.is_edible();
$this->assertTrue($r);
$r = $tomato.what_color();
$e = "red";
$this->assertEqual($r, $e);
}
}
class SpinachTest extends PHPUnit_Framework_TestCase
{
public function test_constructor_two_arguments()
{
$spinach = new Spinach($edible=True);
$r = $spinach.is_edible();
$this->assertTrue($r);
$r = $spinach.what_color();
$e = "green";
$this->assertEqual($r, $e);
}
}
?>
phpunit --bootstrap src/Vegetable.php tests instructs PHPUnit to load src/Vegetable.php before running the tests found in tests.
Note that --bootstrap should be used with an autoloader script, for instance one generated by Composer or PHPAB.
Also have a look at the Getting Started section on PHPUnit's website.

Run function before all tests in PHPUniit

I am looking at PHPunit for running Selenium tests and can run tests fine. What's missing though is the ability to run a function before the tests are run (let's say to set up some config).
<?php
require_once 'SeleniumTest.php';
class MyTest extends SeleniumTest
{
public function testTest1()
{
$this->runSelenese(__DIR__ . '/test1.html');
}
public function testTest2()
{
$this->runSelenese(__DIR__ . '/test2.html');
}
}
The config file is just a regular html file, but needs to be run once before anything else is run
Edit
Another read of the setUp() function has led me to believe that my tests should be setup as follows.
What I want to simulate is a user logging in, setting up some config, running the test and then logging out of the application at the end of the test.
This is what I have come up with; please advise on what I have done wrong, as the tests don't run at the moment. All tests timeout: is this how I should be approaching this?
<?php
require_once 'SeleniumTest.php';
class MyTest extends SeleniumTest
{
public function setUp()
{
$login_path = '/login_path/Login.html';
$logout_path = '/logout_path/Logout.html';
$config_path = '/config_path/config.html';
$this->runSelenese($login_path);
$this->runSelenese($config_path);
$this->runSelenese($logout_path);
}
public function testTest1()
{
$this->runSelenese(__DIR__ . '/test1.html');
}
public function testTest2()
{
$this->runSelenese(__DIR__ . '/test2.html');
}
}

PHP spl_autoload_register is doing nothing

i'm updating my code and trying to use spl_autoload_register but IT SIMPLY DOESN'T WORK!!!
I'm using PHP 5.3.8 - Apache 2.22 on Centos / Ubuntu / Win7 and trying to echo something but i get nothing instead... have been trying to make it work for the last 3 hours with no result... this is driving me mad!!!
class ApplicationInit {
// Constructor
public function __construct() {
spl_autoload_register(array($this, 'classesAutoloader'));
echo 'construct working...!';
}
// Autoloading methods
public function classesAutoloader($class) {
include 'library/' . $class . '.php';
echo 'autoload working...!';
}
}
first echo from __construct works but the "classesAutoloader" doesn't work at all, this class is defined in a php file within a folder and i'm calling it from index.php like follows:
define('DS', DIRECTORY_SEPARATOR);
define('ROOT', getcwd() . DS);
define('APP', ROOT . 'application' . DS);
// Initializing application
require(APP.'appInit.php');
$classAuto = new ApplicationInit();
any help is truly appreciated, thanks in advance!
It seems like you're doing the wrong thing. The function that you pass to spl_autoload_register is what's responsible for loading the class file.
Your code is calling
$classAuto = new ApplicationInit();
but by that time, ApplicationInit is already loaded so the autoload function doesn't get called
A more logical way would be for you to to call
spl_autoload_register(function($class){
include 'library/' . $class . '.php';
});
Then when you call
$something = new MyClass();
And the MyClass is not defined, then it will call your function to load that file and define the class.
What is your problem? Your code is working correctly.
class ApplicationInit {
public function __construct() {
spl_autoload_register(array($this, 'classesAutoloader'));
echo 'construct working...!';
}
public function classesAutoloader($class) {
include 'library/' . $class . '.php';
echo 'autoload working...!';
}
}
$classAuto = new ApplicationInit(); //class already loaded so dont run autoload
$newclass = new testClass(); //class not loaded so run autoload

PHP autloading failing in __sleep() magic method

I'm having issues with autoloading classes in PHP's magic __sleep() method. Autoloading doesn't take place, so the class cannot be found. In an attempt to debug this I tried calling spl_autoload_functions() which then causes PHP to segfault...
The example code below demonstrates the problem. Using an instance method or a static method has the same behaviour. This seems to work fine for me using __destruct() instead, which suits my use case fine, but I'm curious as to the reason behind this. Is it a PHP bug, or is there a sensible explanation?
In Foo.php, just as an autoload target
<?php
class Foo {
public static function bar() {
echo __FUNCTION__;
}
}
?>
In testcase.php
<?php
class Autoloader {
public static function register() {
// Switch these calls around to use a static or instance autoload function
spl_autoload_register('Autoloader::staticLoad');
//spl_autoload_register(array(new self, 'instanceLoad'));
}
public function instanceLoad($class) {
require_once dirname(__FILE__) . '/' . $class . '.php';
}
public static function staticLoad($class) {
require_once dirname(__FILE__) . '/' . $class . '.php';
}
}
Autoloader::register();
class Bar {
public function __sleep() {
// Uncomment the next line to segfault php...
// print_r(spl_autoload_functions());
Foo::bar();
}
}
$bar = new Bar;
This can be run by placing both files in a directory and running php testcase.php. This occurs for me with PHP 5.3.3 and 5.2.10.
The problem you describe sounds very similar to this entry in the PHP bug tracker:
http://bugs.php.net/bug.php?id=53141
That bug was fixed in PHP 5.3.4 (search for "53141" on http://php.net/ChangeLog-5.php).

Categories