ClientResolverTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\AdvancedSearch\Test\Unit\Model\Client;
  7. use Magento\AdvancedSearch\Model\Client\ClientFactoryInterface;
  8. use Magento\AdvancedSearch\Model\Client\ClientInterface;
  9. use Magento\AdvancedSearch\Model\Client\ClientOptionsInterface;
  10. use Magento\AdvancedSearch\Model\Client\ClientResolver;
  11. use Magento\Framework\ObjectManagerInterface;
  12. use Magento\Framework\Search\EngineResolverInterface;
  13. class ClientResolverTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @var ClientResolver|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. private $model;
  19. /**
  20. * @var ObjectManagerInterface |\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $objectManager;
  23. /**
  24. * @var EngineResolverInterface|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $engineResolverMock;
  27. protected function setUp()
  28. {
  29. $this->engineResolverMock = $this->getMockBuilder(EngineResolverInterface::class)
  30. ->getMockForAbstractClass();
  31. $this->objectManager = $this->createMock(ObjectManagerInterface::class);
  32. $this->model = new ClientResolver(
  33. $this->objectManager,
  34. ['engineName' => 'engineFactoryClass'],
  35. ['engineName' => 'engineOptionClass'],
  36. $this->engineResolverMock
  37. );
  38. }
  39. public function testCreate()
  40. {
  41. $this->engineResolverMock->expects($this->once())->method('getCurrentSearchEngine')
  42. ->will($this->returnValue('engineName'));
  43. $factoryMock = $this->createMock(ClientFactoryInterface::class);
  44. $clientMock = $this->createMock(ClientInterface::class);
  45. $clientOptionsMock = $this->createMock(ClientOptionsInterface::class);
  46. $this->objectManager->expects($this->exactly(2))->method('create')
  47. ->withConsecutive(
  48. [$this->equalTo('engineFactoryClass')],
  49. [$this->equalTo('engineOptionClass')]
  50. )
  51. ->willReturnOnConsecutiveCalls(
  52. $factoryMock,
  53. $clientOptionsMock
  54. );
  55. $clientOptionsMock->expects($this->once())->method('prepareClientOptions')
  56. ->with([])
  57. ->will($this->returnValue(['parameters']));
  58. $factoryMock->expects($this->once())->method('create')
  59. ->with($this->equalTo(['parameters']))
  60. ->will($this->returnValue($clientMock));
  61. $result = $this->model->create();
  62. $this->assertInstanceOf(ClientInterface::class, $result);
  63. }
  64. /**
  65. * @expectedException \InvalidArgumentException
  66. */
  67. public function testCreateExceptionThrown()
  68. {
  69. $this->objectManager->expects($this->once())->method('create')
  70. ->with($this->equalTo('engineFactoryClass'))
  71. ->will($this->returnValue('t'));
  72. $this->model->create('engineName');
  73. }
  74. /**
  75. * @expectedException LogicException
  76. */
  77. public function testCreateLogicException()
  78. {
  79. $this->model->create('input');
  80. }
  81. public function testGetCurrentEngine()
  82. {
  83. $this->engineResolverMock->expects($this->once())->method('getCurrentSearchEngine')
  84. ->will($this->returnValue('engineName'));
  85. $this->assertEquals('engineName', $this->model->getCurrentEngine());
  86. }
  87. }