SitePoint PHPUpgrading Sylius the TDD Way: Exploring Behat (29.4.2017, 16:00 UTC)

Last time, we developed some new features on top of Sylius' core to indicate which products and their variants are low on stock and need replenishing. Now, we move on to seeing our changes in the UI, which means we will need to do a bit of StoryBDD testing.

When browsing the list of products, we want to see a new column called Inventory which will hold a sum of all available tracked variants' stock amounts.

Sylius logo

Writing StoryBDD tests

Behat is the tool we want to use here. After making sure Behat is working well by running any feature from the Sylius package, we create a new features/product/managing_products/browsing_products_with_inventory.feature file with the following definition:

Feature: Browsing products with inventory
    In order to manage my shop merchandise
    As an Administrator
    I want to be able to browse products

        Given the store operates on a single channel in "United States"
        And the store has a product "Kubus"
        And it comes in the following variations:
            | name          | price     |
            | Kubus Banana  | $2.00     |
            | Kubus Carrot  | $2.00     |
        And there are 3 units of "Kubus Banana" variant of product "Kubus" available in the inventory
        And there are 5 units of "Kubus Carrot" variant of product "Kubus" available in the inventory
        And I am logged in as an administrator

    Scenario: Browsing defined products with inventory
        Given the "Kubus Banana" product variant is tracked by the inventory
        And the "Kubus Carrot" product variant is tracked by the inventory
        When I want to browse products
        Then I should see that the product "Kubus" has 8 on hand quantity

Again, we describe the make-up of our test product, stating the name, price and available stock of the two variants. If we run this feature, we see a list of paths for available contexts. We aren't interested in any of them, but we also get a hint, so we select None.

php bin/behat features/product/managing_products/browsing_products_with_inventory.feature

--- Use --snippets-for CLI option to generate snippets for following ui_managing_inventory suite steps:

    When I want to browse products
    Then I should see that the product "Kubus" has 18 on hand quantity

We create our context in src/Sylius/Behat/Context/Ui/Admin/ManagingProductsInventoryContext.php and add this:


// src/Sylius/Behat/Context/Ui/Admin/ManagingProductsInventoryContext.php

namespace Sylius\Behat\Context\Ui\Admin;

use Behat\Behat\Context\Context;

class ManagingProductsInventoryContext implements Context

Running the feature again doesn't seem to help as we get the same list of contexts as before. That's because Sylius doesn't know anything about our class. We need to configure a service for our context together with what Sylius has in src/Sylius/Behat/Resources/config/services/contexts/ui.xml. We now search for managing_products and add this below it:

<service id="sylius.behat.context.ui.admin.managing_products_inventory" class="Sylius\Behat\Context\Ui\Admin\ManagingProductsInventoryContext">
    <argument type="service" id="" />
    <tag name="fob.context_service" />

Let's add our sylius.behat.context.ui.admin.managing_products_inventory service (that's the id in ui.xml) to the context services for ui_managing_inventory suites in src/Sylius/Behat/Resources/config/suites/ui/inventory/managing_inventory.yml.

We may need to clear the cache. If we run the feature, we now get an option to select Sylius\Behat\Context\Ui\Admin\ManagingProductsInventoryContext. We then get:

--- Sylius\Behat\Context\Ui\Admin\ManagingProductsInventoryContext has missing steps. Define them with these snippets:

     * @When I want to browse products
    public function iWantToBrowseProducts()
        throw new PendingException();

     * @Then I should see that the product :arg1 has :arg2 on hand quantity
    public function iShouldSee

Truncated by Planet PHP, read more at the original (another 892 bytes)

SitePoint PHPUpgrading Sylius the TDD Way: Exploring PhpSpec (28.4.2017, 16:00 UTC)

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 Symfony\Component\HttpKernel\Bundle\Bundle;

class AppBundle extends Bundle


// app/AppKernel.php

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

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%

SitePoint PHPFunctional Programming with Phunkie: Parser Combinators in PHP (27.4.2017, 16:00 UTC)

Phunkie is a library with functional structures for PHP. In this tutorial, Phunkie creator Marcello Duarte, head of training at Inviqa, explains how to create Parser combinators using the functional library. This post first appeared on the Inviqa blog, and was republished here with their permission.

Phunkie logo

Learning functional programming

Functional programming really matters. Pure functions (or functions with no side effect) are the perfect units of behaviour because we can rely on them to build larger and more complex systems. The greatness of functional programming relies on this power of composability.

