>本文展示了使用測試驅動的開發(TDD)擴展Sylius的核心功能,以改善庫存管理。 我們將在產品列表中添加顏色編碼的低儲物指標。 這將是後端實施;將來的文章將介紹使用Behat的視覺測試。 假設您有一個工作的sylius實例。
Sylius提供了強大的庫存管理,但我們可以增強它。目前,管理員產品列表缺乏庫存信息。雖然變體細節顯示出庫存水平和跟踪,但將其添加到產品列表中會提高可用性。 我們還會引入一個分層的警告系統(例如,綠色含量充足,黃色,低庫存,紅色庫存)。 擴展
和ProductVariant
Product
>
和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
:
$reorderLevel
ProductVariant.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 --force
ProductVariant.orm.yml
5。更多SPECBDD測試reorderLevel
Product.orm.yml
>編寫phpspec測試ProductVariant
>和Product
,實現getReorderLevel()
>,setReorderLevel()
,isReorderable()
,getOnHand()
,isTracked()
和
此TDD方法可確保強大的代碼。 下一篇文章將涵蓋視覺驗證的Behat測試。
(為簡潔而省略了FAQ部分,因為它在很大程度上與核心代碼示例無關,並且會大大增加響應長度。提供的常見問題解答是寫得很好的,可以很容易地分別包含。 >
以上是升級Sylius TDD方式:探索PhpsPec的詳細內容。更多資訊請關注PHP中文網其他相關文章!