Skip to content

Commit 9105035

Browse files
committed
Allow overwriting shortcode params on detail page
This eases more automated tests for a shortcode than just, e.g. for unhappy paths.
1 parent d047542 commit 9105035

2 files changed

Lines changed: 91 additions & 1 deletion

File tree

Controller/GuideController.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
77
use Symfony\Component\DependencyInjection\Container;
88
use Symfony\Component\DependencyInjection\ContainerBuilder;
9+
use Symfony\Component\HttpFoundation\Request;
910
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1011

1112
/**
@@ -44,10 +45,16 @@ public function listAction()
4445
* @Template()
4546
* @return array
4647
*/
47-
public function detailAction($shortcode)
48+
public function detailAction($shortcode, Request $request)
4849
{
4950
foreach ($this->shortcodeTags as $shortcodeTag) {
5051
if ($shortcodeTag['shortcode'] === $shortcode) {
52+
// if custom parameters are provided, replace the example
53+
$customParameters = $request->get('customParameters');
54+
if ($customParameters) {
55+
$shortcodeTag['example'] = $shortcode . ' ' . $customParameters;
56+
}
57+
5158
return [
5259
'shortcodeTag' => $shortcodeTag,
5360
];

Tests/Functional/ShortcodeTest.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace Webfactory\ShortcodeBundle\Tests\Functional;
4+
5+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6+
use Symfony\Component\DomCrawler\Crawler;
7+
8+
/**
9+
* Abstract template for common shortcode tests.
10+
*/
11+
abstract class ShortcodeTest extends WebTestCase
12+
{
13+
/**
14+
* @return string name of the shortcode to test.
15+
*/
16+
abstract protected function getShortcodeToTest();
17+
18+
protected function setUp()
19+
{
20+
parent::setUp();
21+
22+
if ($this->getShortcodeToTest() === '' || $this->getShortcodeToTest() === null) {
23+
throw new \PHPUnit_Framework_IncompleteTestError(
24+
'Albeit being a ' . __CLASS__ . ', ' . get_called_class() . ' does not define a shortcode to test.'
25+
);
26+
}
27+
}
28+
29+
/**
30+
* @param string|null $customParameters
31+
* @return Crawler
32+
*/
33+
protected function crawlRenderedExample($customParameters = null)
34+
{
35+
$urlWithRenderedExample = $this->getUrlWithRenderedExample($customParameters);
36+
37+
$client = static::createClient();
38+
$crawlerOnRenderedExamplePage = $client->request('GET', $urlWithRenderedExample);
39+
$this->assertEquals(200, $client->getResponse()->getStatusCode());
40+
41+
$crawlerOnRenderedExample = $crawlerOnRenderedExamplePage->filter('#rendered-example');
42+
if ($crawlerOnRenderedExample->count() === 0) {
43+
throw new \PHPUnit_Framework_ExpectationFailedException(
44+
'No rendered example found for shortcode "' . $this->shortcode . '"'
45+
);
46+
}
47+
48+
return $crawlerOnRenderedExample;
49+
}
50+
51+
/**
52+
* @param int $expectedStatusCode
53+
* @param string|null $customParameters
54+
* @return Crawler
55+
*/
56+
protected function assertHttpStatusCodeWhenCrawlingRenderedExample($expectedStatusCode, $customParameters = null)
57+
{
58+
$urlWithRenderedExample = $this->getUrlWithRenderedExample($customParameters);
59+
60+
$client = static::createClient();
61+
$crawlerOnRenderedExamplePage = $client->request('GET', $urlWithRenderedExample);
62+
$this->assertEquals($expectedStatusCode, $client->getResponse()->getStatusCode());
63+
}
64+
65+
/**
66+
* @param string|null $customParameters
67+
* @return string
68+
*/
69+
protected function getUrlWithRenderedExample($customParameters = null)
70+
{
71+
static::bootKernel();
72+
73+
$urlParameters = ['shortcode' => $this->getShortcodeToTest()];
74+
if ($customParameters) {
75+
$urlParameters['customParameters'] = $customParameters;
76+
}
77+
78+
return static::$kernel
79+
->getContainer()
80+
->get('router')
81+
->generate('webfactory.shortcode.guide-detail', $urlParameters);
82+
}
83+
}

0 commit comments

Comments
 (0)