1
<?php

namespace Tests\AppBundle\Component\CalculationMethod\ParentChild;

use AppBundle\Component\CalculationMethod\ParentChild\CalculationMethod;
use AppBundle\Component\QueryBuilder\JqueryToRuleSet\Interpreter;
use AppBundle\Component\ScoreEngine\ScoreResultChecker;
use AppBundle\Entity\CalculationPeriod;
use AppBundle\Entity\Sla;
use AppBundle\Entity\SlaScore;
use AppBundle\Form\DataTransformer\DateTimeImmutableToAirDatePickerTransformer;
use PHPUnit\Framework\TestCase;
use AppBundle\Component\DataProvider\DataProviderFactory;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\DBAL\DBALException;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\ORMException;

/**
 * Class CalculationMethodTest
 * @package Tests\AppBundle\Component\CalculationMethod\ParentChild
 */
class CalculationMethodTest extends TestCase
{
    /** @var Sla */
    private $parentSla;
    /** @var CalculationPeriod */
    private $calculationPeriod;
    /** @var CalculationMethod */
    private $calculationMethod;
    private $dataProviderFactory;
   
    protected function setUp()
    {
        $rulesJson = '{  "condition": "AND",  "rules": [    {      "id": "1",      "field": "1",      "type": "boolean",      "input": "text",      "operator": "passed",      "value": null    },    {      "id": "2",      "field": "2",      "type": "boolean",      "input": "text",      "operator": "passed",      "value": null    },    {      "condition": "OR",      "rules": [        {          "id": "11",          "field": "11",          "type": "boolean",          "input": "text",          "operator": "passed",          "value": null        },        {          "id": "10",          "field": "10",          "type": "boolean",          "input": "text",          "operator": "failed",          "value": null        }      ]    }  ],  "valid": true}';
        $rulesInterpreter = (new Interpreter(new DateTimeImmutableToAirDatePickerTransformer('Y-m-d', new \DateTimeZone('Europe/London'))));
        $ruleSet = $rulesInterpreter->interpret(json_decode($rulesJson, true));
        $this->calculationMethod = new CalculationMethod($ruleSet, new ScoreResultChecker());
        $this->parentSla = new Sla();
        $this->calculationPeriod = new CalculationPeriod();
        //EntityManagerInterface $entityManager;
        //$this->entityManager = $entityManager;
    }

    public function testCalculatesPassForAllPassingScores()
    {
        $entityManager = $this->getMockBuilder(EntityManagerInterface::class)->getMock();
        $slaid = 18;  
        $slaScore = $entityManager->getRepository('AppBundle:Sla')->findById($slaid);
        $dataProvider = $this->dataProviderFactory->create($slaScore->getSla());
        $score = $this->calculationMethod->calculateScore($dataProvider, $slaScore, $this->calculationPeriod);
        $this->assertEquals("PASS", $score->getTextValue());
    }

private function generateScore(int $slaId, bool $hasPassed)
    {
        $sla = new Sla();
        $reflection = new \ReflectionClass($sla);
        $property = $reflection->getProperty('id');
        $property->setAccessible(true);
        $property->setValue($sla, $slaId);
        $sla->setTargetMinimum(80);
        $sla->setTargetExpected(90);
        $slaScore = new SlaScore($sla, $this->calculationPeriod);
        $slaScore->setValue($hasPassed ? 95 : 70); 
        return $slaScore;
    }
}
?>

I am getting the error as command

phpunit --filter testCalculatesPassForAllPassingScores

PHP Fatal error:Uncaught Error: Call to a member function findById() on null.

I am running this testcase but I am getting this error How to resolve this problem can you help me to resolve this issue?

1
  • Why would you mock EntityManager in this test? I don't see it used as a dependency and you seem to want to use it for just loading something from a database. In that case you'd need a real em not a mocked one, but I would just hardcode the entity in the test. Commented Oct 25, 2020 at 20:16

1 Answer 1

1

The error message is pretty clear, you are calling the findById() method from a null value instead of a Repository instance.

Given your code, in my opinion you should replace the line below

$slaScore = $entityManager->getRepository('AppBundle:Sla')->findById($slaid);

by this

$slaScore = $entityManager->getRepository(Sla::class)->findById($slaid);
Sign up to request clarification or add additional context in comments.

1 Comment

I tried this also - $slaScore = $entityManager->getRepository(Sla::class)->findById($slaid); But, I am getting the same error as - PHP Fatal error: Uncaught Error: Call to a member function findById() on null. can you suggest me more answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.