|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2016 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Directory\Test\Unit\Model\ResourceModel\Region; |
| 7 | + |
| 8 | +use Magento\Directory\Model\ResourceModel\Region\Collection; |
| 9 | +use Magento\Framework\DB\Adapter\Pdo\Mysql; |
| 10 | +use Magento\Framework\DB\Select; |
| 11 | +use Magento\Framework\Model\ResourceModel\Db\AbstractDb; |
| 12 | +use Magento\Framework\Event\ManagerInterface; |
| 13 | +use Magento\Framework\Data\Collection\Db\FetchStrategyInterface; |
| 14 | +use Magento\Framework\Data\Collection\EntityFactory; |
| 15 | +use Magento\Framework\Locale\ResolverInterface; |
| 16 | +use Magento\Framework\DataObject; |
| 17 | +use Psr\Log\LoggerInterface; |
| 18 | + |
| 19 | +class CollectionTest extends \PHPUnit_Framework_TestCase |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var Collection |
| 23 | + */ |
| 24 | + private $collection; |
| 25 | + |
| 26 | + protected function setUp() |
| 27 | + { |
| 28 | + $entityFactoryMock = $this->getMock(EntityFactory::class, [], [], '', false); |
| 29 | + $loggerMock = $this->getMock(LoggerInterface::class); |
| 30 | + $fetchStrategyMock = $this->getMock(FetchStrategyInterface::class); |
| 31 | + $eventManagerMock = $this->getMock(ManagerInterface::class); |
| 32 | + $localeResolverMock = $this->getMock(ResolverInterface::class); |
| 33 | + $connectionMock = $this->getMock(Mysql::class, [], [], '', false); |
| 34 | + $resourceMock = $this->getMockForAbstractClass(AbstractDb::class, |
| 35 | + [], |
| 36 | + '', |
| 37 | + false, |
| 38 | + true, |
| 39 | + true, |
| 40 | + ['getConnection', 'getMainTable', 'getTable', '__wakeup'] |
| 41 | + ); |
| 42 | + |
| 43 | + $selectMock = $this->getMock(Select::class, [], [], '', false); |
| 44 | + $connectionMock->expects($this->any())->method('select')->will($this->returnValue($selectMock)); |
| 45 | + $resourceMock->expects($this->any())->method('getConnection')->will($this->returnValue($connectionMock)); |
| 46 | + $resourceMock->expects($this->any())->method('getTable')->will($this->returnArgument(0)); |
| 47 | + |
| 48 | + $this->collection = new Collection( |
| 49 | + $entityFactoryMock, |
| 50 | + $loggerMock, |
| 51 | + $fetchStrategyMock, |
| 52 | + $eventManagerMock, |
| 53 | + $localeResolverMock, |
| 54 | + $connectionMock, |
| 55 | + $resourceMock |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + public function testToOptionArray() |
| 60 | + { |
| 61 | + $items = [ |
| 62 | + [ |
| 63 | + 'name' => 'Region Name 1', |
| 64 | + 'default_name' => 'Default Region Name 1', |
| 65 | + 'region_id' => 1, |
| 66 | + 'country_id' => 1, |
| 67 | + ], |
| 68 | + [ |
| 69 | + 'name' => 'Region Name 2', |
| 70 | + 'default_name' => 'Default Region Name 2', |
| 71 | + 'region_id' => 2, |
| 72 | + 'country_id' => 1, |
| 73 | + ], |
| 74 | + ]; |
| 75 | + foreach ($items as $itemData) { |
| 76 | + $this->collection->addItem(new DataObject($itemData)); |
| 77 | + } |
| 78 | + |
| 79 | + $expectedResult = [ |
| 80 | + [ |
| 81 | + 'label' => __('Please select a region, state or province.'), |
| 82 | + 'value' => null, |
| 83 | + 'title' => null, |
| 84 | + ], |
| 85 | + [ |
| 86 | + 'value' => 1, |
| 87 | + 'title' => 'Default Region Name 1', |
| 88 | + 'country_id' => 1, |
| 89 | + 'label' => 'Region Name 1', |
| 90 | + ], |
| 91 | + [ |
| 92 | + 'value' => 2, |
| 93 | + 'title' => 'Default Region Name 2', |
| 94 | + 'country_id' => 1, |
| 95 | + 'label' => 'Region Name 2', |
| 96 | + ], |
| 97 | + ]; |
| 98 | + |
| 99 | + $this->assertEquals($expectedResult, $this->collection->toOptionArray()); |
| 100 | + } |
| 101 | +} |
0 commit comments