簡體   English   中英

在 symfony 內使用 phpunit 進行測試服務

[英]Test Service with phpunit inside symfony

如何使用 symfony 測試帶有 PHPUnit 的服務? 到目前為止,我安裝並包含了測試包 DAMA Doctrine Bundle,並創建了測試數據庫。

Inside.env.test 我添加了數據庫連接

# define your env variables for the test env here
KERNEL_CLASS='App\Kernel'
APP_SECRET='$ecretf0rt3st'
SYMFONY_DEPRECATIONS_HELPER=999999
PANTHER_APP_ENV=panther
PANTHER_ERROR_SCREENSHOT_DIR=./var/error-screenshots
# .env.test.local
DATABASE_URL="mysql://root:root@db:3306/testdb?serverVersion=mariadb-10.4.11&charset=utf8mb4"

我在 phpunit.xml.dist 中包含了 DAMA Doctrine 捆綁包

<extensions>
    <extension class="DAMA\DoctrineTestBundle\PHPUnit\PHPUnitExtension"/>
</extensions>

現在,我要測試的是我的服務(例如 CartService、ProductService 等)

use App\Entity\Cart;
use App\Entity\CartItem;
use App\Entity\Product;
use App\Entity\User;
use App\Repository\CartItemRepository;
use App\Repository\CartRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\Security;

class CartService
{

    private CartRepository     $cartRepository;
    private ManagerRegistry    $managerRegistry;
    private CartItemRepository $cartItemRepository;
    private Security           $security;

    public function __construct(Security $security, CartItemRepository $cartItemRepository, CartRepository $cartRepository, ManagerRegistry $managerRegistry)
    {
        $this->cartItemRepository = $cartItemRepository;
        $this->cartRepository = $cartRepository;
        $this->managerRegistry = $managerRegistry;
        $this->security = $security;
    }

    /**
     * Get Cart by ID
     *
     * @return Cart|null
     */
    public function getCartByUserId(): ?Cart
    {
        $user = $this->security->getUser();
        return $this->cartRepository->findOneBy(['customer' => $user]);
    }

    /**
     * Show Cart and Total Price
     *
     * @return Cart|null
     */
    public function showCart(): ?Cart
    {
        $cart = $this->getCartByUserId();
        $this->calculateTotalPrice();
        return $cart;
    }

當我在 CartServiceTest 上運行 phpunit 測試時,我收到此錯誤:


1) App\Tests\CartServiceTest::testShowCart
Error: Typed property App\Tests\CartServiceTest::$cartService must not be accessed before initialization

/var/www/html/Tests/CartServiceTest.php:29


CartServiceTest 看起來像這樣

<?php

namespace App\Tests;

use App\Entity\Product;
use App\Service\CartService;
use Doctrine\ORM\EntityManager;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class CartServiceTest extends KernelTestCase
{
    /**
     * @var EntityManager
     */
    private EntityManager $entityManager;
    private CartService $cartService;

    public function setUp(): void
    {
        $kernel = self::bootKernel();
        $this->entityManager = $kernel->getContainer()
                                      ->get('doctrine')
                                      ->getManager();
    }

    public function testShowCart()
    {
        $user = 11;
        $cart = $this->cartService->getCartByUserId();
        dump($cart);


    }


    protected function tearDown(): void
    {
        $this->entityManager->close();
    }

}
 

錯誤:在初始化之前不得訪問類型屬性 App\Tests\CartServiceTest::$cartService

意味着您必須已經在您的應用程序中使用了 cartService。 例如,如果您已經注入此服務在您的 controller 之一中具有依賴項注入,那沒關系。

但你可以做得更好。 只需為您的測試“services_test.yaml”創建一個服務配置並將您的服務公開

就像是:

#servies_test.yaml
services:
    App\Service\CartService:
        public: true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM