|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Webfactory\ShortcodeBundle\Tests\Functional; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
|
6
|
|
|
use Symfony\Component\BrowserKit\Client; |
|
|
|
|
|
|
7
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
|
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Abstract template for common shortcode tests. |
|
11
|
|
|
*/ |
|
12
|
|
|
abstract class ShortcodeTest extends WebTestCase |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var Client */ |
|
15
|
|
|
protected $client; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @return string name of the shortcode to test. |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract protected function getShortcodeToTest(): string; |
|
21
|
|
|
|
|
22
|
|
|
protected function setUp(): void |
|
23
|
|
|
{ |
|
24
|
|
|
parent::setUp(); |
|
25
|
|
|
|
|
26
|
|
|
if ('' === $this->getShortcodeToTest() || null === $this->getShortcodeToTest()) { |
|
27
|
|
|
throw new \PHPUnit_Framework_IncompleteTestError( |
|
|
|
|
|
|
28
|
|
|
'Albeit being a '.__CLASS__.', '.\get_called_class().' does not define a shortcode to test.' |
|
29
|
|
|
); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
static::bootKernel(); |
|
33
|
|
|
$this->client = static::createClient(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param array|string|null $customParameters use of strings is deprecated, use array instead. |
|
38
|
|
|
* |
|
39
|
|
|
* @return Crawler |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function getRenderedExampleHtml(?array $customParameters = null): string |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->crawlRenderedExample($customParameters)->html(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param array|string|null $customParameters use of strings is deprecated, use array instead. |
|
48
|
|
|
* |
|
49
|
|
|
* @return Crawler |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function crawlRenderedExample(/*array*/ $customParameters = null): Crawler |
|
52
|
|
|
{ |
|
53
|
|
|
$urlWithRenderedExample = $this->getUrlWithRenderedExample($customParameters); |
|
54
|
|
|
|
|
55
|
|
|
$crawlerOnRenderedExamplePage = $this->client->request('GET', $urlWithRenderedExample); |
|
56
|
|
|
$this->assertEquals(200, $this->client->getResponse()->getStatusCode()); |
|
57
|
|
|
|
|
58
|
|
|
$crawlerOnRenderedExample = $crawlerOnRenderedExamplePage->filter('#rendered-example'); |
|
59
|
|
|
if (0 === $crawlerOnRenderedExample->count()) { |
|
60
|
|
|
throw new \PHPUnit_Framework_ExpectationFailedException( |
|
|
|
|
|
|
61
|
|
|
'No rendered example found for shortcode "'.$this->shortcode.'"' |
|
|
|
|
|
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $crawlerOnRenderedExample; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param int $expectedStatusCode |
|
70
|
|
|
* @param array|string|null $customParameters use of strings is deprecated, use array instead. |
|
71
|
|
|
* |
|
72
|
|
|
* @return Crawler |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function assertHttpStatusCodeWhenCrawlingRenderedExample( |
|
75
|
|
|
int $expectedStatusCode, |
|
76
|
|
|
/*array*/ $customParameters = null |
|
77
|
|
|
): Crawler { |
|
78
|
|
|
$urlWithRenderedExample = $this->getUrlWithRenderedExample($customParameters); |
|
79
|
|
|
|
|
80
|
|
|
$crawlerOnRenderedExamplePage = $this->client->request('GET', $urlWithRenderedExample); |
|
|
|
|
|
|
81
|
|
|
$this->assertEquals($expectedStatusCode, $this->client->getResponse()->getStatusCode()); |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param array|string|null $customParameters use of strings is deprecated, use array instead. |
|
86
|
|
|
* |
|
87
|
|
|
* @return Crawler |
|
88
|
|
|
*/ |
|
89
|
|
|
private function getUrlWithRenderedExample(/*array*/ $customParameters = null): string |
|
90
|
|
|
{ |
|
91
|
|
|
$urlParameters = ['shortcode' => $this->getShortcodeToTest()]; |
|
92
|
|
|
|
|
93
|
|
|
$customParametersAsString = $this->getCustomParametersAsString($customParameters); |
|
94
|
|
|
if ($customParametersAsString) { |
|
95
|
|
|
$urlParameters['customParameters'] = $customParametersAsString; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return static::$container->get('router')->generate('webfactory.shortcode.guide-detail', $urlParameters); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
private function getCustomParametersAsString($customParametersAsMixed): ?string |
|
102
|
|
|
{ |
|
103
|
|
|
if (\is_string($customParametersAsMixed)) { |
|
104
|
|
|
return $customParametersAsMixed; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if (\is_array($customParametersAsMixed)) { |
|
108
|
|
|
$customParametersAsString = ''; |
|
109
|
|
|
foreach ($customParametersAsMixed as $name => $value) { |
|
110
|
|
|
$customParametersAsString .= $name.'='.$value.' '; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $customParametersAsString; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return null; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths