DesignExceptionsTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Test\Unit;
  7. use Magento\Framework\Serialize\Serializer\Json;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  9. class DesignExceptionsTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /** @var \Magento\Framework\View\DesignExceptions */
  12. private $designExceptions;
  13. /** @var ObjectManagerHelper */
  14. private $objectManagerHelper;
  15. /** @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject */
  16. private $scopeConfigMock;
  17. /** @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject */
  18. private $requestMock;
  19. /** @var string */
  20. private $exceptionConfigPath = 'exception_path';
  21. /** @var string */
  22. private $scopeType = 'scope_type';
  23. /** @var Json|\PHPUnit_Framework_MockObject_MockObject */
  24. private $serializerMock;
  25. protected function setUp()
  26. {
  27. $this->scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  28. $this->requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
  29. $this->serializerMock = $this->createMock(Json::class);
  30. $this->objectManagerHelper = new ObjectManagerHelper($this);
  31. $this->designExceptions = $this->objectManagerHelper->getObject(
  32. \Magento\Framework\View\DesignExceptions::class,
  33. [
  34. 'scopeConfig' => $this->scopeConfigMock,
  35. 'exceptionConfigPath' => $this->exceptionConfigPath,
  36. 'scopeType' => $this->scopeType,
  37. 'serializer' => $this->serializerMock,
  38. ]
  39. );
  40. }
  41. /**
  42. * @param string $userAgent
  43. * @param bool $configValue
  44. * @param int $callNum
  45. * @param bool|string $result
  46. * @param array $expressions
  47. * @dataProvider getThemeByRequestDataProvider
  48. */
  49. public function testGetThemeByRequest($userAgent, $configValue, $callNum, $result, $expressions = [])
  50. {
  51. $this->requestMock->expects($this->once())
  52. ->method('getServer')
  53. ->with($this->equalTo('HTTP_USER_AGENT'))
  54. ->will($this->returnValue($userAgent));
  55. if ($userAgent) {
  56. $this->scopeConfigMock->expects($this->once())
  57. ->method('getValue')
  58. ->with($this->equalTo($this->exceptionConfigPath), $this->equalTo($this->scopeType))
  59. ->will($this->returnValue($configValue));
  60. }
  61. $this->serializerMock->expects($this->exactly($callNum))
  62. ->method('unserialize')
  63. ->with($configValue)
  64. ->willReturn($expressions);
  65. $this->assertSame($result, $this->designExceptions->getThemeByRequest($this->requestMock));
  66. }
  67. /**
  68. * @return array
  69. */
  70. public function getThemeByRequestDataProvider()
  71. {
  72. return [
  73. [false, null, 0, false],
  74. ['iphone', null, 0, false],
  75. ['iphone', 'serializedExpressions1', 1, false],
  76. ['iphone', 'serializedExpressions2', 1, 'matched', [['regexp' => '/iphone/', 'value' => 'matched']]],
  77. ['explorer', 'serializedExpressions3', 1, false, [['regexp' => '/iphone/', 'value' => 'matched']]],
  78. ];
  79. }
  80. }