AdapterFactoryTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Search\Test\Unit\Model;
  7. use Magento\Framework\ObjectManagerInterface;
  8. use Magento\Framework\Search\EngineResolverInterface;
  9. use Magento\Search\Model\AdapterFactory;
  10. class AdapterFactoryTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * @var AdapterFactory|\PHPUnit_Framework_MockObject_MockObject
  14. */
  15. private $adapterFactory;
  16. /**
  17. * @var ObjectManagerInterface |\PHPUnit_Framework_MockObject_MockObject
  18. */
  19. private $objectManager;
  20. /**
  21. * @var EngineResolverInterface|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. private $engineResolverMock;
  24. protected function setUp()
  25. {
  26. $this->engineResolverMock = $this->getMockBuilder(EngineResolverInterface::class)
  27. ->getMockForAbstractClass();
  28. $this->objectManager = $this->createMock(ObjectManagerInterface::class);
  29. $this->adapterFactory = new AdapterFactory(
  30. $this->objectManager,
  31. ['ClassName' => 'ClassName'],
  32. $this->engineResolverMock
  33. );
  34. }
  35. public function testCreate()
  36. {
  37. $this->engineResolverMock->expects($this->once())->method('getCurrentSearchEngine')
  38. ->will($this->returnValue('ClassName'));
  39. $adapter = $this->getMockBuilder(\Magento\Framework\Search\AdapterInterface::class)
  40. ->disableOriginalConstructor()
  41. ->getMock();
  42. $this->objectManager->expects($this->once())->method('create')
  43. ->with($this->equalTo('ClassName'), $this->equalTo(['input']))
  44. ->will($this->returnValue($adapter));
  45. $result = $this->adapterFactory->create(['input']);
  46. $this->assertInstanceOf(\Magento\Framework\Search\AdapterInterface::class, $result);
  47. }
  48. /**
  49. * @expectedException \InvalidArgumentException
  50. */
  51. public function testCreateExceptionThrown()
  52. {
  53. $this->engineResolverMock->expects($this->once())->method('getCurrentSearchEngine')
  54. ->will($this->returnValue('ClassName'));
  55. $this->objectManager->expects($this->once())->method('create')
  56. ->with($this->equalTo('ClassName'), $this->equalTo(['input']))
  57. ->will($this->returnValue('t'));
  58. $this->adapterFactory->create(['input']);
  59. }
  60. /**
  61. * @expectedException \LogicException
  62. */
  63. public function testCreateLogicException()
  64. {
  65. $this->engineResolverMock->expects($this->once())->method('getCurrentSearchEngine')
  66. ->will($this->returnValue('Class'));
  67. $this->adapterFactory->create(['input']);
  68. }
  69. }