Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I assertInstanceOf() in PHPUnit?

I have write this piece of code for a Unit test:

<?php

namespace tests;

use data\address\Address;
use lists\Collection;
use PHPUnit\Framework\TestCase;

class CollectionTest extends TestCase
{
    public function testIsCollectionOf()
    {
        $Collection = new Collection(Address::class);
        $this->assertInstanceOf($Collection, Collection::class);
    }
}

When I run over it I got this errors:

PHPUnit 5.4.6 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.0.6-6+donate.sury.org~trusty+1
Configuration: /var/www/project/phpunit.xml

E                                                                   1 / 1 (100%)


Time: 33 ms, Memory: 2.00MB

There was 1 error:

1) tests\CollectionTest::testIsCollectionOf
PHPUnit_Framework_Exception: Argument #1 (No Value) of PHPUnit_Framework_Assert::assertInstanceOf() must be a class or interface name

/var/www/project/vendor/phpunit/phpunit/src/Util/InvalidArgumentHelper.php:30
/var/www/project/vendor/phpunit/phpunit/src/Framework/Assert.php:1323
/var/www/project/tests/CollectionTest.php:19
/var/www/project/vendor/phpunit/phpunit/src/Framework/TestCase.php:1081
/var/www/project/vendor/phpunit/phpunit/src/Framework/TestCase.php:932
/var/www/project/vendor/phpunit/phpunit/src/Framework/TestResult.php:701
/var/www/project/vendor/phpunit/phpunit/src/Framework/TestCase.php:888
/var/www/project/vendor/phpunit/phpunit/src/Framework/TestSuite.php:753
/var/www/project/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:465
/var/www/project/vendor/phpunit/phpunit/src/TextUI/Command.php:162
/var/www/project/vendor/phpunit/phpunit/src/TextUI/Command.php:113

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

Now if I var_dump($Collection) I got:

object(Collection)#18 (2) {
  ["class_name":"Collection":private]=>
  string(32) "data\address\Address"
  ["_items":protected]=>
  array(0) {
  }
}

Why? What's wrong in my test case?

like image 804
ReynierPM Avatar asked Dec 18 '22 15:12

ReynierPM


1 Answers

The order of arguments is wrong. Try this:

$this->assertInstanceOf(Collection::class, $Collection);
like image 160
Weltschmerz Avatar answered Jan 05 '23 17:01

Weltschmerz