fmalk/codeigniter-phpunit

My test file isn't called

Closed this issue · 11 comments

Hi,

Thank's for this. I'm running on PHP 5.5.14.

I've replaced my 3 core system files by yours. Here is my XML file:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/bootstrap.php">
    <testsuites>
        <testsuite name="offer">
            <directory suffix=".php">./tests/offer</directory>
        </testsuite>
    </testsuites>
    <php>
        <const name="PHPUNIT_TEST" value="1" />
        <const name="PHPUNIT_CHARSET" value="UTF-8" />
        <server name="REMOTE_ADDR" value="0.0.0.0" />
    </php>
    <filter>
        <blacklist>
            <directory suffix=".php">sys</directory>
        </blacklist>
    </filter>
</phpunit>

My tests folder is in the root.

/tests/bootstrap.php

<?php
/*
*---------------------------------------------------------------
* OVERRIDE FUNCTIONS
*---------------------------------------------------------------
*
* This will "override" later functions meant to be defined
* in core\Common.php, so they throw erros instead of output strings
*/
function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
{
    throw new PHPUnit_Framework_Exception($message, $status_code);
}
function show_404($page = '', $log_error = TRUE)
{
    throw new PHPUnit_Framework_Exception($page, 404);
}
/*
*---------------------------------------------------------------
* BOOTSTRAP
*---------------------------------------------------------------
*
* Bootstrap CodeIgniter from index.php as usual
*/
require_once dirname(__FILE__) . '/../index.php';
/*
* This will autoload controllers inside subfolders
*/
spl_autoload_register(function ($class) {
    foreach (glob(APPPATH.'controllers/**/'.strtolower($class).'.php') as $controller) {
        require_once $controller;
    }
});

I've updated the index.php. The file is loaded as an echo is OK.

/tests/offer/HelperTest.php:

<?php
echo 'helper';
class EmailHelperTest extends PHPUnit_Framework_TestCase
{
    private $CI;

    public static function setUpBeforeClass()
    {
        $CI =& get_instance();
        $CI->load->helper('email');
    }

    public function testEmailValidation()
    {
        $this->assertTrue(valid_email('test@test.com'));
        $this->assertFalse(valid_email('test#test.com'));
    }
}
?>

My echo isn't called and no assert...

~ phpunit
PHPUnit 4.0.20 by Sebastian Bergmann.


phpunit works well in other projects. Also tried phpunit --testsuite offer.

PS: I don't need to test a database. My point is to test a library, but anyway, an helper is OK to start with...

Bests

Let me try to replicate your environment, I'll get back to you

@sylouuu I ran a test using your file and environment as you described, and it works fine.
I do run PHPUnit 3.7 as I have not updated to 4.x, because I had problems with that version before (see #4). However, Travis runs PHPUnit 4.1.3 and all is well.
Your console command doesn't show any feedback (no "no test executed!", no errors), so it seems a problem with your particular PHPUnit executable.

Hi @fmalk, I tried with 3.7, same result, no output.

~ phpunit 
PHPUnit 3.7.38 by Sebastian Bergmann.

Did you try with PHP 5.5 ?

~ php -v
PHP 5.5.14 (cli) (built: Sep  9 2014 19:09:25) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies

No, I haven't, my environment uses 5.4.
However, what is recommended in those cases of no output by PHPUnit is to try the simplest TestCase possible (assertTrue(true), no bootstrap, no .xml, call TestCase file directly) and see if it works. From there, increase complexity of the test case little by little to check where the error is occurring.

No xml, tests/MoneyTest.php:

<?php
class MoneyTest extends PHPUnit_Framework_TestCase
{
    public function testCanBeNegated()
    {
        $this->assertTrue(true);
    }
}
~ phpunit tests/MoneyTest.php
PHPUnit 4.1.1 by Sebastian Bergmann.

.

Time: 39 ms, Memory: 2.50Mb

OK (1 test, 1 assertion)

Great, now work it up to your desired test: create a phpunit.xml to call that test. Modify it to use the desired folder structure (`tests/offer'), and so on.

It's better, it works if I call directly the file. But without specifying it:

~ phpunit
PHP Fatal error:  Uncaught exception 'PHPUnit_Framework_Exception' with message 'Class "Offer" does not extend PHPUnit_Framework_TestCase.' in /Users/syl/Sites/web-appoftheday/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:183
Stack trace:
#0 phar:///usr/local/bin/phpunit/phpunit/Util/Configuration.php(851): PHPUnit_Framework_TestSuite->__construct('offer')
#1 phar:///usr/local/bin/phpunit/phpunit/Util/Configuration.php(825): PHPUnit_Util_Configuration->getTestSuite(Object(DOMElement), NULL)
#2 phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php(683): PHPUnit_Util_Configuration->getTestSuiteConfiguration(NULL)
#3 phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php(141): PHPUnit_TextUI_Command->handleArguments(Array)
#4 phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php(132): PHPUnit_TextUI_Command->run(Array, true)
#5 /usr/local/bin/phpunit(584): PHPUnit_TextUI_Command::main()
#6 {main}
  thrown in /Users/syl/Sites/web-appoftheday/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php on line 183

Fatal error: Uncaught exception 'PHPUnit_Framework_Exception' with message 'Class "Offer" does not extend PHPUnit_Framework_TestCase.' in /Users/syl/Sites/web-appoftheday/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php:183
Stack trace:
#0 phar:///usr/local/bin/phpunit/phpunit/Util/Configuration.php(851): PHPUnit_Framework_TestSuite->__construct('offer')
#1 phar:///usr/local/bin/phpunit/phpunit/Util/Configuration.php(825): PHPUnit_Util_Configuration->getTestSuite(Object(DOMElement), NULL)
#2 phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php(683): PHPUnit_Util_Configuration->getTestSuiteConfiguration(NULL)
#3 phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php(141): PHPUnit_TextUI_Command->handleArguments(Array)
#4 phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php(132): PHPUnit_TextUI_Command->run(Array, true)
#5 /usr/local/bin/phpunit(584): PHPUnit_TextUI_Command::main()
#6 {main}
  thrown in /Users/syl/Sites/web-appoftheday/vendor/phpunit/phpunit/PHPUnit/Framework/TestSuite.php on line 183

Same with phpunit --testsuite offer

Well, that exception seems pretty self-explanatory to me.

But the Offer class is my library, app/libraries/offer.php. :)

So I test it with the full path and it's OK...

Then you have a problem with your phpunit.xml.

Okay I had to write "TestSuite" and works with PHPUnit 4.x.x:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/bootstrap.php">
    <testsuites>
        <testsuite name="TestSuite">
            <directory suffix=".php">tests/</directory>
        </testsuite>
    </testsuites>
    <php>
        <const name="PHPUNIT_TEST" value="1" />
        <const name="PHPUNIT_CHARSET" value="UTF-8" />
        <server name="REMOTE_ADDR" value="0.0.0.0" />
    </php>
    <filter>
        <blacklist>
            <directory suffix=".php">sys</directory>
        </blacklist>
    </filter>
</phpunit>

Thanks!