phpunit icon indicating copy to clipboard operation
phpunit copied to clipboard

array_merge(): Argument #1 must be of type array, null given - TestCase.php:1540

Open geeksesi opened this issue 3 years ago • 6 comments

Q A
PHPUnit version 9.5.11
PHP version 8.0.14
Installation Method Composer

Summary

i'm using laravel 8 with php 8 and when i want to run my tests i will get error.

array_merge(): Argument #1 must be of type array, null given

verbose output :

  • Tests\Feature\Api\V1\User\UserTest > can register new user with random number                                                                                                                          [4/1918]
   TypeError 
                                                    
  array_merge(): Argument #1 must be of type array, null given

  at vendor/phpunit/phpunit/src/Framework/TestCase.php:1540     
    1536▕                 'PHPUnit\Framework\TestCase::$name must be a non-blank string.'
    1537▕             );
    1538▕         }
    1539▕ 
  ➜ 1540▕         $testArguments = array_merge($this->data, $this->dependencyInput);
    1541▕ 
    1542▕         $this->registerMockObjectsFromTestArguments($testArguments);
    1543▕ 
    1544▕         try {

  1   vendor/phpunit/phpunit/src/Framework/TestCase.php:1540
      array_merge()

  2   vendor/phpunit/phpunit/src/Framework/TestCase.php:1151
      PHPUnit\Framework\TestCase::runTest()

  3   vendor/phpunit/phpunit/src/Framework/TestResult.php:726
      PHPUnit\Framework\TestCase::runBare()

  4   vendor/phpunit/phpunit/src/Framework/TestCase.php:903
      PHPUnit\Framework\TestResult::run()

  5   vendor/phpunit/phpunit/src/Framework/TestSuite.php:678
      PHPUnit\Framework\TestCase::run()

  6   vendor/phpunit/phpunit/src/Framework/TestSuite.php:678
      PHPUnit\Framework\TestSuite::run()

  7   vendor/phpunit/phpunit/src/Framework/TestSuite.php:678
      PHPUnit\Framework\TestSuite::run()

  8   vendor/phpunit/phpunit/src/TextUI/TestRunner.php:670
      PHPUnit\Framework\TestSuite::run()

  9   vendor/phpunit/phpunit/src/TextUI/Command.php:143
      PHPUnit\TextUI\TestRunner::run()

  10  vendor/phpunit/phpunit/src/TextUI/Command.php:96
      PHPUnit\TextUI\Command::run()

  11  vendor/phpunit/phpunit/phpunit:98
      PHPUnit\TextUI\Command::main()


how to solve : (but not good)

at vendor/phpunit/phpunit/src/Framework/TestCase.php:1540
make a type check below this line like this :

        if (!is_array($this->data)) {
            $this->data = [];
        }
        $testArguments = array_merge($this->data, $this->dependencyInput);

so guys have you any idea ? why i have get this error ?!

even i made a log on __construct like this :

    public function __construct(?string $name = null, array $data = [], $dataName = '')
    {
        if ($name !== null) {
            $this->setName($name);
        }
        dump($name, $data, $dataName);
        $this->data     = $data;
        $this->dataName = $dataName;
    }

and the out put is :

^ null                                                                                                                                                                                                             
^ []                                                                                                                                                                                                               
^ ""                                                                                                                                                                                                               
^ null                                                                                                                                                                                                             
^ []                                                                                                                                                                                                               
^ ""                                                                                                                                                                                                               
^ null                                                                                                                                                                                                             
^ []                                                                                                                                                                                                               
^ ""     

on object creation $data is array. and i didn't found where this variable will be changed,

geeksesi avatar Dec 29 '21 15:12 geeksesi

Thank you for your report.

Please provide a minimal, self-contained, reproducing test case that shows the problem you are reporting.

Without such a minimal, self-contained, reproducing test case I will not be able to investigate this issue.

sebastianbergmann avatar Dec 29 '21 15:12 sebastianbergmann

@sebastianbergmann thank you.

this is my entire test folder. tests.zip

"laravel/framework": "^8.65"

geeksesi avatar Dec 30 '21 06:12 geeksesi

Something that uses/requires Laravel is not self-contained.

sebastianbergmann avatar Dec 30 '21 06:12 sebastianbergmann

I can get the same error just by using a constructor in a unit test class, changing it to use setUp() function fixed it for me.

phpws avatar Jan 12 '22 17:01 phpws

You must not implement a constructor in a class that extends PHPUnit\Framework\TestCase.

sebastianbergmann avatar Jan 12 '22 17:01 sebastianbergmann

Just ran into this issue on one of our repositories and it appears that you can use a constructor method on a class that extends TestCase, as long as you make sure to pass the values needed for the TestCase constructor onto the parent. If you do not pass these values on you will encounter the error mentioned above.

    class ApplicationTestSuite extends TestCase {
        public function __construct($name = null, array $data = [], $dataName = '') {
            parent::__construct($name, $data, $dataName);
            .....
        };
        ......
    }

Xenology avatar Mar 16 '22 23:03 Xenology