>本文展示了使用测试驱动的开发(TDD)扩展Sylius的核心功能,以改善库存管理。 我们将在产品列表中添加颜色编码的低储物指标。 这将是后端实施;将来的文章将介绍使用Behat的视觉测试。 假设您有一个工作的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中文网其他相关文章!