简体   繁体   中英

I am getting error after running below phpunit test case

<?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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM