School of Prophets: Start Small, start with Pimple

Lately I decided to speck Pimple :). PHPSpec is a great tool, but everytime that I look more into it and the people who use it I am more convinced that it is like a school. First of all you learn BDD Specking type by doing, by a change of mindset, someone external apply it to you or you happen to mock it at least to be ready, or a combination. Yeah some abc of theology mixed into the explanation.

A good `prophet` knows how to describe objects in a way that the behavior is clean and directed, he has to convey the message and not babbling. He studies and reacts to what he sees and keeps conveying compulsorily the message/specification that the divine person behind him/her has revealed. The greater design of the divine person is also revealed to even the prophet himself/herself, some people called this emergent design, which I think is a discovery of what is there but is unseen up to the point of specking.

So I start looking at the PHPUnit tests of Pimple to do a regression. It is also good that even though I have looked at Pimple before I have never understood why some decisions were taken.

So my first describing of things was that the class was of the Pimple type:

    function it_is_initializable()
    {
        $this->shouldHaveType('Pimple');
    }

Then the next test on the suite was that Pimple can store strings:

    function it_stores_strings()
    {
        $this['param'] = 'value';
        $this['param']->shouldReturn('value');
    }

It was until I went to the this test that PHPSpec told me I had to implement ArrayAccess itself. I then wrote:

    function it_is_initializable()
    {
        $this->shouldHaveType('Pimple');
        $this->shouldImplement('ArrayAccess');
    }

That nailed it. Then the next test was:

    function it_stores_closures()
    {
        $this['callback'] = function() { return 5; };
 
        $this['callback']->shouldBeAClosure(function() {return 5;});
    }

Yep, you can also store closures. All tests were green up to this point. I had to discover and do a custom matcher for the above thing to work:

public function getMatchers()
    {
        return [
            'beAClosure' => function($subject, $closure) {
                $rf = new ReflectionFunction($closure);
                $result = call_user_func($closure) === call_user_func($subject);
                return $rf->isClosure() && $result;
            }
        ];
    }

This basically hacky way ensures it is the closure that gets in that gets out.

Then I ran into Pimple’s probably most neat feature looking forward to automating Dependency Injection in an ArrayAccess object.

    function it_passes_itself_when_stored_value_called_takes_an_argument()
    {
        $this['container'] = function($container) { return $container; };
 
        $this['container']->shouldReturnWhenInvokedWith(array($this, $this));
    }

And I had to add the matcher:

            'returnWhenInvokedWith' => function($subject, array $resultAndArgument) {
                return $resultAndArgument[0] === call_user_func($subject, $resultAndArgument[1]);
            }

All was green and great until I realize I had not written any code on Pimple, yet I knew something was wrong because this should be a feature.

I understood that Pimple’s behavior when an object closure is injected/stored when invoked it has to take Pimple as an argument. That logic was not happening.

And for tonight it is here where it ends, because I have to ask the more learned guys, even the creator @everzet, and @md, or @jakzalas to see how to approach this scenario.

Notice the next tests are easy and hard. So it would be interesting how to resolve this problem.

Until next time, I enjoyed tonight. Lord willing there will be more PHPSpec in Warsaw SymfonyCon.

Thankful for my Saviour’s totally undeserved favor and grace.

Selection_556

He he sorry, only valid part of the graphic ends before Scripture\Memory. 🙂 I wrote it on top of another project I was thinking about.

Leave a Reply

Your email address will not be published. Required fields are marked *