Skip to content

Commit 71c9919

Browse files
committed
Explained how to easily test shortcodes
1 parent a23cce8 commit 71c9919

1 file changed

Lines changed: 62 additions & 13 deletions

File tree

README.md

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ used by users in user generated content to embed some other content or markup. E
1111
comment:
1212

1313
```
14-
[image src="https://upload.wikimedia.org/wikipedia/en/f/f7/RickRoll.png"]
14+
[image url="https://upload.wikimedia.org/wikipedia/en/f/f7/RickRoll.png"]
1515
[text color="red"]This is red text.[/text]
1616
```
1717

18-
In analogy to living style guides, this bundle also provides an optional shortcode guide.
18+
In analogy to living style guides, this bundle also provides an optional shortcode guide. This guide can be used for
19+
automated testing of your shortcodes as well.
1920

2021

2122
## Installation
@@ -86,7 +87,7 @@ after the name in the suqared brackets wll be considered as parameters that will
8687

8788
### Full example
8889

89-
To allow a user input of ```[image src="https://upload.wikimedia.org/wikipedia/en/f/f7/RickRoll.png"]``` to be replaced
90+
To allow a user input of ```[image url="https://upload.wikimedia.org/wikipedia/en/f/f7/RickRoll.png"]``` to be replaced
9091
with HTML markup for this image, use the twig filter "shortcodes" on the user input:
9192

9293
```twig
@@ -147,12 +148,11 @@ final class EmbeddedImageController
147148
*/
148149
public function showAction($url)
149150
{
150-
return $this->twigEngine->renderResponse(
151-
'@App/EmbeddedImage/show.html.twig',
152-
[
153-
'url' => $url,
154-
]
155-
);
151+
if (!$url) {
152+
throw new \RuntimeException('No url provided');
153+
}
154+
155+
return $this->twigEngine->renderResponse('@App/EmbeddedImage/show.html.twig', ['url' => $url]);
156156
}
157157
}
158158
```
@@ -161,7 +161,9 @@ And finally a twig template like this:
161161

162162
```twig
163163
{# src/Ressources/views/EmbeddedImage/show.html.twig #}
164-
<img src="{{ url }}" />
164+
<div class="shortcode-container">
165+
<img src="{{ url }}" />
166+
</div>
165167
```
166168

167169

@@ -210,18 +212,65 @@ Finally, enrich your shortcode tags with description and example attributes for
210212
<tag
211213
name="webfactory.shortcode"
212214
shortcode="image"
213-
description="Renders an image tag with the source {src}."
214-
example="img src=https://upload.wikimedia.org/wikipedia/en/f/f7/RickRoll.png"
215+
description="Renders an image tag with the {url} as it's source."
216+
example="image url=https://upload.wikimedia.org/wikipedia/en/f/f7/RickRoll.png"
215217
/>
216218
</service>
217219
</services>
218220
</container>
219221
```
220222

221-
With the route prefix defined as above, call /shortcodes/ to get the list of shortcodes and follow the links to the
223+
With the route prefix defined as above, call ```/shortcodes/``` to get the list of shortcodes and follow the links to the
222224
detail pages.
223225

224226

227+
### Automated Tests for your Shortcodes
228+
229+
With the shortcode guide enabled (remember: you may enable it just in your test environment), you can easily write
230+
functional tests for your shortcodes using the rendered detail pages. This way, you can test even shortcodes with
231+
complex dependencies. But as functional tests are slow, you may want to keep your shortcode tests in a seperate slow
232+
test suite.
233+
234+
To speed things up, the bundle provides the abstract ```\Webfactory\ShortcodeBundle\Tests\Functional\ShortcodeTest```
235+
class for you to extend. Using it, your test class may look like this (we recommend one test class for each shortcode):
236+
237+
```php
238+
<?php
239+
240+
namespace AppBundle\Tests\Shortcodes;
241+
242+
use Webfactory\ShortcodeBundle\Tests\Functional\ShortcodeTest;
243+
244+
final class SeiteTeaserTest extends ShortcodeTest
245+
{
246+
protected function getShortcodeToTest()
247+
{
248+
return 'image';
249+
}
250+
251+
/** @test */
252+
public function teaser_gets_rendered()
253+
{
254+
// without $customParameters, crawlRenderedExample() will crawl a page rendering the example configured in the
255+
// shortcode tag, in this case "image url=https://upload.wikimedia.org/wikipedia/en/f/f7/RickRoll.png"
256+
$crawler = $this->crawlRenderedExample();
257+
258+
$this->assertCount(1, $crawler->filter('.shortcode-container'));
259+
$this->assertCount(1, $crawler->filter('.shortcode-container img[src="https://upload.wikimedia.org/wikipedia/en/f/f7/RickRoll.png"]'));
260+
}
261+
262+
/** @test */
263+
public function teaser_to_nonexisting_page_gives_error()
264+
{
265+
// both crawlRenderedExample() and assertHttpStatusCodeWhenCrawlingRenderedExample() accept a $customParameters
266+
// argument that will replace the parameters provided in the configuration of the shortcode tag.
267+
// This can be used to cover more test cases, e.g. an unhappy path
268+
$this->assertHttpStatusCodeWhenCrawlingRenderedExample(500, 'url=');
269+
}
270+
}
271+
```
272+
273+
225274
## Credits, Copyright and License
226275

227276
This bundle was started at webfactory GmbH, Bonn.

0 commit comments

Comments
 (0)