Home > Blockchain >  Wanna unit-test a function, but constructor gives error
Wanna unit-test a function, but constructor gives error

Time:01-14

I am trying to unit test a function which is in an entity class, and it is stored in my DB by the use of a constructor. Each time I am trying to test this function it is giving me that error

ArgumentCountError: Too few arguments to function App\Entity\Deal::__construct(), 0 passed in /var/www/html/casus/tests/dealsEntityFunctionsTest.php on line 10 and exactly 1 expected

It is obvious I think, but I am really new with unit testing and that stuff so I couldn't find the answer. Could you please help me?

My code is

class Deal
{
    private bool $isNewToday

    public function __construct($deal)
    {
        $this->isNewToday = $deal['is_new_today'];
    }

    public function getIsNewToday(): ?bool
    {
        return $this->isNewToday;
    }

    public function setIsNewToday(bool $isNewToday): self
    {
        $this->isNewToday = $isNewToday;

        return $this;
    }
}

And my unit test is

class test extends TestCase
{
    public function testIsNewTodayIsTrue()
    {
        $deal = new Deal();
        $deal->setIsForSale(true);
        $this->assertTrue($deal->getIsForSale(), true);
    }
}

CodePudding user response:

As brombeer suggested, new Deal entity requires parameter. This parameter looks like an array, with key 'is_new_today'. So, sth like this below should help with constructor error.

class test extends TestCase
{
    public function testIsNewTodayIsTrue()
    {
        $deal = new Deal(['is_new_today' => true]);
        $deal->setIsForSale(true);
        $this->assertTrue($deal->getIsForSale(), true);
    }
}

CodePudding user response:

This has nothing to do with Unit Testing, or Symfony, or any of the other details you mentioned. You've defined something with a mandatory parameter, and then aren't passing that parameter.

Just like any function, the parameters to a constructor are mandatory unless you provide a default. And if you write code that assumes the parameter will have a particular format, you need to provide a value that meets that assumption.

So either pass the parameter every time you create the object, with whatever format the constructor expects:

$deal = new Deal(['is_new_today' => false]);

... or make it optional, and decide what should happen if it's not passed:

class Deal
{
    private bool $isNewToday

    public function __construct(?array $deal = null)
    {
        if ( isset($deal) ) {
            $this->isNewToday = $deal['is_new_today'];
        }
        else {
            $this->isNewToday = false;
        } 
    }
}

Note that $isNewToday is defined as a non-nullable boolean, so you should always give it a value in the constructor, or an inline default, like private bool $isNewToday = false; Otherwise, you'll get "uninitialized value" errors if you try to read it. For that reason, the return type of ?bool on getIsNewToday() doesn't make sense - it can't return null, because $this->isNewToday can never be bool.

  •  Tags:  
  • Related