A
A
Alexander Sergin2015-07-04 08:37:09
PHP
Alexander Sergin, 2015-07-04 08:37:09

How to correctly describe the specification (phpspec)?

Good afternoon! I'm trying to get into testing, namely SpecBDD (PHPSpec). There was a small question. There is a run()
method (it is clear that there should not be a code specification before writing the code specification, it is just for an example):

public function run()
{
    /** @var EventDispatcherInterface $eventDispatcher */
    $eventDispatcher = $this->container
        ->get('event_dispatcher');
    $eventDispatcher->dispatch(static::EVENT_RUN);
    if ($this->request) {
        $request = $this->request;
    } else {
        $request = Request::createFromGlobals();
    }
    $requestEvent = new RequestEvent($request);
    $eventDispatcher->dispatch(static::EVENT_REQUEST, $requestEvent);
    if (!$requestEvent->getResponse()) {
        throw new NoResponseException();
    }
    $responseEvent = new ResponseEvent($requestEvent->getRequest(), $requestEvent->getResponse());
    $eventDispatcher->dispatch(static::EVENT_RESPONSE, $responseEvent);
    $responseEvent->getResponse()
        ->send();
}

The question is: how do I correctly lock $requestEvent and $responseEvent , in order to check calls to $eventDispatcher->dispatch(...) and replace the return value in $requestEvent->getResponse() ?
I see the following options:
  1. create events through some EventBuilder by injecting it into the described class;
  2. public methods createRequestEvent(...) and createResponseEvent(...) on the described class.

The first option seems too cumbersome, the second - some kind of crutch. What other options are there?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-07-05
@alex_sergin

The whole relish of PhpSpec is manifested just in such cases. Do you find it difficult to get wet? Is it difficult to write tests? So something went wrong with your code (PhpSpec is very restrictive in how and what you can test).
1) why are you injecting the entire container? Inject - then mock it and in the get method return a mock for the event dispatcher
2) everything that is created inside the method is not mocked. That is, since you are doing new in the method, then it is necessary. You won't do anything here. And since we have a mock event dispatcher, you can check what exactly you are sending there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question