Created
September 27, 2017 09:43
-
-
Save alexander-schranz/8a0dda598537ad5740faefe6333637e8 to your computer and use it in GitHub Desktop.
Sulu Sample Test Case Setup
This file contains hidden or 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
| diff --git a/tests/Functional/AbstractAppTestCase.php b/tests/Functional/AbstractAppTestCase.php | |
| new file mode 100644 | |
| index 0000000..a45faab | |
| --- /dev/null | |
| +++ b/tests/Functional/AbstractAppTestCase.php | |
| @@ -0,0 +1,117 @@ | |
| +<?php | |
| + | |
| +namespace Tests\Functional; | |
| + | |
| +use Doctrine\ORM\EntityManager; | |
| +use Sulu\Bundle\TestBundle\Testing\SuluTestCase; | |
| +use Sulu\Component\DocumentManager\DocumentManager; | |
| +use Symfony\Bundle\FrameworkBundle\Client; | |
| +use Symfony\Component\HttpKernel\Kernel; | |
| + | |
| +/** | |
| + * Base test case. | |
| + */ | |
| +abstract class AbstractAppTestCase extends SuluTestCase | |
| +{ | |
| + /** | |
| + * @var EntityManager | |
| + */ | |
| + protected $entityManager; | |
| + | |
| + /** | |
| + * @var DocumentManager | |
| + */ | |
| + protected $documentManager; | |
| + | |
| + /** | |
| + * @var Client | |
| + */ | |
| + protected $client; | |
| + | |
| + /** | |
| + * @var Kernel[] | |
| + */ | |
| + protected $kernelStack; | |
| + | |
| + /** | |
| + * {@inheritdoc} | |
| + */ | |
| + public function tearDown() | |
| + { | |
| + while ($kernel = array_shift($this->kernelStack)) { | |
| + $kernel->shutdown(); | |
| + } | |
| + } | |
| + | |
| + /** | |
| + * {@inheritdoc} | |
| + */ | |
| + protected function setUp() | |
| + { | |
| + $this->purgeDatabase(); | |
| + $this->entityManager = $this->getEntityManager(); | |
| + $this->client = $this->createAuthenticatedClient('127.0.0.1'); | |
| + } | |
| + | |
| + /** | |
| + * {@inheritdoc} | |
| + */ | |
| + protected function getKernel(array $options = []) | |
| + { | |
| + $kernel = new \WebsiteKernel('test', true); | |
| + $kernel->boot(); | |
| + $this->kernelStack[] = $kernel; | |
| + | |
| + return $kernel; | |
| + } | |
| + | |
| + /** | |
| + * {@inheritdoc} | |
| + */ | |
| + protected function createAuthenticatedClient($httpHost = null) | |
| + { | |
| + $client = $this->createClient( | |
| + [ | |
| + 'environment' => 'test', | |
| + ], | |
| + [ | |
| + 'PHP_AUTH_USER' => 'test', | |
| + 'PHP_AUTH_PW' => 'test', | |
| + ] | |
| + ); | |
| + | |
| + if (null !== $httpHost) { | |
| + $client->setServerParameter('HTTP_HOST', $httpHost); | |
| + } | |
| + | |
| + return $client; | |
| + } | |
| + | |
| + /** | |
| + * @return DocumentManager | |
| + */ | |
| + protected function getDocumentManager() | |
| + { | |
| + return $this->getContainer()->get('sulu_document_manager.document_manager'); | |
| + } | |
| + | |
| + /** | |
| + * Gets a service. | |
| + * | |
| + * @param $id | |
| + * | |
| + * @return object | |
| + */ | |
| + protected function get($id) | |
| + { | |
| + return $this->getContainer()->get($id); | |
| + } | |
| + | |
| + /** | |
| + * {@inheritdoc} | |
| + */ | |
| + protected function getTestUser() | |
| + { | |
| + return $this->getContainer()->get('test_user_provider')->getUser(); | |
| + } | |
| +} | |
| diff --git a/tests/Functional/AppAdminTestCase.php b/tests/Functional/AppAdminTestCase.php | |
| new file mode 100644 | |
| index 0000000..e764b74 | |
| --- /dev/null | |
| +++ b/tests/Functional/AppAdminTestCase.php | |
| @@ -0,0 +1,21 @@ | |
| +<?php | |
| + | |
| +namespace Tests\Functional; | |
| + | |
| +/** | |
| + * Admin test case. | |
| + */ | |
| +abstract class AppAdminTestCase extends AbstractAppTestCase | |
| +{ | |
| + /** | |
| + * {@inheritdoc} | |
| + */ | |
| + protected function getKernel(array $options = []) | |
| + { | |
| + $kernel = new \AdminKernel('test', true); | |
| + $kernel->boot(); | |
| + $this->kernelStack[] = $kernel; | |
| + | |
| + return $kernel; | |
| + } | |
| +} | |
| diff --git a/tests/Functional/AppWebsiteTestCase.php b/tests/Functional/AppWebsiteTestCase.php | |
| new file mode 100644 | |
| index 0000000..16c4852 | |
| --- /dev/null | |
| +++ b/tests/Functional/AppWebsiteTestCase.php | |
| @@ -0,0 +1,21 @@ | |
| +<?php | |
| + | |
| +namespace Tests\Functional; | |
| + | |
| +/** | |
| + * Website test case. | |
| + */ | |
| +abstract class AppWebsiteTestCase extends AbstractAppTestCase | |
| +{ | |
| + /** | |
| + * {@inheritdoc} | |
| + */ | |
| + protected function getKernel(array $options = []) | |
| + { | |
| + $kernel = new \WebsiteKernel('test', true); | |
| + $kernel->boot(); | |
| + $this->kernelStack[] = $kernel; | |
| + | |
| + return $kernel; | |
| + } | |
| +} | |
| diff --git a/tests/Functional/Templates/Pages/DefaultTest.php b/tests/Functional/Templates/Pages/DefaultTest.php | |
| new file mode 100644 | |
| index 0000000..acf456f | |
| --- /dev/null | |
| +++ b/tests/Functional/Templates/Pages/DefaultTest.php | |
| @@ -0,0 +1,45 @@ | |
| +<?php | |
| + | |
| +namespace Tests\Functional\Templates\Pages; | |
| + | |
| +use Tests\Functional\AppWebsiteTestCase; | |
| +use Tests\Functional\Traits\PageTrait; | |
| + | |
| +/** | |
| + * Test default template. | |
| + */ | |
| +class DefaultTest extends AppWebsiteTestCase | |
| +{ | |
| + use PageTrait; | |
| + | |
| + /** | |
| + * {@inheritdoc} | |
| + */ | |
| + public function setUp() | |
| + { | |
| + parent::setUp(); | |
| + $this->initPhpcr(); | |
| + } | |
| + | |
| + public function testDefault() | |
| + { | |
| + $this->createPage( | |
| + 'default', | |
| + [ | |
| + 'locale' => 'en', | |
| + 'title' => 'New Defaultseite', | |
| + 'url' => '/default', | |
| + 'article' => '<p>Description</p>', | |
| + 'published' => true, | |
| + ] | |
| + ); | |
| + | |
| + $client = $this->createWebsiteClient(); | |
| + $crawler = $client->request('GET', '/default'); | |
| + | |
| + $this->assertHttpStatusCode(200, $client->getResponse()); | |
| + | |
| + $this->assertCrawlerProperty($crawler, '[property=title]', 'New Defaultseite'); | |
| + $this->assertCrawlerProperty($crawler, '[property=article]', '<p>Description</p>'); | |
| + } | |
| +} | |
| diff --git a/tests/Functional/Templates/Pages/HomepageTest.php b/tests/Functional/Templates/Pages/HomepageTest.php | |
| new file mode 100644 | |
| index 0000000..8cf8439 | |
| --- /dev/null | |
| +++ b/tests/Functional/Templates/Pages/HomepageTest.php | |
| @@ -0,0 +1,45 @@ | |
| +<?php | |
| + | |
| +namespace Tests\Functional\Templates\Pages; | |
| + | |
| +use Tests\Functional\AppWebsiteTestCase; | |
| +use Tests\Functional\Traits\PageTrait; | |
| + | |
| +/** | |
| + * Test homepage template. | |
| + */ | |
| +class HomepageTest extends AppWebsiteTestCase | |
| +{ | |
| + use PageTrait; | |
| + | |
| + /** | |
| + * {@inheritdoc} | |
| + */ | |
| + public function setUp() | |
| + { | |
| + parent::setUp(); | |
| + $this->initPhpcr(); | |
| + } | |
| + | |
| + public function testHomepage() | |
| + { | |
| + $this->createPage( | |
| + 'homepage', | |
| + [ | |
| + 'locale' => 'en', | |
| + 'title' => 'New Homepage', | |
| + 'url' => '/homepage', | |
| + 'article' => '<p>Description</p>', | |
| + 'published' => true, | |
| + ] | |
| + ); | |
| + | |
| + $client = $this->createWebsiteClient(); | |
| + $crawler = $client->request('GET', '/homepage'); | |
| + | |
| + $this->assertHttpStatusCode(200, $client->getResponse()); | |
| + | |
| + $this->assertCrawlerProperty($crawler, 'title', 'New Homepage'); | |
| + $this->assertCrawlerProperty($crawler, '[property=article]', '<p>Description</p>'); | |
| + } | |
| +} | |
| diff --git a/tests/Functional/Traits/PageTrait.php b/tests/Functional/Traits/PageTrait.php | |
| new file mode 100644 | |
| index 0000000..608c4c4 | |
| --- /dev/null | |
| +++ b/tests/Functional/Traits/PageTrait.php | |
| @@ -0,0 +1,59 @@ | |
| +<?php | |
| + | |
| +namespace Tests\Functional\Traits; | |
| + | |
| +use Sulu\Bundle\ContentBundle\Document\PageDocument; | |
| +use Sulu\Component\Content\Document\WorkflowStage; | |
| +use Symfony\Component\DomCrawler\Crawler; | |
| + | |
| +/** | |
| + * Page trait to create new pages. | |
| + */ | |
| +trait PageTrait | |
| +{ | |
| + /** | |
| + * @param string $template | |
| + * @param array $data | |
| + * @param string $locale | |
| + * | |
| + * @return PageDocument | |
| + */ | |
| + protected function createPage($template, array $data) | |
| + { | |
| + $documentManager = $this->getDocumentManager(); | |
| + /** @var \Sulu\Bundle\ContentBundle\Document\PageDocument $document */ | |
| + $document = $documentManager->create('page'); | |
| + $document->setLocale($data['locale']); | |
| + $document->setTitle($data['title']); | |
| + $document->setStructureType($template); | |
| + $document->setResourceSegment($data['url']); | |
| + | |
| + if ($data['published']) { | |
| + $document->setWorkflowStage(WorkflowStage::PUBLISHED); | |
| + } | |
| + | |
| + $document->getStructure()->bind($data); | |
| + | |
| + $documentManager->persist($document, $data['locale'], ['parent_path' => '/cmf/example/contents']); | |
| + | |
| + if ($data['published']) { | |
| + $documentManager->publish($document, $data['locale']); | |
| + } | |
| + | |
| + $documentManager->flush(); | |
| + | |
| + return $document; | |
| + } | |
| + | |
| + /** | |
| + * Assert crawler property. | |
| + * | |
| + * @param Crawler $crawler | |
| + * @param string $selector | |
| + * @param string $needle | |
| + */ | |
| + private function assertCrawlerProperty(Crawler $crawler, $selector, $needle) | |
| + { | |
| + $this->assertContains($needle, $crawler->filter($selector)->html()); | |
| + } | |
| +} | |
| diff --git a/tests/HelperTest.php b/tests/HelperTest.php | |
| new file mode 100644 | |
| index 0000000..0382cd5 | |
| --- /dev/null | |
| +++ b/tests/HelperTest.php | |
| @@ -0,0 +1,11 @@ | |
| +<?php | |
| + | |
| +namespace Tests; | |
| + | |
| +class HelperTest extends \PHPUnit_Framework_TestCase | |
| +{ | |
| + public function testHelp() | |
| + { | |
| + $this->assertEquals('test', 'test'); | |
| + } | |
| +} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment