|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * |
| 4 | + * NOTICE OF LICENSE |
| 5 | + * |
| 6 | + * This source file is subject to the Skywire License |
| 7 | + * that is available through the world-wide-web at this URL: |
| 8 | + * http://skywire.co.uk/skywire/license/ |
| 9 | + * If you did not receive a copy of the license and are unable to |
| 10 | + * obtain it through the world-wide-web, please send an email |
| 11 | + * to interact@skywire.co.uk so we can send you a copy immediately. |
| 12 | + * |
| 13 | + */ |
| 14 | + |
| 15 | +/** |
| 16 | + * Description of class |
| 17 | + * |
| 18 | + * Further description is required |
| 19 | + * |
| 20 | + * @category Skywire |
| 21 | + * @package Skywire\ModuleName |
| 22 | + * @author Skywire Tech Team <tech@skywire.co.uk> |
| 23 | + * @copyright 2018 Skywire Ltd (http://skywire.co.uk) |
| 24 | + * @license Skywire Licence http://skywire.co.uk/skywire/license/ |
| 25 | + * @link http://skywire.co.uk |
| 26 | + */ |
| 27 | + |
| 28 | +namespace Skywire\WordpressApi\Block; |
| 29 | + |
| 30 | + |
| 31 | +use Magento\Framework\App\Area; |
| 32 | +use Magento\Framework\App\State; |
| 33 | +use Skywire\TestFramework\Integration\TestCase; |
| 34 | +use Skywire\WordpressApi\Model\RestClientFactory; |
| 35 | + |
| 36 | +class MegaMenuTest extends TestCase |
| 37 | +{ |
| 38 | + /** |
| 39 | + * @var \Skywire\WordpressApi\Model\Api\Category |
| 40 | + */ |
| 41 | + private $categoryApi; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var \Skywire\WordpressApi\Model\Api\Post |
| 45 | + */ |
| 46 | + private $postApi; |
| 47 | + |
| 48 | + public function testGetCategories() |
| 49 | + { |
| 50 | + $block = $this->objectManager->create(MegaMenu::class, ['categoryApi' => $this->categoryApi]); |
| 51 | + $categories = $block->getCategories(); |
| 52 | + |
| 53 | + $this->assertCount(2, $categories); |
| 54 | + |
| 55 | + $this->assertEquals('Level 1', $categories[0]->getName()); |
| 56 | + $this->assertCount(1, $categories[0]->getChildren()); |
| 57 | + $this->assertEquals('Level 2', $categories[0]->getChildren()[0]->getName()); |
| 58 | + |
| 59 | + $this->assertEquals('Level 1-a', $categories[1]->getName()); |
| 60 | + $this->assertCount(1, $categories[1]->getChildren()); |
| 61 | + $this->assertEquals('Level 2-a', $categories[1]->getChildren()[0]->getName()); |
| 62 | + } |
| 63 | + |
| 64 | + public function testGetLatest() |
| 65 | + { |
| 66 | + $block = $this->objectManager->create(MegaMenu::class, ['postApi' => $this->postApi]); |
| 67 | + |
| 68 | + $latest = $block->getLatest(); |
| 69 | + $this->assertCount(4, $latest); |
| 70 | + $this->assertEquals('post-1', $latest[1]->getSlug()); |
| 71 | + $this->assertEquals('post-4', $latest[4]->getSlug()); |
| 72 | + } |
| 73 | + |
| 74 | + public function testToHtml() |
| 75 | + { |
| 76 | + $block = $this->objectManager->create(MegaMenu::class, |
| 77 | + [ |
| 78 | + 'categoryApi' => $this->categoryApi, |
| 79 | + 'postApi' => $this->postApi |
| 80 | + ] |
| 81 | + ); |
| 82 | + |
| 83 | + $html = $this->objectManager->get(State::class)->emulateAreaCode(Area::AREA_FRONTEND, |
| 84 | + function () use ($block) { |
| 85 | + return $block->toHtml(); |
| 86 | + } |
| 87 | + ); |
| 88 | + |
| 89 | + $actual = new \DOMDocument(); |
| 90 | + $actual->loadHTML($html); |
| 91 | + |
| 92 | + $this->assertCount(4, $actual->getElementsByTagName('ul')); |
| 93 | + $this->assertCount(8, $actual->getElementsByTagName('li')); |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + protected function getCategoryData() |
| 98 | + { |
| 99 | + return file_get_contents(__DIR__ . '/../_files/category_hierarchy.json'); |
| 100 | + } |
| 101 | + |
| 102 | + protected function getLatestData() |
| 103 | + { |
| 104 | + return file_get_contents(__DIR__ . '/../_files/latest_posts.json'); |
| 105 | + } |
| 106 | + |
| 107 | + protected function setUp() |
| 108 | + { |
| 109 | + // setup category |
| 110 | + $response = new \Zend_Http_Response(200, [], $this->getCategoryData()); |
| 111 | + |
| 112 | + $restClient = $this->getMockBuilder(\Zend_Rest_Client::class)->disableOriginalConstructor()->getMock(); |
| 113 | + $restClient->method('restGet')->willReturn($response); |
| 114 | + |
| 115 | + $clientFactory = $this->getMockBuilder(RestClientFactory::class)->getMock(); |
| 116 | + $clientFactory->method('create')->willReturn($restClient); |
| 117 | + |
| 118 | + $categoryApi = $this->objectManager->create(\Skywire\WordpressApi\Model\Api\Category::class, |
| 119 | + ['restClientFactory' => $clientFactory]); |
| 120 | + |
| 121 | + $this->categoryApi = $categoryApi; |
| 122 | + |
| 123 | + // setup posts |
| 124 | + $response = new \Zend_Http_Response(200, [], $this->getLatestData()); |
| 125 | + |
| 126 | + $restClient = $this->getMockBuilder(\Zend_Rest_Client::class)->disableOriginalConstructor()->getMock(); |
| 127 | + $restClient->method('restGet')->willReturn($response); |
| 128 | + |
| 129 | + $clientFactory = $this->getMockBuilder(RestClientFactory::class)->getMock(); |
| 130 | + $clientFactory->method('create')->willReturn($restClient); |
| 131 | + |
| 132 | + $postApi = $this->objectManager->create(\Skywire\WordpressApi\Model\Api\Post::class, |
| 133 | + ['restClientFactory' => $clientFactory]); |
| 134 | + |
| 135 | + $this->postApi = $postApi; |
| 136 | + } |
| 137 | +} |
0 commit comments