ViewTest.php 8.46 KB
Newer Older
Ketan's avatar
Ketan committed
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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
<?php
/**
 * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
 */
namespace Temando\Shipping\Block\Adminhtml\Dispatch;

use Magento\Framework\Session\SessionManagerInterface;
use Magento\Framework\View\LayoutInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Temando\Shipping\Model\DispatchInterface;
use Temando\Shipping\Model\DispatchProvider;
use Temando\Shipping\Model\ResourceModel\Dispatch\DispatchRepository;
use Temando\Shipping\Model\ResourceModel\Repository\DispatchRepositoryInterface;
use Temando\Shipping\Rest\Adapter;
use Temando\Shipping\Rest\AuthenticationInterface;
use Temando\Shipping\Rest\RestClient;
use Temando\Shipping\Test\Integration\Provider\RestResponseProvider;
use Temando\Shipping\Webservice\HttpClientInterface;
use Temando\Shipping\Webservice\HttpClientInterfaceFactory;

/**
 * Temando View Dispatch Page Test
 *
 * @package  Temando\Shipping\Test\Integration
 * @author   Christoph Aßmann <christoph.assmann@netresearch.de>
 * @license  http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
 * @link     http://www.temando.com/
 *
 */
class ViewTest extends \PHPUnit\Framework\TestCase
{
    /**
     * Delegate provisioning of test data to separate class
     * @return string[]
     */
    public function getCompletionDataProvider()
    {
        return RestResponseProvider::getCompletionResponseDataProvider();
    }

    protected function setUp()
    {
        parent::setUp();

        /** @var SessionManagerInterface $adminSession */
        $adminSession = Bootstrap::getObjectManager()->get(SessionManagerInterface::class);
        $adminSession->setData(AuthenticationInterface::DATA_KEY_SESSION_TOKEN_EXPIRY, '2038-01-19T03:03:33.000Z');
    }

    protected function tearDown()
    {
        /** @var \Magento\TestFramework\ObjectManager $objectManager */
        $objectManager = Bootstrap::getObjectManager();

        /** @var SessionManagerInterface $adminSession */
        $adminSession = $objectManager->get(SessionManagerInterface::class);
        $adminSession->unsetData(AuthenticationInterface::DATA_KEY_SESSION_TOKEN_EXPIRY);

        $objectManager->removeSharedInstance(RestClient::class);
        $objectManager->removeSharedInstance(DispatchProvider::class);
        $objectManager->removeSharedInstance(DispatchRepository::class);
        $objectManager->removeSharedInstance(Adapter::class);

        parent::tearDown();
    }

    /**
     * Assert dispatch listing url is being generated.
     *
     * @test
     * @magentoAppArea adminhtml
     * @magentoConfigFixture default/carriers/temando/sovereign_endpoint https://foo.temando.io/v1/
     */
    public function getDispatchListingPageUrl()
    {
        /** @var LayoutInterface $layout */
        $layout = Bootstrap::getObjectManager()->get(LayoutInterface::class);
        /** @var View $block */
        $block = $layout->createBlock(View::class);

        $this->assertContains('dispatch/index', $block->getDispatchesPageUrl());
    }

    /**
     * Assert exception is caught if repository cannot load dispatch.
     *
     * @test
     * @magentoAppArea adminhtml
     * @magentoConfigFixture default/carriers/temando/sovereign_endpoint https://foo.temando.io/v1/
     */
    public function dispatchCannotBeLoaded()
    {
        // prepare dispatch view request
        $request = Bootstrap::getObjectManager()->get(\Magento\TestFramework\Request::class);
        $request->setParam('dispatch_id', 'f00');

        // prepare api response (dispatch not found)
        $body = '{"errors":[{"status":"404","title":"Completion with id \'f00\' not found.","code":"NotFoundError"}]}';
        $testResponse = new \Zend\Http\Response();
        $testResponse->setStatusCode(\Zend\Http\Response::STATUS_CODE_404);
        $testResponse->setContent($body);

        $testAdapter = new \Zend\Http\Client\Adapter\Test();
        $testAdapter->setResponse($testResponse);

        $zendClient = new \Zend\Http\Client();
        $zendClient->setAdapter($testAdapter);

        $httpClient = Bootstrap::getObjectManager()->create(HttpClientInterface::class, [
            'client' => $zendClient,
        ]);

        $clientFactoryMock = $this->getMockBuilder(HttpClientInterfaceFactory::class)
            ->disableOriginalConstructor()
            ->setMethods(['create'])
            ->getMock();
        $clientFactoryMock
            ->expects($this->any())
            ->method('create')
            ->willReturn($httpClient);
        Bootstrap::getObjectManager()->addSharedInstance($clientFactoryMock, HttpClientInterfaceFactory::class);

        // obtain dispatch through block
        /** @var LayoutInterface $layout */
        $layout = Bootstrap::getObjectManager()->get(LayoutInterface::class);
        /** @var View $block */
        $block = $layout->createBlock(View::class);

        $this->assertNull($block->getDispatch());
    }