That’s something I first came to believe back in the days of my licentiate degree on Computing for Management, during a semester in AI with Lisp. My course curriculum was mostly focused on C/C++, therefore I had to stay focused on where the skills demand was.

Thankfully, I’ve been able to reignite my love of studying functional programming here at Inviqa where we’re delivering more and more projects based on Scala, the general purpose programming language.

I’ve read "the red book" about three times (the Chiusano & Bjarnason one, not the Vaughn Vernon’s one). I took all the Martin Odersky’s Coursera courses, and I spent hours of my weekends watching videos and reading papers.

And I set myself a challenge: to do with functional programming what I did when I was learning TDD and created PHPSpec (a BDD testing and design tool for PHP developers) to help me learn; I decided to write my own functional programming dialect in PHP. And so Phunkie was born!

Now over to you. Learning functional programming means immersing yourself in a new paradigm, which can be challenging. It requires a totally different mindset for approaching problems.

So, by the time you can use functional programming to solve real-world problems, you’ll have spent hours grasping the new thinking or getting clued-up on the theory.

In this tutorial, my aim is to help fast-track your journey by going over my implementation of Hutton & Meijer’s Monadic Parser Combinators. Hopefully you will be able to see both the thinking and the usefulness of functional programming.

And, by using Phunkie, you can eliminate the need to learn Haskell to understand the topic. All you should need is some familiarity with PHP.

So let’s get started!

Continue reading %Functional Programming with Phunkie: Parser Combinators in PHP%

Simon HolywellPHP and immutability: objects and generalisation - part three (27.4.2017, 03:01 UTC)

In the last article we learnt how to create modified copies of an immutable in PHP. This one is going to tackle an issue I have hitherto skirted around and avoided. Objects in immutable data structures.

This article is part of a series I have written on the topic of immutability in PHP code:

  1. Part two - improve the process of creating modified copies of the immutable
  2. Part three - objects in immutable data structures and a generalised immutable implementation

What’s the problem with objects?

Objects or instances of classes are passed by reference in PHP. Any changes to the class will be reflected in all places it is passed to. This is different to scalar values like strings, that are passed by value instead.

