-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add helper classes for shortcode testing (#23)
This PR addresses the shortcomings outlined in #12 and #13, and probably replaces #10 and #18 as well. 1. Deprecate the `\Webfactory\ShortcodeBundle\Tests\Functional\ShortcodeTest` class. * It was not available through the regular autoload mechanism (required extra trickery), * depended on undeclared dependencies (BrowserKit and DomCrawler), * came without tests, * suggested users to do/prefer full-scale Application Testing (using `WebTestCase`) and * worked only through the means of (ab-)using the Shortcode Guide. 2. Provide alternate means of testing shortcode processing * Encourage users to do plain/direct unit testing (or integration testing, when necessary) of their shortcode handlers and/or shortcode handling controllers through new sections in the README. This gives best (most direct) control of input parameters. * Add the public `\Webfactory\ShortcodeBundle\Test\ShortcodeDefinitionTestHelper` service that can be used in Kernel-based (functional) tests to verify a given shortcode name is known, the controller can be resolved etc. * Add a `\Webfactory\ShortcodeBundle\Test\EndToEndTestHelper` class that simplifies full end-to-end (from content to processed content) functional testing, including round-trips through Symfony's FragmentHandler for controller-based shortcodes. Fixes #12, fixes #13, closes #10, closes #18, closes #24. Co-authored-by: Malte Wunsch <mw@webfactory.de>
- Loading branch information
1 parent
6a0f46d
commit ee4e741
Showing
14 changed files
with
393 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
#!/bin/bash | ||
|
||
cat <<< $(jq --arg version $VERSION '.require |= with_entries(if ((.key|test("^symfony/service-contracts")|not) and (.key|test("^symfony/"))) then .value=$version else . end)' < composer.json) > composer.json | ||
cat <<< $(jq --arg version $VERSION '.require |= with_entries(if ((.key|test("^symfony/deprecation-contracts")|not) and (.key|test("^symfony/"))) then .value=$version else . end)' < composer.json) > composer.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Webfactory\ShortcodeBundle\Test; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Thunder\Shortcode\Processor\Processor; | ||
|
||
/** | ||
* Helper class that you can use in functional (end-to-end) tests to verify that a given | ||
* content with shortcodes is processed as expected. | ||
*/ | ||
class EndToEndTestHelper | ||
{ | ||
/** | ||
* @var Processor | ||
*/ | ||
private $processor; | ||
|
||
/** | ||
* @var RequestStack | ||
*/ | ||
private $requestStack; | ||
|
||
public static function createFromContainer(ContainerInterface $container): self | ||
{ | ||
return new self($container->get(Processor::class), $container->get(RequestStack::class)); | ||
} | ||
|
||
public function __construct(Processor $processor, RequestStack $requestStack) | ||
{ | ||
$this->processor = $processor; | ||
$this->requestStack = $requestStack; | ||
} | ||
|
||
public function processShortcode(string $shortcode): string | ||
{ | ||
// The fragment handler used by EmbeddedShortcodeHandler requires a request to be active, so let's make sure that is the case | ||
if (null === $this->requestStack->getCurrentRequest()) { | ||
$this->requestStack->push(new Request()); | ||
} | ||
|
||
return $this->processor->process($shortcode); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace Webfactory\ShortcodeBundle\Test; | ||
|
||
use RuntimeException; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface; | ||
use Thunder\Shortcode\HandlerContainer\HandlerContainerInterface; | ||
use Webfactory\ShortcodeBundle\Handler\EmbeddedShortcodeHandler; | ||
|
||
/** | ||
* Helper class that you can use in functional (end-to-end) tests to verify that a given | ||
* content with shortcodes is processed as expected. | ||
*/ | ||
class ShortcodeDefinitionTestHelper | ||
{ | ||
/** | ||
* @var ControllerResolverInterface | ||
*/ | ||
private $controllerResolver; | ||
|
||
/** | ||
* @var HandlerContainerInterface | ||
*/ | ||
private $handlerContainer; | ||
|
||
public function __construct(ControllerResolverInterface $controllerResolver, HandlerContainerInterface $handlerContainer) | ||
{ | ||
$this->handlerContainer = $handlerContainer; | ||
$this->controllerResolver = $controllerResolver; | ||
} | ||
|
||
public function hasShortcode(string $shortcode): bool | ||
{ | ||
return null !== $this->handlerContainer->get($shortcode); | ||
} | ||
|
||
public function getHandler(string $shortcode): callable | ||
{ | ||
return $this->handlerContainer->get($shortcode); | ||
} | ||
|
||
/** | ||
* @return callable-array | ||
*/ | ||
public function resolveShortcodeController(string $shortcode): array | ||
{ | ||
$handler = $this->handlerContainer->get($shortcode); | ||
|
||
if (!$handler instanceof EmbeddedShortcodeHandler) { | ||
throw new RuntimeException('In order to test resolution of shortcodes to Controllers, the handler must be an instance of EmbeddedShortcodeHandler'); | ||
} | ||
|
||
return $this->controllerResolver->getController(new Request([], [], ['_controller' => $handler->getControllerName()])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.