PHPUnit 6.1.x throws array_merge() error when my test class uses its own constructor method
Asked Answered
R

2

37

I get this error:

1) XTest::testX
array_merge(): Argument #1 is not an array

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

On this test case:

use PHPUnit\Framework\TestCase;

class XTest extends TestCase
{

    function __construct()
    {}

    function testX()
    {
        $this->assertTrue(true);
    }
}

If I remove __construct method, my tests pass. What is going on with PHPUnit's handling of my class constructor methods? It worked fine in PHPUnit version 4.8, but now I am using PHPUnit version 6.1.3

Roussillon answered 3/5, 2017 at 14:21 Comment(12)
what if you call parent::__construct(); ? The signature can be found here github.com/sebastianbergmann/phpunit/blob/6.1.3/src/Framework/…Inkberry
interestingly, it will not throw error if I use parent::__construct();Roussillon
You are better off using the setUp() methods for initialization. Further more you can get a stacktrace when your run phpunit with the -v verbose flag.Inkberry
How would you use setUp() for initialization? Sometimes I have classes that use __construct() method legitimately. How would you use setUp on them?...Roussillon
setUp is automatically called by phpunit after your constructor is called. You can find more information about other method here : phpunit.de/manual/current/en/fixtures.htmlInkberry
You can't override a method that requires an argument with one that doesn't (well theoretically you could, but you never should)Disfrock
thanks, thus perhaps I can rename my __constructor to say setUp for any initialization tasks, although in my case I need to initialize it once per class, while setUp does it once per test caseRoussillon
You can also use setUpBeforeClass which is called really once. setUp is called for every specified test in your class.Inkberry
also just to note, using -v option did not uncover any more info in my case, it gave the same error message, and i.e. did not give me the offending line where it crashed. I had to sleuth this one out. I am curious though that if I use parent::__construct(), still without any parameters, it did work fineRoussillon
The verbose flag doesn't work you're rightInkberry
It happens because the constructor of the TestCase sets $this->data = []Inkberry
There is no reason to implement __construct() in a TestCase. Use setUp() for initialization. It is invoked before each test.Impish
I
58

PHPUnit uses the constructor for initialization of the base TestCase

You can see the constructor method here: https://github.com/sebastianbergmann/phpunit/blob/6.1.3/src/Framework/TestCase.php#L328

public function __construct($name = null, array $data = [], $dataName = '')

You shouldn't use the constructor, because it's used by phpunit and any change to the signature etc can break things.

You can use the special setUp and setUpBeforeClass methods which phpunit will call for you.

use PHPUnit\Framework\TestCase;

class XTest extends TestCase
{
    function static setUpBeforeClass()
    { 
       // Called once just like normal constructor
       // You can create database connections here etc
    }

    function setUp()
    {
      //Initialize the test case
      //Called for every defined test
    }

    function testX()
    {
        $this->assertTrue(true);
    }

    // Clean up the test case, called for every defined test
    public function tearDown() { }

    // Clean up the whole test class
    public static function tearDownAfterClass() { }
}

The docs: https://phpunit.de/manual/current/en/fixtures.html

Note that the setUp gets called for every specified test in the class.

For a single initialization you can use setUpBeforeClass.

And another tip: run your phpunit with the -v flag to display stack traces ;)

Inkberry answered 3/5, 2017 at 14:35 Comment(7)
it looks like setUpBeforeClass needs to be static to work properly, while setUp does not need to beRoussillon
No, using the constructor is not a solution. Use setUp().Rattlebox
Hey sebastian could you explain that a bit? I know it's bad practice but not sure the reasons behind it.Inkberry
The constructor "belongs" to PHPUnit. setUp() is the extension point for users.Rattlebox
So by overwriting the __constructor you should follow the exact same constructor signature defined by phpunit. Therefor it's dis encouraged because users can easily break thingsInkberry
@SebastianBergmann I find it logical. However since it did work in PHPUnit 4.8, it would be very nice to see a bit more descriptive error message than array_merge(): Argument #1 is not an array. Maybe a hint like setUp() should be used instead of __construct().Alluvium
@SebastianBergmann Using setUp or setUpBeforeClass seems a not complete solution because they are static classes. No way to init data like $this->myvarforothermethod = .... Adding parent::__construct(); in custom constructor solve the trouble for me and make my tests compatible with phpunit v6.1 and lower.Cursorial
A
20

As Sander Visser's answer correctly pointed, the parent constructor might have additional parameters, etc, and generally you'd want to use setUpBeforeClass or setUp, but, if you know what you're doing, you can call parent::__construct(); in the constructor of your Test class:

public function __construct() {
    parent::__construct();
    // Your construct here
}

Edit 2019

In Codeception, this can also be caused by an invalid YML indentation on your suite file.

Appledorf answered 4/1, 2018 at 20:54 Comment(3)
better solutionDc
+1: useful for when you want to set non-static variables once for the test class - even though that may not be good practice. Tests may not be atomic if the state of those non-static variables changes from test to test.Marteena
I think it would be great and easiest to start for all.Tatro

© 2022 - 2024 — McMap. All rights reserved.