|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Webfactory\ShortcodeBundle\Test; |
|
4
|
|
|
|
|
5
|
|
|
use RuntimeException; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
7
|
|
|
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface; |
|
8
|
|
|
use Thunder\Shortcode\HandlerContainer\HandlerContainerInterface; |
|
9
|
|
|
use Webfactory\ShortcodeBundle\Handler\EmbeddedShortcodeHandler; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Helper class that you can use in functional (end-to-end) tests to verify that a given |
|
13
|
|
|
* content with shortcodes is processed as expected. |
|
14
|
|
|
*/ |
|
15
|
|
|
class ShortcodeDefinitionTestHelper |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var ControllerResolverInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $controllerResolver; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var HandlerContainerInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $handlerContainer; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct(ControllerResolverInterface $controllerResolver, HandlerContainerInterface $handlerContainer) |
|
28
|
|
|
{ |
|
29
|
|
|
$this->handlerContainer = $handlerContainer; |
|
30
|
|
|
$this->controllerResolver = $controllerResolver; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function hasShortcode(string $shortcode): bool |
|
34
|
|
|
{ |
|
35
|
|
|
return null !== $this->handlerContainer->get($shortcode); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function getHandler(string $shortcode): callable |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->handlerContainer->get($shortcode); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return callable-array |
|
|
|
|
|
|
45
|
|
|
*/ |
|
46
|
|
|
public function resolveShortcodeController(string $shortcode): array |
|
47
|
|
|
{ |
|
48
|
|
|
$handler = $this->handlerContainer->get($shortcode); |
|
49
|
|
|
|
|
50
|
|
|
if (!$handler instanceof EmbeddedShortcodeHandler) { |
|
51
|
|
|
throw new RuntimeException('In order to test resolution of shortcodes to Controllers, the handler must be an instance of EmbeddedShortcodeHandler'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $this->controllerResolver->getController(new Request([], [], ['_controller' => $handler->getControllerName()])); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|