Last active
May 2, 2017 19:44
-
-
Save real34/d117d1df536d61d0b544249a38b6b5a1 to your computer and use it in GitHub Desktop.
Magento2 Fakes implementation built step by step to support our tests. (Context: https://mobile.twitter.com/fschmengler/status/859473111984590848)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Terrang\Fluxs\Test\Fake\Repository; | |
use Magento\Framework\Exception\NoSuchEntityException; | |
class FakeAddressRepository implements \Magento\Customer\Api\AddressRepositoryInterface | |
{ | |
private $db = []; | |
public function save(\Magento\Customer\Api\Data\AddressInterface $address) | |
{ | |
$this->db[$address->getId()] = $address; | |
} | |
public function getById($addressId) | |
{ | |
if (!array_key_exists($addressId, $this->db)) { | |
throw NoSuchEntityException::singleField('addressId', $addressId); | |
} | |
return $this->db[$addressId]; | |
} | |
public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria) | |
{ | |
// TODO: Implement search filters | |
return $this->db; | |
} | |
public function delete(\Magento\Customer\Api\Data\AddressInterface $address) | |
{ | |
return $this->deleteById($address->getId()); | |
} | |
public function deleteById($addressId) | |
{ | |
$found = false; | |
if (array_key_exists($addressId, $this->db)) { | |
$found = true; | |
unset($this->db[$addressId]); | |
} | |
return $found; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Terrang\Fluxs\Test\Fake; | |
class FakeCollection extends FakeDb | |
{ | |
public function __construct() { | |
parent::__construct(); | |
} | |
public function getTable($table) {} | |
public function setEntity($entity) {} | |
public function getEntity() {} | |
public function getResource() {} | |
public function setObject($object = null) { | |
return $this; | |
} | |
public function addItem(\Magento\Framework\DataObject $object) { | |
return parent::_addItem($object); | |
} | |
public function getAttribute($attributeCode) {} | |
public function addAttributeToFilter($attribute, $condition = null, $joinType = 'inner') { | |
$this->addFieldToFilter($attribute, $condition); | |
return $this; | |
} | |
public function addFieldToFilter($attribute, $condition = null) { | |
$this->_filters[] = compact('attribute', 'condition'); | |
return $this; | |
} | |
public function addAttributeToSort($attribute, $dir = self::SORT_ORDER_ASC) { | |
return $this; | |
} | |
public function addAttributeToSelect($attribute, $joinType = false) { | |
return $this; | |
} | |
public function addEntityTypeToSelect($entityType, $prefix) { | |
return $this; | |
} | |
public function addStaticField($field) { | |
return $this; | |
} | |
public function addExpressionAttributeToSelect($alias, $expression, $attribute) { | |
return $this; | |
} | |
public function groupByAttribute($attribute) { | |
return $this; | |
} | |
public function joinAttribute($alias, $attribute, $bind, $filter = null, $joinType = 'inner', $storeId = null) { | |
return $this; | |
} | |
public function joinField($alias, $table, $field, $bind, $cond = null, $joinType = 'inner') { | |
return $this; | |
} | |
public function joinTable($table, $bind, $fields = null, $cond = null, $joinType = 'inner') { | |
return $this; | |
} | |
public function removeAttributeToSelect($attribute = null) { | |
return $this; | |
} | |
public function setPage($pageNum, $pageSize) { | |
$this->_curPage = $pageNum; | |
$this->_pageSize = $pageSize; | |
return $this; | |
} | |
public function load($printQuery = false, $logQuery = false) { | |
parent::load(); | |
if ($this->_pageSize) { | |
$startIndex = $this->_pageSize * ($this->_curPage - 1); | |
$this->_items = array_slice($this->_items, $startIndex, $this->_pageSize); | |
} | |
return $this; | |
} | |
public function getAllIds($limit = null, $offset = null) {} | |
public function getAllIdsSql() {} | |
public function save() { | |
return $this; | |
} | |
public function delete() { | |
return $this; | |
} | |
public function exportToArray() { | |
return $this->_items; | |
} | |
public function getRowIdFieldName() {} | |
public function getIdFieldName() {} | |
public function setRowIdFieldName($fieldName) { | |
return $this; | |
} | |
public function _loadEntities($printQuery = false, $logQuery = false) { | |
return $this; | |
} | |
public function _loadAttributes($printQuery = false, $logQuery = false) { | |
return $this; | |
} | |
public function setOrder($attribute, $dir = self::SORT_ORDER_ASC) { | |
return $this; | |
} | |
public function toArray($arrAttributes = []) { | |
return parent::toArray($arrAttributes); | |
} | |
public function getLoadedIds() {} | |
public function clear() { | |
return parent::clear(); | |
} | |
public function removeAllItems() { | |
return $this; | |
} | |
public function removeItemByKey($key) { | |
return $this; | |
} | |
public function getMainTable() {} | |
public function addFieldToSelect($field, $alias = null) { | |
return $this; | |
} | |
public function removeFieldFromSelect($field) { | |
return $this; | |
} | |
public function removeAllFieldsFromSelect() { | |
return $this; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Terrang\Fluxs\Test\Fake; | |
use Magento\Framework\Api\ExtensionAttribute\JoinDataInterface; | |
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface; | |
/** | |
* Overrides all public method of the AbstractDB | |
* to provide a simple in memory implementation | |
* for use in tests | |
* | |
* @package Terrang\Fluxs\Test\Fake | |
*/ | |
class FakeDb extends \Magento\Framework\Data\Collection\AbstractDb | |
{ | |
/** | |
* In memory database content | |
* Almost the same than _items but in case of pagination items will | |
* be a slice of it | |
* | |
* @var array | |
*/ | |
protected $__db; | |
public function __construct() { | |
$this->populateWith([]); | |
} | |
public function populateWith(array $items) | |
{ | |
$this->__db = $items; | |
$this->_items = $items; | |
} | |
public function getResource() {} | |
public function addBindParam($name, $value) {} | |
public function getIdFieldName() {} | |
public function setConnection(\Magento\Framework\DB\Adapter\AdapterInterface $conn) {} | |
public function getSelect() {} | |
public function getConnection() {} | |
public function getSize() { | |
return count($this->_items); | |
} | |
public function getSelectCountSql() {} | |
public function getSelectSql($stringMode = false) {} | |
public function setOrder($field, $direction = self::SORT_ORDER_DESC) {} | |
public function addOrder($field, $direction = self::SORT_ORDER_DESC) {} | |
public function unshiftOrder($field, $direction = self::SORT_ORDER_DESC) {} | |
public function addFieldToFilter($field, $condition = null) {} | |
public function distinct($flag) {} | |
public function load($printQuery = false, $logQuery = false) { | |
$this->_items = array_reduce($this->_filters, [$this, 'applyFilter'], $this->__db); | |
return $this; | |
} | |
public function loadWithFilter($printQuery = false, $logQuery = false) {} | |
public function fetchItem() {} | |
public function getData() {} | |
public function resetData() {} | |
public function loadData($printQuery = false, $logQuery = false) {} | |
public function printLogQuery($printQuery = false, $logQuery = false, $sql = null) {} | |
public function addFilterToMap($filter, $alias, $group = 'fields') {} | |
public function __clone() {} | |
public function joinExtensionAttribute(JoinDataInterface $join, JoinProcessorInterface $extensionAttributesJoinProcessor) {} | |
public function getItemObjectClass() {} | |
public function __sleep() {} | |
public function __wakeup() {} | |
/** INTERNAL IMPLEMENTATIONS (private API) */ | |
private function applyFilter(array $data, array $filter) | |
{ | |
$conditionMatcher = $this->makeConditionMatcher($filter['condition']); | |
$attribute = $filter['attribute']; | |
return array_values(array_filter($data, function (\Magento\Framework\DataObject $row) use ($attribute, $conditionMatcher) { | |
return $conditionMatcher($row->getData($attribute)); | |
})); | |
} | |
private function makeConditionMatcher($condition) | |
{ | |
$trueMatcher = function ($value) { | |
return true; | |
}; | |
$inMatcher = function ($allowedValues) { | |
return function($value) use ($allowedValues) { | |
return in_array($value, $allowedValues); | |
}; | |
}; | |
if (!empty($condition['in'])) { | |
return $inMatcher($condition['in']); | |
} | |
return $trueMatcher; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment