<commit_before><?php
namespace Pupcake\Tests;

use Pupcake;

class CustomEventRegisterTest extends Pupcake\TestCase
{
    public function testRouteActionAndRouteConstraint()
    {
        $expectations = array();
        $expectations[] = array('input' => '192.168.2.1', 'output' => 'api#iphello world');
        $expectations[] = array('input' => '10.0.0.1', 'output' => 'api#iphello world');
        $expectations[] = array('input' => 'random', 'output' => 'Invalid Request');

        foreach($expectations as $expectation){
            $this->simulateRequest("get", "/api/ip/".$expectation['input']);

            $app = new Pupcake\Pupcake();
            
            $services = array();
            $services['express'] = $app->getService("Pupcake\Service\Express"); //load Express service
            $services['constraint'] = $app->getService("Pupcake\Service\RouteConstraint"); //load RouteConstraint service
            $services['action'] = $app->getService("Pupcake\Service\RouteAction"); //load RouteAction service

            // custom event registration
            $app->on("system.routing.route.create", function($event) use ($services) {
                return $event->register(
                    $services['express'],
                    $services['constraint'],
                    $services['action'],
                    function($event){
                        $event->props('route')->method('hello', function($word){
                            return "hello $word";
                        }); 
                    }
                )->start();
            });

            $app->get("api/ip/:ip", function($req, $res) use ($app) {
                $route = $app->getRouter()->getMatchedRoute();
                $action = $route->getAction();
                $hello = $route->hello('world');
                $res->send($action.$hello);
            })
            ->to("api#ip")
            ->constraint(array(
                'ip' =>  function($value){
                    return \Respect\Validation\Validator::ip()->validate($value);
                }
            ));

            $app->run();

            $this->assertEquals($this->getRequestOutput(), $expectation['output']);
        }

    }
}<commit_msg>add test case for building service event handlers on top of existing ones<commit_after><?php
namespace Pupcake\Tests;

use Pupcake;

class CustomEventRegisterTest extends Pupcake\TestCase
{
    public function testRouteActionAndRouteConstraint()
    {
        $expectations = array();
        $expectations[] = array('input' => '192.168.2.1', 'output' => 'api#iphello world');
        $expectations[] = array('input' => '10.0.0.1', 'output' => 'api#iphello world');
        $expectations[] = array('input' => 'random', 'output' => 'Invalid Request');

        foreach($expectations as $expectation){
            $this->simulateRequest("get", "/api/ip/".$expectation['input']);

            $app = new Pupcake\Pupcake();
            
            $services = array();
            $services['express'] = $app->getService("Pupcake\Service\Express"); //load Express service
            $services['constraint'] = $app->getService("Pupcake\Service\RouteConstraint"); //load RouteConstraint service
            $services['action'] = $app->getService("Pupcake\Service\RouteAction"); //load RouteAction service

            // custom event registration
            $app->on("system.routing.route.create", function($event) use ($services) {
                return $event->register(
                    $services['express'],
                    $services['constraint'],
                    $services['action'],
                    function($event){
                        $event->props('route')->method('hello', function($word){
                            return "hello $word";
                        }); 
                    }
                )->start();
            });

            $app->get("api/ip/:ip", function($req, $res) use ($app) {
                $route = $app->getRouter()->getMatchedRoute();
                $action = $route->getAction();
                $hello = $route->hello('world');
                $res->send($action.$hello);
            })
            ->to("api#ip")
            ->constraint(array(
                'ip' =>  function($value){
                    return \Respect\Validation\Validator::ip()->validate($value);
                }
            ));

            $app->run();

            $this->assertEquals($this->getRequestOutput(), $expectation['output']);
        }

    }

    public function testCustomServiceCallbackOnTopOfExistingOnes()
    {
        $expectations = array();
        $expectations[] = array('input' => '192.168.2.1', 'output' => 'api#iphello world');
        $expectations[] = array('input' => '10.0.0.1', 'output' => 'api#iphello world');
        $expectations[] = array('input' => 'random', 'output' => 'Invalid Request');

        foreach($expectations as $expectation){
            $this->simulateRequest("get", "/api/ip/".$expectation['input']);

            $app = new Pupcake\Pupcake();
            
            $services = array();
            $services['express'] = $app->getService("Pupcake\Service\Express"); //load Express service
            $services['constraint'] = $app->getService("Pupcake\Service\RouteConstraint"); //load RouteConstraint service
            $services['action'] = $app->getService("Pupcake\Service\RouteAction"); //load RouteAction service

            // custom event registration
            $app->on("system.routing.route.create", function($event) use ($services) {
                $service_callbacks = array(
                    function($event){
                        $event->props('route')->method('hello', function($word){
                            return "hello $word";
                        }); 
                    }
                );
                $service_callbacks = array_merge($services, $service_callbacks);
                return call_user_func_array( array($event, "register"), $service_callbacks)->start();
            });

            $app->get("api/ip/:ip", function($req, $res) use ($app) {
                $route = $app->getRouter()->getMatchedRoute();
                $action = $route->getAction();
                $hello = $route->hello('world');
                $res->send($action.$hello);
            })
            ->to("api#ip")
            ->constraint(array(
                'ip' =>  function($value){
                    return \Respect\Validation\Validator::ip()->validate($value);
                }
            ));

            $app->run();

            $this->assertEquals($this->getRequestOutput(), $expectation['output']);
        }

    }

}
{'fraction_non_alphanumeric': 0.16696481812760883, 'fraction_numerical': 0.005813953488372093, 'mean_word_length': 1.8707744972186564, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 86, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, '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': '363669', 'n_tokens_mistral': 1776, 'n_tokens_neox': 1671, 'n_words': 298}