Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SqlCollectorTest.php
Go to the documentation of this file.
1 <?php
8 
12 
16 class SqlCollectorTest extends \PHPUnit\Framework\TestCase
17 {
21  private $unit;
22 
26  private $resourceConnection;
27 
28  protected function setUp()
29  {
30  $this->resourceConnection = $this->getMockBuilder(ResourceConnection::class)
31  ->disableOriginalConstructor()
32  ->getMock();
33  $this->unit = (new ObjectManager($this))->getObject(
34  SqlCollector::class,
35  ['resourceConnection' => $this->resourceConnection]
36  );
37  }
38 
39  public function testGetEmptySql()
40  {
41  $connection = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
42  ->setMethods(['getProfiler'])
43  ->getMockForAbstractClass();
44  $profiler = $this->getMockBuilder(\Zend_Db_Profiler::class)
45  ->disableOriginalConstructor()
46  ->getMock();
47  $connection->expects($this->once())->method('getProfiler')->willReturn($profiler);
48  $this->resourceConnection->expects($this->once())->method('getConnection')->willReturn($connection);
49 
50  $profiler->expects($this->once())->method('getQueryProfiles')->willReturn([]);
51 
52  $this->unit->disable();
53  $this->assertEquals([], $this->unit->getSql());
54  }
55 
57  {
58  $connection = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
59  ->setMethods(['getProfiler'])
60  ->getMockForAbstractClass();
61  $profiler = $this->getMockBuilder(\Zend_Db_Profiler::class)
62  ->disableOriginalConstructor()
63  ->getMock();
64  $connection->expects($this->once())->method('getProfiler')->willReturn($profiler);
65  $this->resourceConnection->expects($this->once())->method('getConnection')->willReturn($connection);
66 
67  $query = $this->getMockBuilder(\Zend_Db_Profiler_Query::class)->disableOriginalConstructor()->getMock();
68  $query->expects($this->exactly(2))->method('getQueryType')->willReturn(\Zend_Db_Profiler::SELECT);
69  $profiler->expects($this->once())->method('getQueryProfiles')->willReturn([$query]);
70 
71  $this->unit->disable();
72  $this->assertEquals([], $this->unit->getSql());
73  }
74 
75  public function testGetSql()
76  {
77  $connection = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
78  ->setMethods(['getProfiler'])
79  ->getMockForAbstractClass();
80  $profiler = $this->getMockBuilder(\Zend_Db_Profiler::class)
81  ->disableOriginalConstructor()
82  ->getMock();
83  $connection->expects($this->once())->method('getProfiler')->willReturn($profiler);
84  $this->resourceConnection->expects($this->once())->method('getConnection')->willReturn($connection);
85 
86  $query = $this->getMockBuilder(\Zend_Db_Profiler_Query::class)->disableOriginalConstructor()->getMock();
87  $query->expects($this->once())->method('getQueryType')->willReturn(\Zend_Db_Profiler::INSERT);
88  $query->expects($this->once())->method('getQuery')->willReturn(
89  'INSERT INTO `catalog_product_entity` (id, sku, type, created_at, attribute_set)'
90  . ' VALUES (?, ?, ?, \'2013-12-11\', ?), (?, ?, ?, \'2013-12-11\', ?)'
91  );
92  $query->expects($this->once())->method('getQueryParams')->willReturn([
93  4, 'sku_4', 'simple', 4, 5, 'sku_5', 'simple', 12
94  ]);
95  $profiler->expects($this->once())->method('getQueryProfiles')->willReturn([$query]);
96 
97  $this->unit->disable();
98  $this->assertEquals(
99  [
100  [
101  [
102  [
103  'id' => 4,
104  'sku' => 'sku_4',
105  'type' => 'simple',
106  'created_at' => '2013-12-11',
107  'attribute_set' => 4,
108  ],
109  [
110  'id' => 5,
111  'sku' => 'sku_5',
112  'type' => 'simple',
113  'created_at' => '2013-12-11',
114  'attribute_set' => 12,
115  ],
116  ],
117  'catalog_product_entity'
118  ]
119  ],
120  $this->unit->getSql()
121  );
122  }
123 }
$connection
Definition: bulk.php:13