在phpunit测试用例下运行后出现错误

问题描述

<?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",{      "condition": "OR","rules": [        {          "id": "11","field": "11","value": null        },{          "id": "10","field": "10","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;
    }
}
?>

我收到命令错误

PHPunit --filter testCalculatesPassForAllPassingscores

PHP致命错误:未捕获错误调用成员函数findById() 为空。

我正在运行此测试用例,但出现此错误。如何解决此问题,您可以帮助我解决此问题吗?

解决方法

错误消息非常清楚,您正在从findById()值而不是null实例调用Repository方法。

鉴于您的代码,我认为您应该替换下面的行

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

以此

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