Skip to content

Instantly share code, notes, and snippets.

@bdunogier
Last active August 29, 2015 14:00
Show Gist options
  • Save bdunogier/11292331 to your computer and use it in GitHub Desktop.
Save bdunogier/11292331 to your computer and use it in GitHub Desktop.
Feature: template helpers
Scenario: As a template developer, I want to get a Route Reference to the current route
Given I am in any Twig template
And I am in the master request
When I use "{{ set route_reference = route_ref() }}"
Then "route_reference" contains a Route Reference to the current route
Scenario: As a template developer, I want to get a Route Reference to a location
Given I am in any Twig template
And "location" is a valid Location
When I use "{{ set route_reference = route_ref( location ) }}"
Then "root_reference" contains a Route Reference to "ez_content:view Location" for "location"
Scenario: As a template developer, I want to get a link to a Route Reference
Given I am in any Twig template
And "route_reference" is a valid Route Reference
When I use "{{ set path = path( route_reference ) }}"
Then "path" contains the href for the Route Reference
Scenario: As a template developer, I want to get a link to a Route Reference in a different language
Given I am in any Twig template
And "route_reference" is a valid Route Reference
When I use "{{ set path = path( route_reference.set( lang, 'fre-FR' ) }}"
Then "path" contains the href for the Route Reference in "fre-FR"
Scenario: As a template developer, I can't get a link to the current route from a subrequest
Given I am in any Twig template
And I am in a sub request
When I use "{{ route_ref() }}"
Then I expect an exception to be thrown
@wouterj
Copy link

wouterj commented Apr 26, 2014

class TwigExtensionSpec extends ObjectBehaviour
{
    public function let(Request $request, RouteReferenceGenerator $generator)
    {
        $this->beConstructedWith($request, $generator);
    }

    public function it_provides_the_route_reference_to_the_current_route(Request $request)
    {
        $request->getParameter('_route')->willReturn('current_route');
        $request->isMasterRequest()->willReturn(true);

        $this->getRouteReference()->shouldReturn(new RouteReference('current_route'));
    }

    public function it_provides_the_route_reference_to_a_location(RouteReferenceGenerator $generator)
    {
        $routeRef = new RouteReference(...);
        $generator->getReferenceForLocation('location')->willReturn($routeRef);

        $this->getRouteReference('location')->shouldReturn($routeRef);
    }

    public function it_cannot_create_a_route_reference_in_a_subrequest(Request $request)
    {
        $request->isMasterRequest()->willReturn(false);

        $this->throwException()->duringGetRouteReference();
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment