首页 >后端开发 >php教程 >升级Sylius TDD方式:探索PhpsPec

升级Sylius TDD方式:探索PhpsPec

Christopher Nolan
Christopher Nolan原创
2025-02-10 11:13:13588浏览

>本文展示了使用测试驱动的开发(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