Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix not found ShortcodeTest class by the autoloader #10

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions src/Test/ShortcodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace Webfactory\ShortcodeBundle\Test;

use PHPUnit_Framework_ExpectationFailedException;
use PHPUnit_Framework_IncompleteTestError;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\BrowserKit\Client;
use Symfony\Component\DomCrawler\Crawler;

/**
* 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.');
}

static::bootKernel();
$this->client = static::createClient();
}

/**
* @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::$kernel->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;
}
}
105 changes: 4 additions & 101 deletions tests/Functional/ShortcodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,110 +2,13 @@

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_error(sprintf('The "%s" class is deprecated, use "%s" instead.', ShortcodeTest::class, \Webfactory\ShortcodeBundle\Test\ShortcodeTest::class), \E_USER_DEPRECATED);

/**
* Abstract template for common shortcode tests.
*
* @deprecated Use \Webfactory\ShortcodeBundle\Test\ShortcodeTest instead
*/
abstract class ShortcodeTest extends WebTestCase
abstract class ShortcodeTest extends \Webfactory\ShortcodeBundle\Test\ShortcodeTest
{
/** @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.');
}

static::bootKernel();
$this->client = static::createClient();
}

/**
* @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::$container->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;
}
}