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

升級Sylius TDD方式:探索PhpsPec

Christopher Nolan
Christopher Nolan原創
2025-02-10 11:13:13565瀏覽

>本文展示了使用測試驅動的開發(TDD)擴展Sylius的核心功能,以改善庫存管理。 我們將在產品列表中添加顏色編碼的低儲物指標。 這將是後端實施;將來的文章將介紹使用Behat的視覺測試。 假設您有一個工作的sylius實例。

Upgrading Sylius the TDD Way: Exploring PhpSpec

Sylius提供了強大的庫存管理,但我們可以增強它。目前,管理員產品列表缺乏庫存信息。雖然變體細節顯示出庫存水平和跟踪,但將其添加到產品列表中會提高可用性。 我們還會引入一個分層的警告系統(例如,綠色含量充足,黃色,低庫存,紅色庫存)。

擴展

ProductVariant Product>

要添加庫存可用性信息,我們將擴展Sylius的

ProductVariant型號。 Product

1。創建一個捆綁

創建

src/AppBundle/AppBundle.php

<code class="language-php"><?php
namespace AppBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class AppBundle extends Bundle
{
}</code>

中註冊它:app/AppKernel.php>

<code class="language-php"><?php
// ...
public function registerBundles()
{
    $bundles = [
        // ...
        new AppBundle\AppBundle(),
    ];
}</code>
update

's composer.json節:autoload

<code class="language-json">{
  // ...
  "autoload": {
    "psr-4": {
      // ...
      "AppBundle\": "src/AppBundle"
    }
  }
  // ...
}</code>
>運行

composer dump-autoload

2。 SPECBDD測試

>我們將使用phpspec進行行為驅動的發展。 在

中,添加:phpspec.yml.dist

<code class="language-yaml">AppBundle: { namespace: AppBundle\Entity, psr4_prefix: AppBundle\Entity, spec_path: src/AppBundle/Entity, src_path: src/AppBundle/Entity }</code>
清除緩存:

php bin/console cache:clear

使用以下方式生成規範

創建接口:
<code class="language-bash">php bin/phpspec desc AppBundle/Entity/ProductVariant
php bin/phpspec desc AppBundle/Entity/Product</code>

擴展其Sylius對應物。然後創建擴展Sylius類並實現新接口的創建ProductInterface>> ProductVariantInterface>Product.php屬性ProductVariant.php

$reorderLevelProductVariant.php 3。覆蓋Sylius類

<code class="language-php"><?php
// src/AppBundle/Entity/ProductVariant.php
// ...
class ProductVariant extends BaseProductVariant implements ProductVariantInterface
{
    const REORDER_LEVEL = 5;
    private $reorderLevel;
    // ...
}</code>

配置sylius以在>中使用我們的擴展類

4。數據庫更新app/config/config.yml

<code class="language-yaml">sylius_product:
    resources:
        product:
            classes:
                model: AppBundle\Entity\Product
        product_variant:
            classes:
                model: AppBundle\Entity\ProductVariant</code>
生成和運行遷移:

(或者必要時使用

)。 創建

以定義數據庫中的

列。創建一個空的
<code class="language-bash">php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate</code>
,因為我們沒有修改

表。 php bin/console doctrine:schema:update --forceProductVariant.orm.yml5。更多SPECBDD測試reorderLevelProduct.orm.yml

>編寫phpspec測試ProductVariant>和Product,實現getReorderLevel()>,setReorderLevel()isReorderable()getOnHand()isTracked()的方法,並根據需要確保所有測試通過。 測試應涵蓋各種情況,包括不同的庫存水平和重新排序水平。 請記住使用必要的方法更新接口。

6。結論

此TDD方法可確保強大的代碼。 下一篇文章將涵蓋視覺驗證的Behat測試。

Upgrading Sylius the TDD Way: Exploring PhpSpec

Upgrading Sylius the TDD Way: Exploring PhpSpec

(為簡潔而省略了FAQ部分,因為它在很大程度上與核心代碼示例無關,並且會大大增加響應長度。提供的常見問題解答是寫得很好的,可以很容易地分別包含。 >

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

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn