-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathShortcodeTest.php
More file actions
113 lines (91 loc) · 4 KB
/
ShortcodeTest.php
File metadata and controls
113 lines (91 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
namespace Webfactory\ShortcodeBundle\Tests\Functional;
use PHPUnit_Framework_ExpectationFailedException;
use PHPUnit_Framework_IncompleteTestError;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\BrowserKit\Client;
use Symfony\Component\DomCrawler\Crawler;
trigger_deprecation('webfactory/shortcode-bundle', '2.1.0', 'The '.ShortcodeTest::class.' class is deprecated without replacement; see the bundle README file for suggestions on how to test your shortcodes.');
/**
* Abstract template for common shortcode tests.
*/
abstract class ShortcodeTest extends WebTestCase
{
/** @var Client */
protected $client;
/**
* @return string name of the shortcode to test.
*/
abstract protected function getShortcodeToTest(): string;
protected function setUp(): void
{
parent::setUp();
if ('' === $this->getShortcodeToTest() || null === $this->getShortcodeToTest()) {
throw new PHPUnit_Framework_IncompleteTestError('Albeit being a '.__CLASS__.', '.static::class.' does not define a shortcode to test.');
}
$this->client = static::createClient();
static::bootKernel();
}
/**
* @param array|string|null $customParameters use of strings is deprecated, use array instead.
*
* @return Crawler
*/
protected function getRenderedExampleHtml(?array $customParameters = null): string
{
return $this->crawlRenderedExample($customParameters)->html();
}
/**
* @param array|string|null $customParameters use of strings is deprecated, use array instead.
*/
protected function crawlRenderedExample(/* array */ $customParameters = null): Crawler
{
$urlWithRenderedExample = $this->getUrlWithRenderedExample($customParameters);
$crawlerOnRenderedExamplePage = $this->client->request('GET', $urlWithRenderedExample);
$this->assertEquals(200, $this->client->getResponse()->getStatusCode());
$crawlerOnRenderedExample = $crawlerOnRenderedExamplePage->filter('#rendered-example');
if (0 === $crawlerOnRenderedExample->count()) {
throw new PHPUnit_Framework_ExpectationFailedException('No rendered example found for shortcode "'.$this->shortcode.'"');
}
return $crawlerOnRenderedExample;
}
/**
* @param array|string|null $customParameters use of strings is deprecated, use array instead.
*/
protected function assertHttpStatusCodeWhenCrawlingRenderedExample(
int $expectedStatusCode,
/* array */ $customParameters = null,
): Crawler {
$urlWithRenderedExample = $this->getUrlWithRenderedExample($customParameters);
$crawlerOnRenderedExamplePage = $this->client->request('GET', $urlWithRenderedExample);
$this->assertEquals($expectedStatusCode, $this->client->getResponse()->getStatusCode());
}
/**
* @param array|string|null $customParameters use of strings is deprecated, use array instead.
*
* @return Crawler
*/
private function getUrlWithRenderedExample(/* array */ $customParameters = null): string
{
$urlParameters = ['shortcode' => $this->getShortcodeToTest()];
$customParametersAsString = $this->getCustomParametersAsString($customParameters);
if ($customParametersAsString) {
$urlParameters['customParameters'] = $customParametersAsString;
}
return static::getContainer()->get('router')->generate('webfactory.shortcode.guide-detail', $urlParameters);
}
private function getCustomParametersAsString($customParametersAsMixed): ?string
{
if (\is_string($customParametersAsMixed)) {
return $customParametersAsMixed;
}
if (\is_array($customParametersAsMixed)) {
$customParametersAsString = '';
foreach ($customParametersAsMixed as $name => $value) {
$customParametersAsString .= $name.'='.$value.' ';
}
return $customParametersAsString;
}
return null;
}
}