Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DocumentFactoryTest.php
Go to the documentation of this file.
1 <?php
8 
11 
12 class DocumentFactoryTest extends \PHPUnit\Framework\TestCase
13 {
17  private $model;
18 
22  protected $objectManager;
23 
28 
32  protected $entityMetadata;
33 
39  protected $instanceName;
40 
46  protected function setUp()
47  {
48  $this->entityMetadata = $this->getMockBuilder(\Magento\Framework\Search\EntityMetadata::class)
49  ->disableOriginalConstructor()
50  ->getMock();
51 
52  $this->objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
53 
54  $this->instanceName = \Magento\Framework\Api\Search\Document::class;
55 
56  $objectManagerHelper = new ObjectManagerHelper($this);
57  $this->model = $objectManagerHelper->getObject(
58  \Magento\Elasticsearch\SearchAdapter\DocumentFactory::class,
59  [
60  'objectManager' => $this->objectManager,
61  'entityMetadata' => $this->entityMetadata
62  ]
63  );
64  }
65 
69  public function testCreate()
70  {
71  $documents = [
72  '_id' => 2,
73  '_score' => 1.00,
74  '_index' => 'indexName',
75  '_type' => 'product',
76  ];
77 
78  $this->entityMetadata->expects($this->once())
79  ->method('getEntityId')
80  ->willReturn('_id');
81 
82  $result = $this->model->create($documents);
83  $this->assertInstanceOf($this->instanceName, $result);
84  $this->assertEquals($documents['_id'], $result->getId());
85  $this->assertEquals($documents['_score'], $result->getCustomAttribute('score')->getValue());
86  }
87 }