    /**
     * @test
     * @magentoAppArea adminhtml
     * @dataProvider getCompletionDataProvider
     * @magentoConfigFixture default/carriers/temando/sovereign_endpoint https://foo.temando.io/v1/
     * @param string $responseBody
     */
    public function dispatchIsLoaded($responseBody)
    {
        $dispatchId = '444cc444-ffff-dddd-eeee-bbbaaddd2000';

        // prepare dispatch view request
        $request = Bootstrap::getObjectManager()->get(\Magento\TestFramework\Request::class);
        $request->setParam('dispatch_id', $dispatchId);

        // prepare api response (dispatch not found)
        $testResponse = new \Zend\Http\Response();
        $testResponse->setStatusCode(\Zend\Http\Response::STATUS_CODE_200);
        $testResponse->setContent($responseBody);

        $testAdapter = new \Zend\Http\Client\Adapter\Test();
        $testAdapter->setResponse($testResponse);

        $zendClient = new \Zend\Http\Client();
        $zendClient->setAdapter($testAdapter);

        $httpClient = Bootstrap::getObjectManager()->create(HttpClientInterface::class, [
            'client' => $zendClient,
        ]);

        $clientFactoryMock = $this->getMockBuilder(HttpClientInterfaceFactory::class)
            ->disableOriginalConstructor()
            ->setMethods(['create'])
            ->getMock();
        $clientFactoryMock
            ->expects($this->any())
            ->method('create')
            ->willReturn($httpClient);
        Bootstrap::getObjectManager()->addSharedInstance($clientFactoryMock, HttpClientInterfaceFactory::class);

        /** @var DispatchRepositoryInterface $dispatchRepository */
        $dispatchRepository = Bootstrap::getObjectManager()->get(DispatchRepositoryInterface::class);
        $dispatch = $dispatchRepository->getById($dispatchId);

        /** @var DispatchProvider $dispatchProvider */
        $dispatchProvider = Bootstrap::getObjectManager()->get(DispatchProvider::class);
        $dispatchProvider->setDispatch($dispatch);

        // obtain dispatch through block
        /** @var LayoutInterface $layout */
        $layout = Bootstrap::getObjectManager()->get(LayoutInterface::class);
        /** @var View $block */
        $block = $layout->createBlock(View::class);

        /** @var DispatchInterface $completion */
        $completion = $block->getDispatch();
        $this->assertInstanceOf(DispatchInterface::class, $completion);
        $this->assertEquals($dispatchId, $completion->getDispatchId());
    }

    /**
     * @test
     * @magentoAppArea adminhtml
     * @magentoConfigFixture default/general/locale/timezone Europe/London
     */
    public function getLocalizedDate()
    {
        /** @var LayoutInterface $layout */
        $layout = Bootstrap::getObjectManager()->get(LayoutInterface::class);
        /** @var View $block */
        $block = $layout->createBlock(View::class);

        $dateTime = $block->getDate('2017-01-01T00:00:01Z');
        $this->assertInstanceOf(\DateTime::class, $dateTime);
        $this->assertEquals('2017-01-01 12:00 am', $dateTime->format('Y-m-d g:i a'));
    }

    /**
     * @test
     * @magentoAppArea adminhtml
     * @magentoConfigFixture default/general/locale/timezone Australia/Brisbane
     */
    public function getLocalizedDateAu()
    {
        /** @var LayoutInterface $layout */
        $layout = Bootstrap::getObjectManager()->get(LayoutInterface::class);
        /** @var View $block */
        $block = $layout->createBlock(View::class);

        $dateTime = $block->getDate('2017-01-01T00:00:01Z');
        $this->assertInstanceOf(\DateTime::class, $dateTime);
        $this->assertEquals('2017-01-01 10:00 am', $dateTime->format('Y-m-d g:i a'));
    }
}