-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEmbeddedShortcodeHandlerTest.php
More file actions
102 lines (86 loc) · 4.12 KB
/
EmbeddedShortcodeHandlerTest.php
File metadata and controls
102 lines (86 loc) · 4.12 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
<?php
namespace Webfactory\ShortcodeBundle\Tests\Functional;
use Generator;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Fragment\FragmentHandler;
use Webfactory\ShortcodeBundle\Handler\EmbeddedShortcodeHandler;
use Webfactory\ShortcodeBundle\Test\EndToEndTestHelper;
use Webfactory\ShortcodeBundle\Tests\Fixtures\Controller\ShortcodeTestController;
/**
* Test shortcode processing using EmbeddedShortcodeHandler and a fixture ShortodeTestController,
* to make sure shortcodes defined in various places (DI config, bundle config) work as expected.
*/
class EmbeddedShortcodeHandlerTest extends KernelTestCase
{
#[Test]
public function paragraphs_wrapping_shortcodes_get_removed(): void
{
self::assertSame('test', $this->processShortcodes('<p>[test-config-inline]</p>'));
}
#[Test]
public function content_without_shortcodes_wont_be_changed(): void
{
self::assertSame('<p>Content without shortcode</p>', $this->processShortcodes('<p>Content without shortcode</p>'));
}
#[DataProvider('provideShortcodeNames')]
#[Test]
public function expand_shortcodes_registered_in_different_ways(string $shortcodeName): void
{
// All shortcodes are set up as fixtures and use ShortcodeTestController
self::assertSame('test foo=bar', $this->processShortcodes("[$shortcodeName foo=bar]"));
}
public static function provideShortcodeNames(): Generator
{
yield 'Inline shortcode defined in bundle config' => ['test-config-inline'];
yield 'ESI-based shortcode defined in bundle config' => ['test-config-esi'];
yield 'Inline shortcode defined in service definitions' => ['test-service-inline'];
yield 'ESI-based shortcode defined in service definitions' => ['test-service-esi'];
}
#[DataProvider('provideEsiShortcodes')]
#[Test]
public function processing_with_esi_fragments(string $shortcodeName): void
{
$request = new Request([], [], [], [], [], ['SCRIPT_URL' => '/', 'HTTP_HOST' => 'localhost']);
$request->headers->set('Surrogate-Capability', 'ESI/1.0');
self::assertStringContainsString('<esi:include ', $this->processShortcodes("[$shortcodeName foo=bar]", $request));
}
public static function provideEsiShortcodes(): Generator
{
yield 'ESI-based shortcode defined in bundle configuration' => ['test-config-esi'];
yield 'ESI-based shortcode defined in service configuration' => ['test-service-esi'];
}
#[Test]
public function invokable_controller_can_be_used(): void
{
self::assertSame('invokable-controller-response', $this->processShortcodes('<p>[test-config-invokable]</p>'));
}
#[DataProvider('provideControllerNames')]
#[Test]
public function throws_exception_on_invalid_controller_names(string $controllerName): void
{
$this->expectException(InvalidArgumentException::class);
new EmbeddedShortcodeHandler($this->createMock(FragmentHandler::class), $controllerName, 'inline', $this->createMock(RequestStack::class));
}
public static function provideControllerNames(): Generator
{
yield 'Empty string' => [''];
yield 'Not existing controller' => ['Foo\Bar::baz'];
yield 'Missing method name' => [ShortcodeTestController::class];
yield 'Not existing method' => [ShortcodeTestController::class.'_notExistingMethod'];
yield 'Missing class' => ['ThisClassDoesNotExist'];
yield 'Valid reference followed by a second scope resolution operator' => [ShortcodeTestController::class.'::test::'];
}
private function processShortcodes(string $content, ?Request $request = null): string
{
self::bootKernel();
if ($request) {
static::getContainer()->get(RequestStack::class)->push($request);
}
return EndToEndTestHelper::createFromContainer(static::getContainer())->processShortcode($content);
}
}