Welcome Guest, Not a member yet? Register   Sign In
PHPUnit strangeness
#1

I should start off by saying that I have PHPUnit working with the CI v4 dev repository. I've been using it successfully for a while and mostly know what I'm doing with it. But I ran into something very curious that I cannot explain and hope the hive-mind might have some insight or ideas. There's quite a lot of setup here so stick with me.

I'm looking into CodeIgniter\Log\Logger and it's associated "handlers" - the code behind this page of documentation.

I have a phpunit.xml that's used to control exactly which tests to run. It was created from phpunit.xml.dist that comes with the install and it looks like this.

Code:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="tests/_support/_bootstrap.php"
        backupGlobals="false"
        colors="true"
        convertErrorsToExceptions="true"
        convertNoticesToExceptions="true"
        convertWarningsToExceptions="true"
        stopOnError="false"
        stopOnFailure="false"
        stopOnIncomplete="false"
        stopOnSkipped="false">
    <testsuites>
        <testsuite name="logs">
            <!--<directory>./tests/system/Log</directory>-->
            <file>./tests/system/Log/LoggerTest.php</file>
            <!--<file>./tests/system/Log/FileHandlerTest.php</file>-->
        </testsuite>

<!--not showing the rest because it's not relevant -->

The most relevant (to where I'm eventually going) section is

Code:
<testsuite name="logs">
    <!--<directory>./tests/system/Log</directory>-->
    <file>./tests/system/Log/LoggerTest.php</file>
    <!--<file>./tests/system/Log/FileHandlerTest.php</file>-->
 </testsuite>

Where I can run all or any of the logger tests by changing what is or is not commented. Executing PHPUnit with the xml file shown above will run only LoggerTest.php  resulting in a lovely green bar with the feel-good message of success for all tests in that test case.

I can use either of the following variations in the xml and get all green with both.

(runs all three tests in the directory)
Code:
<testsuite name="logs">
    <directory>./tests/system/Log</directory>
    <!--<file>./tests/system/Log/LoggerTest.php</file>-->
    <!--<file>./tests/system/Log/FileHandlerTest.php</file>-->
 </testsuite>

(runs only the two test files indicated)
Code:
<testsuite name="logs">
    <!--<directory>./tests/system/Log</directory>-->
    <file>./tests/system/Log/LoggerTest.php</file>
    <file>./tests/system/Log/FileHandlerTest.php</file>
 </testsuite>

The CLI command used is the same (where ./phpunit is a symbolic link to ./vendor/bin/phpunit)
Quote:./phpunit --testsuite logs

Finally, we get to the weird part.

If you run against only the one file ./tests/system/Log/FileHandlerTest.php or swap the order of the two files so FileHandlerTest.php comes first - all the FileHandlerTest tests fail. (There's only four of them.)

The problem seems to be due to autoloading because the message displayed is
Quote:Error: Class 'Tests\Support\Config\MockLogger' not found

Which happens on line 19 which is:
PHP Code:
$config = new LoggerConfig(); 

And, yes I believe the proper use statements are there. Look at the files yourself. This post is big enough without adding all that code. The unit test files are found in /your/folders/to/CodeIgniter4/tests/system/Log/

It gets even weirder though.

If you remove (or comment out) the setup() method of FileHandlerTest the first test succeeds but the rest fail due to undefined property errors. That makes sense because the properties were defined in the now missing setiup(). But wait a minute, where'd the "Class not found" error message go??? Apparently the class is found now.

I'm dumbfounded. (or maybe just dumb) What's going on?  Anybody?
Reply
#2

What happens if you take the namespace off of FileHandlerTest?
Reply
#3

(10-18-2019, 05:17 PM)MGatner Wrote: What happens if you take the namespace off of FileHandlerTest?

Same.

Tried that and other things, but the question was long and confusing enough.

Also tried moving setting up the vfsStream into each test method. Then changed the setUp() to this

PHP Code:
class FileHandlerTest extends \CIUnitTestCase
{
 protected 
$bogus;

 
    protected function setUp(): void
     
{
 
        $this->bogus 'bogus';
 
    

Same fail.

If setUp() is removed then everything works.

I tried upgrading PHPUnit to the latest version. Same fail, plus some deprecation warnings on a few Assertions.

... --- ... We Interrupt this Reply with Breaking NEWS!!!

Got it working!

Compare the above setUp() the this one

PHP Code:
class FileHandlerTest extends \CIUnitTestCase
{
 protected 
$bogus;

 
    protected function setUp(): void
     
{
         parent::setUp();
 
        $this->bogus 'bogus';
 
    
Reply




Theme © iAndrew 2016 - Forum software by © MyBB