<commit_before><?php
namespace spec\watoki\deli;

use spec\watoki\deli\fixtures\RequestFixture;
use spec\watoki\deli\fixtures\TestDelivery;
use watoki\deli\Request;
use watoki\deli\router\DynamicRouter;
use watoki\deli\Router;
use watoki\deli\target\CallbackTarget;
use watoki\deli\target\RespondingTarget;
use watoki\scrut\Specification;

/**
 * This describes the basic set-up which gets the Response from a Target, found using a
 * Router which uses a TargetFactory to create the Target. There are different kind of
 * Targets with different sources for the Response (e.g. closure, objects implementing the
 * Responding interface or plain objects).
 *
 * @property RequestFixture request <-
*/
class DeliverRequestAndResponseTest extends Specification {

    /**
     * The CallbackTarget requires no infrastructure since it simply calls the callable it's given.
     */
    function testCallbackTarget() {
        $this->router->set('path/to/target', CallbackTarget::factory(function (Request $r) {
            return 'Hello ' . $r->getArguments()->get('name');
        }));
        $this->request->givenTheRequestHasTheTarget('path/to/target');
        $this->request->givenTheRequestHasTheArgument_WithTheValue('name', 'Homer');

        $this->whenIRunTheDelivery();
        $this->thenTheResponseShouldBe('Hello Homer');
    }

    /**
     * A class that implements the Responding interface is handled by the RespondingTarget.
     */
    function testRespondingTarget() {
        $className = 'TestResponding';
        eval('class ' . $className . ' implements \\watoki\\deli\\Responding {
            public function respond(\\watoki\\deli\\Request $request) {
                return "Hello " . $request->getArguments()->get("name");
            }
        }');
        $this->router->set('path/to/responding', RespondingTarget::factory($this->factory, new $className()));

        $this->request->givenTheRequestHasTheTarget('path/to/responding');
        $this->request->givenTheRequestHasTheArgument_WithTheValue('name', 'Bart');

        $this->whenIRunTheDelivery();
        $this->thenTheResponseShouldBe('Hello Bart');
    }

    ######################### SET-UP #########################

    /** @var DynamicRouter */
    private $router;

    /** @var TestDelivery */
    private $delivery;

    protected function setUp() {
        parent::setUp();
        $this->router = new DynamicRouter();
    }

    public function whenIRunTheDelivery() {
        $this->delivery = new TestDelivery($this->router, $this->request->request);
        $this->delivery->run();
    }

    public function thenTheResponseShouldBe($string) {
        $this->assertEquals($string, $this->delivery->response);
    }

} <commit_msg>Moved code from scenarios to steps<commit_after><?php
namespace spec\watoki\deli;

use spec\watoki\deli\fixtures\RequestFixture;
use spec\watoki\deli\fixtures\TestDelivery;
use watoki\deli\Request;
use watoki\deli\router\DynamicRouter;
use watoki\deli\Router;
use watoki\deli\target\CallbackTarget;
use watoki\deli\target\RespondingTarget;
use watoki\scrut\Specification;

/**
 * This describes the basic set-up which gets the Response from a Target, found using a
 * Router which uses a TargetFactory to create the Target. There are different kind of
 * Targets with different sources for the Response (e.g. closure, objects implementing the
 * Responding interface or plain objects).
 *
 * @property RequestFixture request <-
*/
class DeliverRequestAndResponseTest extends Specification {

    /**
     * The CallbackTarget requires no infrastructure since it simply calls the callable it's given.
     */
    function testCallbackTarget() {
        $this->given_IsRoutedToTheCallback('path/to/target', function (Request $r) {
            return 'Hello ' . $r->getArguments()->get('name');
        });
        $this->request->givenTheRequestHasTheTarget('path/to/target');
        $this->request->givenTheRequestHasTheArgument_WithTheValue('name', 'Homer');

        $this->whenIRunTheDelivery();
        $this->thenTheResponseShouldBe('Hello Homer');
    }

    /**
     * A class that implements the Responding interface is handled by the RespondingTarget.
     */
    function testRespondingTarget() {
        $this->given_IsRoutedToARespondingClass_ThatRespondsWith('path/to/responding', 'TestResponding',
            'return "Hello " . $request->getArguments()->get("name");');
                $this->request->givenTheRequestHasTheTarget('path/to/responding');
        $this->request->givenTheRequestHasTheArgument_WithTheValue('name', 'Bart');

        $this->whenIRunTheDelivery();
        $this->thenTheResponseShouldBe('Hello Bart');
    }

    ######################### SET-UP #########################

    /** @var DynamicRouter */
    private $router;

    /** @var TestDelivery */
    private $delivery;

    protected function setUp() {
        parent::setUp();
        $this->router = new DynamicRouter();
    }

    private function given_IsRoutedToTheCallback($path, $callback) {
        $this->router->set($path, CallbackTarget::factory($callback));
    }

    private function given_IsRoutedToARespondingClass_ThatRespondsWith($path, $className, $methodBody) {
        eval('class ' . $className . ' implements \\watoki\\deli\\Responding {
            public function respond(\\watoki\\deli\\Request $request) {
                ' . $methodBody . '
            }
        }');
        $this->router->set($path, RespondingTarget::factory($this->factory, new $className()));
    }

    public function whenIRunTheDelivery() {
        $this->delivery = new TestDelivery($this->router, $this->request->request);
        $this->delivery->run();
    }

    public function thenTheResponseShouldBe($string) {
        $this->assertEquals($string, $this->delivery->response);
    }

} 
{'fraction_non_alphanumeric': 0.1451862021623477, 'fraction_numerical': 0.0, 'mean_word_length': 3.38855421686747, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4037243', 'n_tokens_mistral': 1682, 'n_tokens_neox': 1600, 'n_words': 349}