FactoryTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Message\Test\Unit;
  7. class FactoryTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Framework\Message\Factory
  11. */
  12. protected $factory;
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $objectManagerMock;
  17. protected function setUp()
  18. {
  19. $this->objectManagerMock = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  20. $this->factory = new \Magento\Framework\Message\Factory(
  21. $this->objectManagerMock
  22. );
  23. }
  24. /**
  25. * @expectedException \InvalidArgumentException
  26. * @expectedExceptionMessage Wrong message type
  27. */
  28. public function testCreateWithWrongTypeException()
  29. {
  30. $this->objectManagerMock->expects($this->never())->method('create');
  31. $this->factory->create('type', 'text');
  32. }
  33. public function testCreateWithWrongInterfaceImplementation()
  34. {
  35. $this->expectException('\InvalidArgumentException');
  36. $this->expectExceptionMessage(
  37. 'Magento\Framework\Message\Error doesn\'t implement \Magento\Framework\Message\MessageInterface'
  38. );
  39. $messageMock = new \stdClass();
  40. $type = 'error';
  41. $className = 'Magento\\Framework\\Message\\' . ucfirst($type);
  42. $this->objectManagerMock
  43. ->expects($this->once())
  44. ->method('create')
  45. ->with($className, ['text' => 'text'])
  46. ->will($this->returnValue($messageMock));
  47. $this->factory->create($type, 'text');
  48. }
  49. public function testSuccessfulCreateMessage()
  50. {
  51. $messageMock = $this->createMock(\Magento\Framework\Message\Success::class);
  52. $type = 'success';
  53. $className = 'Magento\\Framework\\Message\\' . ucfirst($type);
  54. $this->objectManagerMock
  55. ->expects($this->once())
  56. ->method('create')
  57. ->with($className, ['text' => 'text'])
  58. ->will($this->returnValue($messageMock));
  59. $this->assertEquals($messageMock, $this->factory->create($type, 'text'));
  60. }
  61. }