Support the ongoing development of Laravel.io →
Testing

I've been getting more and more into PHPSpec over the last several weeks and have definitely enjoyed it and learned a lot. However I have found the docs a bit lacking and am trying to get together some good examples/resources to share with others that are interested in getting started with PHPSpec, BDD/TDD, and even just testing in general.

I've been slowly compiling a list of useful links/examples here: Mostly PHPSpec related links

One of the better examples/explanations I've come across is at the Sylius Contribution Docs

<?php

namespace spec\Sylius\Bundle\CoreBundle\Pricing;

use Acme\ApiClient;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Bundle\CoreBundle\Model\ProductInterface;

class PriceUpdaterSpec extends ObjectBehavior
{
    function let(ApiClient $api)
    {
        $this->beConstructedWith($api);
    }

    function it_updates_product_price_through_api($api, ProductInterface $product)
    {
        $product->getSku()->shouldBeCalled()->willReturn('TES-12-A-1090');
        $api->getCurrentProductPrice('TES-12-A-1090')->shouldBeCalled()->willReturn(1545);
        $product->setPrice(1545)->shouldBeCalled();

        $this->updatePrice($product);
    }
}

// The example looks clear and simple, the PriceUpdater service should obtain the SKU of the product, call the external API and update products price accordingly.

If anybody has any solid resources they can recommend, please share below!

It seems to be the recurring theme of most PHPSpecs that I come across. People having a hard time finding examples and getting beyond the initial entry barrier getting a handle on the different types of matching, using Prophecy objects, and even initial setup/config.

Totally open for feedback/discussion.

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.