$class = new stdClass();
function addItem($x, $item) {
    $x->$item = $item;
var_dump($class); // object(stdClass)#1 (0) {}
addItem($class, 'test');
object(stdClass)#1 (1) {
  ["test"]=> string(4) "test"

Here you can see a function called addItem() that adds a property to stdClass instance - this produces a side effect. The original $class is also updated as it references the same value so if we dump the variable we can see it’s value has changed.

Now consider the same example with a simple scalar string where pass by value takes effect.

$string = 'begin';
function addItem($x, $item) {
    $x .= $item;
var_dump($string); // string(5) "begin"
addItem($string, 'end');
var_dump($string); // string(5) "begin"

Here the original value remains intact because, unlike an object, there is no reference to it from within the addItem() function.

These side effects make putting an object into an immutable data structure difficult. Someone with access to the reference could simply change the object after the fact - thus breaking immutability.

What about resources?

Turns out the same issues plague resources as well. They are just references to a resource ID so any change to one will affect all those that also reference it. Simply moving the pointer in a file resource would break immutability.

Truncated by Planet PHP, read more at the original (another 75431 bytes)

Nomad PHPAtlas: A Data Mapper For Your Persistence Model (26.4.2017, 10:00 UTC)

Speaker: Paul M. Jones @pmjones Atlas lets you build an OO model of your SQL tables and relationships. You can use it at the start of your project for basic CRUD operations. As you begin to need simple behaviors in your application, you can add them to the Record and RecordSet persistence model objects. Finally, …

The post Atlas: A Data Mapper For Your Persistence Model appeared first on Nomad PHP.

Paul M. Jones“A False Sense of Simplicity” (25.4.2017, 13:51 UTC)

These year-old posts from Piotr Solnica are about Ruby On Rails …

… but the experiences related therein should be valuable to anyone using or building a full-stack PHP framework. (I can imagine it applying to CMSes as well.)

Does this story from Piotr remind you of any framework-based project you’ve worked on in PHP?

Once, I joined an existing project. It was a huuuuge app which was running an on-line shopping community website. Complicated sales model, complicated promotions, complicated product setups, coupons, user groups, messages – it had it all. I joined them to help ship a few new features. One of my early tasks was to … add a link to something on some page. It took me few days to add this stupid link. Why? The app was a big ball of complex domain logic scattered across multiple layers with view templates so complicated, it wasn’t even simple to find the right template where the link was supposed to be added. Since I needed some data in order to create that link, it wasn’t obvious how I should get it. There was a lack of internal application APIs and relying on ActiveRecord exclusively made it extremely difficult.

I’ve consulted on projects like that more than once. Indeed, the posts might well have been subtitled “The Perils of Convenience-Oriented Development”:

People are attracted by Rails because it gives you a false sense of simplicity, whereas what really happens is that complexity is being hidden by convenient interfaces.

Read both of the posts, and see if you can relate. They just reinforce to me that this is what to expect when you embed your domain logic in your user interface logic.

Remember: with server-side web-based applications, the user interface is the HTTP request and response. Any code that reads from the request, or that writes to the response, is part of the user interface. Putting your domain logic in the user interface is convenient to start with, but as soon as things become even a little bit complex, that “convenience” becomes a burden.

PHP ClassesPHP 7.2 Release Date and Managers Being Chosen - 7 Minutes Lately in PHP podcast episode 82 (25.4.2017, 11:40 UTC)
By Manuel Lemos
PHP 7.2 development is reaching to the alpha stage in June, hopefully to have a final version released later this year. So for now the release managers are being chosen, so they can start preparing to work on each alpha, beta and release candidate version.

This was one of the main topics discussed by Manuel Lemos and Arturs Sosins on the episode 82 of the Lately in PHP podcast.

In this episode they also talked about other proposals for PHP cache keys for stream wrappers, serialized object validation with is_string, type variants, let range() return a generator, named parameters again, and removing the need for ; on the end of the line .

They also commented on an article about promoting Open Source projects using data mining and business intelligence to boost SEO factors, and using OpenID Connect protocol to implement single sign-on social login systems.

This article also contains a podcast summary as a text transcript and a 5 minute video of the summary.

Listen to the podcast, or watch the hangout video, or read the transcript text to learn more about these interesting PHP topics.
Voices of the ElePHPantInterview with Adam Englander (25.4.2017, 09:00 UTC) Link
Nomad PHPWhat I Learned About Testing WhileWalking Uphill Both Ways In The Snow (25.4.2017, 00:33 UTC)

July US Presented by Chris Hartjes (@grmpyprogrammer) Date: July 20, 2017 Time: 20:00 CDT 18:00 PDT, 3:00 CEST (July 21), 2:00 BST (July 21) Not sure of the time in your area? Check it on Back when I was learning about how to test PHP code, I had to walk both ways uphill in …

The post What I Learned About Testing While
Walking Uphill Both Ways In The Snow
appeared first on Nomad PHP.

SitePoint PHPHow Privileged Are Programmers? Are You a John, Too? (24.4.2017, 17:00 UTC)

John was a developer. To be specific, he was a young, white, straight, young, self-taught developer. He wasn't rare, but he was special. John grew up with a couple parents, who paid for everything he needed. John regularly filled his belly, with the finest food his family could provide. John got every toy he asked for, once he learn that asking for 3 toys was a good way to get at least 1 toy.

A spoiled child with many toys

John got average grades, but it was ok because [according to mum]; "he's just bored of schooling, and too clever". He walked right out of high-school and into a programming job. The pay wasn't great; only enough for a small apartment and modest groceries [for one]. In time he'd earn more.

Over the years, John quickly got bored of programming. He loved the thought of the career, but it was all so boring. He moved jobs every year or so, and only then when his idiot bosses stopped seeing how much he mattered to their company.

Person leaving a job happily

It was just as well, because most of the other developers he worked with were idiots too. Did they even know how to program? All they wanted to do was talk and ask questions and they weren't as interested in John's work as intelligent people should be. He did once work with a girl developer, though. She was so pretty for a programmer. I mean, if you can call CSS and HTML programming.

Illustration of a female web designer

I am angry.

For the longest time, I was John. I thought every boring task beneath me, every other developer mediocre at best. I was my own hero, and my mom was right (albeit annoying) that I was brilliant. If only those around me could see this.

Continue reading %How Privileged Are Programmers? Are You a John, Too?%

LinksRSS 0.92   RDF 1.
Atom Feed   100% Popoon
PHP5 powered   PEAR
ButtonsPlanet PHP   Planet PHP
Planet PHP