CollectionTest.php 3.2 KB

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