UrlRewriteExceptionMessageFactoryTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\UrlRewrite\Test\Unit\Model\Message;
  7. use Magento\Framework\Message\MessageInterface;
  8. use Magento\UrlRewrite\Model\Message\UrlRewriteExceptionMessageFactory;
  9. use Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException;
  10. class UrlRewriteExceptionMessageFactoryTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var \Magento\Framework\Message\Factory | \PHPUnit_Framework_MockObject_MockObject
  14. */
  15. private $messageFactoryMock;
  16. /**
  17. * @var \Magento\Framework\UrlInterface| \PHPUnit_Framework_MockObject_MockObject
  18. */
  19. private $urlMock;
  20. /**
  21. * @var \Magento\UrlRewrite\Model\Message\UrlRewriteExceptionMessageFactory
  22. */
  23. private $urlRewriteExceptionMessageFactory;
  24. protected function setUp()
  25. {
  26. $this->urlMock = $this->createMock(\Magento\Framework\UrlInterface::class);
  27. $this->messageFactoryMock = $this->createPartialMock(
  28. \Magento\Framework\Message\Factory::class,
  29. ['create']
  30. );
  31. $this->urlRewriteExceptionMessageFactory = new UrlRewriteExceptionMessageFactory(
  32. $this->messageFactoryMock,
  33. $this->urlMock
  34. );
  35. }
  36. public function testCreateMessage()
  37. {
  38. $exception = new \Exception('exception');
  39. $urlAlreadyExistsException = new UrlAlreadyExistsException(
  40. __('message'),
  41. $exception,
  42. 0,
  43. [['request_path' => 'url']]
  44. );
  45. $this->urlMock->expects($this->once())
  46. ->method('getUrl')
  47. ->willReturn('htmlUrl');
  48. $message = $this->createMock(MessageInterface::class);
  49. $message->expects($this->once())
  50. ->method('setText')
  51. ->with($urlAlreadyExistsException->getMessage())
  52. ->willReturn($message);
  53. $message->expects($this->once())
  54. ->method('setIdentifier')
  55. ->with(UrlRewriteExceptionMessageFactory::URL_DUPLICATE_MESSAGE_MAP_ID)
  56. ->willReturnSelf();
  57. $message->expects($this->once())
  58. ->method('setData')
  59. ->with(['urls' => ['htmlUrl' => 'url']])
  60. ->willReturn($message);
  61. $this->messageFactoryMock->expects($this->once())
  62. ->method('create')
  63. ->with(MessageInterface::TYPE_ERROR)
  64. ->willReturn($message);
  65. $this->assertEquals(
  66. $message,
  67. $this->urlRewriteExceptionMessageFactory->createMessage($urlAlreadyExistsException)
  68. );
  69. }
  70. /**
  71. * @expectedException \Magento\Framework\Exception\RuntimeException
  72. */
  73. public function testCreateMessageNotFound()
  74. {
  75. $exception = new \Exception('message');
  76. $this->urlRewriteExceptionMessageFactory->createMessage($exception);
  77. }
  78. }