Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MetaTest.php
Go to the documentation of this file.
1 <?php
7 
9 
13 class MetaTest extends \PHPUnit\Framework\TestCase
14 {
18  private $connectionMock;
19 
23  private $dbContext;
24 
28  private $metaFactory;
29 
33  private $meta;
34 
38  private $profile;
39 
43  private $resourceProfile;
44 
48  private $resource;
49 
53  protected $resourceMock;
54 
58  private $select;
59 
63  protected function setUp()
64  {
65  $this->connectionMock = $this->getMockForAbstractClass(
66  \Magento\Framework\DB\Adapter\AdapterInterface::class,
67  [],
68  '',
69  false,
70  false,
71  true,
72  ['query']
73  );
74  $this->dbContext = $this->createMock(\Magento\Framework\Model\ResourceModel\Db\Context::class);
75  $this->metaFactory = $this->createPartialMock(\Magento\SalesSequence\Model\MetaFactory::class, ['create']);
76  $this->resourceProfile = $this->createPartialMock(
77  \Magento\SalesSequence\Model\ResourceModel\Profile::class,
78  ['loadActiveProfile', 'save']
79  );
80  $this->resourceMock = $this->createPartialMock(
81  \Magento\Framework\App\ResourceConnection::class,
82  ['getConnection', 'getTableName']
83  );
84  $this->dbContext->expects($this->once())->method('getResources')->willReturn($this->resourceMock);
85  $this->select = $this->createMock(\Magento\Framework\DB\Select::class);
86  $this->meta = $this->createMock(\Magento\SalesSequence\Model\Meta::class);
87  $this->profile = $this->createMock(\Magento\SalesSequence\Model\Profile::class);
88  $this->resource = new Meta(
89  $this->dbContext,
90  $this->metaFactory,
91  $this->resourceProfile
92  );
93  }
94 
95  public function testLoadBy()
96  {
97  $metaTableName = 'sequence_meta';
98  $metaIdFieldName = 'meta_id';
99  $entityType = 'order';
100  $storeId = 1;
101  $metaId = 1;
102  $metaData = [
103  'meta_id' => 1,
104  'profile_id' => 2
105  ];
106  $this->resourceMock->expects($this->any())
107  ->method('getConnection')
108  ->willReturn($this->connectionMock);
109  $this->resourceMock->expects($this->once())
110  ->method('getTableName')
111  ->willReturn($metaTableName);
112  $this->connectionMock->expects($this->any())->method('select')->willReturn($this->select);
113  $this->select->expects($this->at(0))
114  ->method('from')
115  ->with($metaTableName, [$metaIdFieldName])
116  ->willReturn($this->select);
117  $this->select->expects($this->at(1))
118  ->method('where')
119  ->with('entity_type = :entity_type AND store_id = :store_id')
120  ->willReturn($this->select);
121  $this->connectionMock->expects($this->once())
122  ->method('fetchOne')
123  ->with($this->select, ['entity_type' => $entityType, 'store_id' => $storeId])
124  ->willReturn($metaId);
125  $this->metaFactory->expects($this->once())->method('create')->willReturn($this->meta);
126  $this->stepCheckSaveWithActiveProfile($metaData);
127  $this->meta->expects($this->once())->method('beforeLoad');
128  $this->assertEquals($this->meta, $this->resource->loadByEntityTypeAndStore($entityType, $storeId));
129  }
130 
134  private function stepCheckSaveWithActiveProfile($metaData)
135  {
136  $this->select->expects($this->at(2))
137  ->method('from')
138  ->with('sequence_meta', '*', null)
139  ->willReturn($this->select);
140  $this->connectionMock->expects($this->any())
141  ->method('quoteIdentifier');
142  $this->connectionMock->expects($this->once())->method('fetchRow')->willReturn($metaData);
143  $this->resourceProfile->expects($this->once())->method('loadActiveProfile')->willReturn($this->profile);
144  }
145 }