public function testAddValueSortToCollection() { $attributeCode = 'attribute_code'; $dir = \Magento\Framework\DB\Select::SQL_ASC; $collection = $this->getMockBuilder('Magento\\Eav\\Model\\Entity\\Collection\\AbstractCollection')->setMethods(['getSelect', 'getStoreId'])->disableOriginalConstructor()->getMockForAbstractClass(); $attribute = $this->getMockBuilder('Magento\\Eav\\Model\\Entity\\Attribute\\AbstractAttribute')->setMethods(['getAttributeCode', 'getEntity', 'getBackend', 'getId'])->disableOriginalConstructor()->getMockForAbstractClass(); $attribute->expects($this->any())->method('getAttributeCode')->willReturn($attributeCode); $entity = $this->getMockBuilder('Magento\\Eav\\Model\\Entity\\AbstractEntity')->setMethods(['getLinkField'])->disableOriginalConstructor()->getMockForAbstractClass(); $attribute->expects($this->once())->method('getEntity')->willReturn($entity); $entity->expects($this->once())->method('getLinkField')->willReturn('entity_id'); $select = $this->getMockBuilder('Magento\\Framework\\DB\\Select')->setMethods(['joinLeft', 'getConnection', 'order'])->disableOriginalConstructor()->getMock(); $collection->expects($this->any())->method('getSelect')->willReturn($select); $select->expects($this->any())->method('joinLeft')->willReturnSelf(); $backend = $this->getMockBuilder('Magento\\Eav\\Model\\Entity\\Attribute\\Backend\\AbstractBackend')->setMethods(['getTable'])->disableOriginalConstructor()->getMockForAbstractClass(); $attribute->expects($this->any())->method('getBackend')->willReturn($backend); $backend->expects($this->any())->method('getTable')->willReturn('table_name'); $attribute->expects($this->any())->method('getId')->willReturn(1); $collection->expects($this->once())->method('getStoreId')->willReturn(1); $connection = $this->getMockBuilder('Magento\\Framework\\DB\\Adapter\\AdapterInterface')->disableOriginalConstructor()->getMock(); $expr = $this->getMockBuilder('Zend_Db_Expr')->disableOriginalConstructor()->getMock(); $connection->expects($this->once())->method('getCheckSql')->willReturn($expr); $select->expects($this->once())->method('getConnection')->willReturn($connection); $attrOption = $this->getMockBuilder('Magento\\Eav\\Model\\ResourceModel\\Entity\\Attribute\\Option')->disableOriginalConstructor()->getMock(); $this->attrOptionFactory->expects($this->once())->method('create')->willReturn($attrOption); $attrOption->expects($this->once())->method('addOptionValueToCollection')->with($collection, $attribute, $expr)->willReturnSelf(); $select->expects($this->once())->method('order')->with("{$attributeCode} {$dir}"); $this->model->setAttribute($attribute); $this->assertEquals($this->model, $this->model->addValueSortToCollection($collection, $dir)); }