SynonymAnalyzerTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Search\Test\Unit\Model;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  9. /**
  10. * Class SynonymAnalyzerTest
  11. */
  12. class SynonymAnalyzerTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var \Magento\Search\Model\SynonymAnalyzer
  16. */
  17. private $synonymAnalyzer;
  18. /**
  19. * @var \Magento\Search\Model\SynonymReader |\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $synReaderModel;
  22. /**
  23. * Test set up
  24. */
  25. protected function setUp()
  26. {
  27. $helper = new ObjectManager($this);
  28. $this->synReaderModel = $this->getMockBuilder(\Magento\Search\Model\SynonymReader::class)
  29. ->disableOriginalConstructor()
  30. ->getMock();
  31. $this->synonymAnalyzer = $helper->getObject(
  32. \Magento\Search\Model\SynonymAnalyzer::class,
  33. [
  34. 'synReader' => $this->synReaderModel,
  35. ]
  36. );
  37. }
  38. /**
  39. * @test
  40. */
  41. public function testGetSynonymsForPhrase()
  42. {
  43. $phrase = 'Elizabeth is the british queen';
  44. $expected = [
  45. 0 => [ 0 => "Elizabeth" ],
  46. 1 => [ 0 => "is" ],
  47. 2 => [ 0 => "the" ],
  48. 3 => [ 0 => "british", 1 => "english" ],
  49. 4 => [ 0 => "queen", 1 => "monarch" ],
  50. ];
  51. $this->synReaderModel->expects($this->once())
  52. ->method('loadByPhrase')
  53. ->with($phrase)
  54. ->willReturnSelf()
  55. ;
  56. $this->synReaderModel->expects($this->once())
  57. ->method('getData')
  58. ->willReturn([
  59. ['synonyms' => 'british,english'],
  60. ['synonyms' => 'queen,monarch'],
  61. ])
  62. ;
  63. $actual = $this->synonymAnalyzer->getSynonymsForPhrase($phrase);
  64. $this->assertEquals($expected, $actual);
  65. }
  66. /**
  67. * @test
  68. *
  69. * Empty phrase scenario
  70. */
  71. public function testGetSynonymsForPhraseEmptyPhrase()
  72. {
  73. $phrase = '';
  74. $expected = [];
  75. $actual = $this->synonymAnalyzer->getSynonymsForPhrase($phrase);
  76. $this->assertEquals($expected, $actual);
  77. }
  78. }