• Home
  • blog
  • Upgrading Sylius the TDD Way: Exploring PhpSpec

Upgrading Sylius the TDD Way: Exploring PhpSpec

The post on developing and testing new Sylius features was an introduction to the three types of tests that are used in Sylius – PHPUnit, Phpspec and Behat.

In this part, we’ll extend some core classes to indicate color-coded inventory status. First, we’ll deal with the back end part. In a followup post, we’ll use Behat and test the visual changes. Please follow the instructions in the previous post to get a working instance up and running.

Sylius logo

Sylius has an excellent inventory management solution. However, there’s always some room for a tweak or two. If you look at the list of products (admin/products) there’s no information about available stock. Looking at the variants of a product, we see inventory data, whether it’s tracked or not, and the number of total items in stock, if tracked. It would be nice to see that kind of information on the product listing page, too. In addition, the stock level is all or nothing – for example, a green label says “10 Available on hand” or red “0 Available on hand”. How about something in-between, say a yellow label for “3 Available on hand” to indicate that stock is low? Then, the store admin can decide it’s time to replenish.

Extend ProductVariant and Product models

We want to extend the behavior of both the ProductVariant and Product models provided by Sylius, so we can see extra information on stock availability when viewing products.

Create a Bundle

First, we create the src/AppBundle/AppBundle.php file and register it in app/AppKernel.php.


// src/AppBundle/AppBundle.php

namespace AppBundle;

use SymfonyComponentHttpKernelBundleBundle;

class AppBundle extends Bundle

// app/AppKernel.php

public function registerBundles()
    $bundles = [
        // ...
        new AppBundleAppBundle(),

Next, we inform the autoloader about the new bundle by adding it to the “autoload” section of composer.json.

Continue reading %Upgrading Sylius the TDD Way: Exploring PhpSpec%