Symfony 5单元测试无法获取实体集合

问题描述

我尝试在单元测试中访问它时,我的收藏集为空。在dev && prod env中,可以按方面进行访问。

一个customerOrder可以有多个发票,而一个发票可以有一个customerOrder。

class CustomerOrderServiceTest extends KernelTestCase
{
use RefreshDatabaseTrait;

public function testAdd()
{
    self::bootKernel();
    $container = self::$container;

    $orderRepository = $container->get(CustomerOrderRepository::class);
    $customerOrder = $orderRepository->find(1);
    foreach ($customerOrder->getInvoices() as $invoice) {
        dump('Invoice id: ' . $invoice->getId());
    }
    dump('Invoice count: ' . $customerOrder->getInvoices()->count());

    $invoiceRepository = $container->get(InvoiceRepository::class);
    $invoice = $invoiceRepository->find(1);
    dump('Order id: ' . $invoice->getCustomerOrder()->getId());
    dump('Invoice count: ' . $invoice->getCustomerOrder()->getInvoices()->count());
}
}

输出

测试项目测试套件 ^“发票计数:0” ^“订单ID:1” ^“发票数:0”

因此可以通过发票访问订单,但不能通过其他方式访问。

实体:

/**
* CustomerOrder
* @ORM\Entity(repositoryClass="App\Repository\CustomerOrderRepository")
*/
class CustomerOrder{
/**
 * @var Collection
 * @ORM\OnetoMany(targetEntity="App\Entity\Invoice",mappedBy="customerOrder",cascade={"persist"},fetch="EAGER")
 */
    private $invoices;
}


/**
* Invoice
* @ORM\Entity(repositoryClass="App\Repository\InvoiceRepository")
* @ORM\Table(uniqueConstraints={@ORM\UniqueConstraint(name="number",columns={"number","owner"})})
*/
class Invoice{
/**
 * @var CustomerOrder
 */
  private $customerOrder;
}
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://PHPunit.readthedocs.io/en/latest/configuration.html -->
<PHPunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="bin/.PHPunit/PHPunit.xsd"
         colors="true"
         bootstrap="tests/bootstrap.PHP"
>
    <PHP>
        <ini name="error_reporting" value="-1"/>
        <server name="APP_ENV" value="test" force="true"/>
        <server name="SHELL_VERBOSITY" value="-1"/>
        <server name="SYMFONY_PHPUNIT_REMOVE" value=""/>
        <server name="SYMFONY_PHPUNIT_VERSION" value="7.5"/>

        <!-- ###+ symfony/framework-bundle ### -->
        <env name="APP_ENV" value="test" force="true"/>
        <env name="APP_SECRET" value="*****"/>
        <!-- ###- symfony/framework-bundle ### -->
    </PHP>

    <testsuites>
        <testsuite name="Project Test Suite">
            <directory>tests</directory>
        </testsuite>
    </testsuites>

    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".PHP">src</directory>
        </whitelist>
    </filter>

    <listeners>
        <listener class="Symfony\Bridge\PHPUnit\SymfonyTestsListener" />
    </listeners>
</PHPunit>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)