首頁 > 後端開發 > php教程 > 升級Sylius的TDD方式:探索Behat

升級Sylius的TDD方式:探索Behat

Joseph Gordon-Levitt
發布: 2025-02-10 09:21:09
原創
553 人瀏覽過

與Storybdd擴展Sylius:增強產品庫存管理>本文詳細介紹了Sylius添加庫存跟踪,重點介紹了Storybdd測試。 我們以前添加了後端功能;現在,我們將它們集成到UI中。

>

Upgrading Sylius the TDD Way: Exploring Behat

我們的目標:在產品列表中顯示一個新的“庫存”列,顯示了所有跟踪變體的總庫存。

Upgrading Sylius the TDD Way: Exploring Behat

密鑰概念:

  • Storybdd(行為驅動的開發):使用Behat來測試用戶故事,確保准確地實現庫存管理等功能。 >
  • 自定義behat上下文:在Sylius中創建和配置自定義上下文類,以處理特定的測試方案。
  • >數據庫集成:地址數據庫架構更新和潛在連接錯誤。
  • UI更新:修改網格和模板以在Sylius Admin面板中正確顯示庫存數據。
使用behat實施Storybdd測試

我們創建一個behat功能文件():>

運行此操作會揭示缺失的步驟。 我們創建一個自定義上下文(features/product/managing_products/browsing_products_with_inventory.feature):

>
@managing_inventory
Feature: Browsing products with inventory
    In order to manage my shop merchandise
    As an Administrator
    I want to be able to browse products

    Background:
        Given the store operates on a single channel in "United States"
        And the store has a product "Kubus"
        And it comes in the following variations:
            | name          | price     |
            | Kubus Banana  | .00     |
            | Kubus Carrot  | .00     |
        And there are 3 units of "Kubus Banana" variant of product "Kubus" available in the inventory
        And there are 5 units of "Kubus Carrot" variant of product "Kubus" available in the inventory
        And I am logged in as an administrator

    @ui
    Scenario: Browsing defined products with inventory
        Given the "Kubus Banana" product variant is tracked by the inventory
        And the "Kubus Carrot" product variant is tracked by the inventory
        When I want to browse products
        Then I should see that the product "Kubus" has 8 on hand quantity
登入後複製

我們將此上下文配置在src/Sylius/Behat/Context/Ui/Admin/ManagingProductsInventoryContext.php

中。 數據庫設置(
<?php
// ... (imports) ...

class ManagingProductsInventoryContext implements Context
{
    // ... (IndexPageInterface injection and methods) ...
}
登入後複製

src/Sylius/Behat/Resources/config/services/contexts/ui.xml)是必要的。 src/Sylius/Behat/Resources/config/suites/ui/inventory/managing_inventory.yml> doctrine:database:create實施丟失的步驟涉及使用Sylius的doctrine:schema:create與產品列表進行交互,並斷言以驗證庫存列的存在和數據。 我們還需要更新sylius網格配置(doctrine:schema:update)以添加“庫存”列並創建一個自定義模板(

)以用顏色編碼的指示器顯示庫存信息(綠色,綠色,足夠的庫存,黃色,低適用於低點庫存,無庫存)。

>最後,我們擴展了產品變體形式以允許修改重新排序級別。這涉及創建一個表單擴展名(src/AppBundle/Form/Type/Extension/ProductVariantTypeExtension.php),將其配置為服務(src/AppBundle/Resources/config/services.yml),並更新相關模板(app/Resources/SyliusAdminBundle/views/ProductVariant/Tab/_details.html.twig)。

Upgrading Sylius the TDD Way: Exploring Behat

結論: >這個過程展示了一種強大的方法,可以在保持高測試覆蓋範圍的同時擴展Sylius。 StoryBDD和自定義Behat上下文的使用可確保對新功能進行徹底測試並無縫集成到現有平台中。 請記住要在更改配置文件後清除緩存。

(因簡潔而省略了常見問題部分,因為它與所述的核心代碼和修改無關。

以上是升級Sylius的TDD方式:探索Behat的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板