|
| 1 | +<?php |
| 2 | +/*** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\Variable\Test\Unit\Model\ResourceModel\Variable; |
| 8 | + |
| 9 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 10 | +use Magento\Framework\DB\Select; |
| 11 | +use Magento\Framework\Model\ResourceModel\Db\AbstractDb; |
| 12 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 13 | +use Magento\Variable\Model\ResourceModel\Variable\Collection; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | + |
| 16 | +/** |
| 17 | + * Provide tests for Variable collection class. |
| 18 | + */ |
| 19 | +class CollectionTest extends TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * Test Collection::addValuesToResult() build correct query. |
| 23 | + * |
| 24 | + * @return void |
| 25 | + */ |
| 26 | + public function testAddValuesToResult() |
| 27 | + { |
| 28 | + $mainTableName = 'testMainTable'; |
| 29 | + $tableName = 'variable_value'; |
| 30 | + $field = 'value_table.store_id'; |
| 31 | + |
| 32 | + $select = $this->getMockBuilder(Select::class) |
| 33 | + ->disableOriginalConstructor() |
| 34 | + ->getMock(); |
| 35 | + $select->expects($this->once()) |
| 36 | + ->method('from') |
| 37 | + ->with($this->identicalTo(['main_table' => $mainTableName])) |
| 38 | + ->willReturnSelf(); |
| 39 | + $select->expects($this->once()) |
| 40 | + ->method('join') |
| 41 | + ->with( |
| 42 | + $this->identicalTo(['value_table' => $tableName]), |
| 43 | + $this->identicalTo('value_table.variable_id = main_table.variable_id'), |
| 44 | + $this->identicalTo(['value_table.plain_value', 'value_table.html_value']) |
| 45 | + )->willReturnSelf(); |
| 46 | + |
| 47 | + $connection = $this->getMockBuilder(AdapterInterface::class) |
| 48 | + ->disableOriginalConstructor() |
| 49 | + ->setMethods(['select', 'prepareSqlCondition', 'quoteIdentifier']) |
| 50 | + ->getMockForAbstractClass(); |
| 51 | + $connection->expects($this->any()) |
| 52 | + ->method('select') |
| 53 | + ->willReturn($select); |
| 54 | + $connection->expects($this->once()) |
| 55 | + ->method('quoteIdentifier') |
| 56 | + ->with($this->identicalTo($field)) |
| 57 | + ->willReturn($field); |
| 58 | + $connection->expects($this->once()) |
| 59 | + ->method('prepareSqlCondition') |
| 60 | + ->with( |
| 61 | + $this->identicalTo($field), |
| 62 | + $this->identicalTo(['eq' => 0]) |
| 63 | + )->willReturn('testResultCondition'); |
| 64 | + |
| 65 | + $resource = $this->getMockBuilder(AbstractDb::class) |
| 66 | + ->setMethods(['getTable', 'getMainTable', 'getConnection']) |
| 67 | + ->disableOriginalConstructor() |
| 68 | + ->getMockForAbstractClass(); |
| 69 | + $resource->expects($this->any()) |
| 70 | + ->method('getConnection') |
| 71 | + ->willReturn($connection); |
| 72 | + $resource->expects($this->once()) |
| 73 | + ->method('getMainTable') |
| 74 | + ->willReturn('testMainTable'); |
| 75 | + $resource->expects($this->exactly(2)) |
| 76 | + ->method('getTable') |
| 77 | + ->withConsecutive( |
| 78 | + [$mainTableName], |
| 79 | + [$tableName] |
| 80 | + )->willReturnOnConsecutiveCalls( |
| 81 | + $mainTableName, |
| 82 | + $tableName |
| 83 | + ); |
| 84 | + |
| 85 | + $objectManager = new ObjectManager($this); |
| 86 | + $collection = $objectManager->getObject( |
| 87 | + Collection::class, |
| 88 | + [ |
| 89 | + 'resource' => $resource, |
| 90 | + ] |
| 91 | + ); |
| 92 | + $this->assertInstanceOf(Collection::class, $collection->addValuesToResult()); |
| 93 | + } |
| 94 | +} |
0 commit comments