public function getWhereFromApiCriteria(\Magento\Framework\Api\Search\SearchCriteriaInterface $criteria, \Praxigento\Core\Repo\Query\Criteria\IMapper $mapper = null) { $result = ''; $filterGroups = $criteria->getFilterGroups(); foreach ($filterGroups as $filterGroup) { $processed = []; // I don't know what is it "filter group" so uniquelize conditions inside one group only /** @var \Magento\Framework\Api\Filter $item */ foreach ($filterGroup->getFilters() as $item) { $field = $item->getField(); if ($mapper) { $field = $mapper->get($field); } $cond = $item->getConditionType(); $value = $item->getValue(); $where = $this->_conn->prepareSqlCondition($field, [$cond => $value]); if (!in_array($where, $processed)) { $result .= "({$where}) AND "; $processed[] = $where; } } } $result .= '1'; return $result; }
public function testResetSearchResult() { $this->resource->expects($this->once())->method('getTableName')->with('search_query', 'core_read')->willReturn('table_name_search_query'); $this->adapter->expects($this->once())->method('update')->with('table_name_search_query', ['is_processed' => 0], ['is_processed != 0'])->willReturn(10); $result = $this->target->resetSearchResults(); $this->assertEquals($this->target, $result); }
/** * @return void */ public function testDeleteRecordsOlderThen() { $timestamp = 12345; $this->resourceMock->expects($this->once())->method('getConnection')->willReturn($this->dbAdapterMock); $this->dbAdapterMock->expects($this->once())->method('delete')->with($this->model->getMainTable(), ['created_at < ?' => $this->dateTimeMock->formatDate($timestamp)])->willReturnSelf(); $this->assertEquals($this->model, $this->model->deleteRecordsOlderThen($timestamp)); }
/** * @magentoDataFixture Magento/Bundle/_files/product.php * @covers \Magento\Indexer\Model\Indexer::reindexAll * @covers \Magento\Bundle\Model\Product\Type::getSearchableData */ public function testPrepareProductIndexForBundleProduct() { $this->indexer->reindexAll(); $select = $this->connectionMock->select()->from($this->resource->getTableName('catalogsearch_fulltext_scope1'))->where('`data_index` LIKE ?', '%' . 'Bundle Product Items' . '%'); $result = $this->connectionMock->fetchAll($select); $this->assertCount(1, $result); }
public function runQuery($query) { $this->addQueryToLog($query); $this->connection->query($query); $this->connection->resetDdlCache(); return $this; }
protected function setUp() { $objectManager = new ObjectManager($this); $this->connection = $this->getMockBuilder('\\Magento\\Framework\\DB\\Adapter\\AdapterInterface')->disableOriginalConstructor()->getMock(); $this->connection->expects($this->any())->method('quoteInto')->willReturnCallback(function ($query, $expression) { return str_replace('?', $expression, $query); }); $this->resource = $this->getMockBuilder('\\Magento\\Framework\\App\\ResourceConnection')->disableOriginalConstructor()->getMock(); $this->resource->method('getTableName')->willReturnCallback(function ($table) { return 'prefix_' . $table; }); $this->resource->expects($this->any())->method('getConnection')->willReturn($this->connection); $this->website = $this->getMockBuilder('\\Magento\\Store\\Api\\Data\\WebsiteInterface')->disableOriginalConstructor()->getMockForAbstractClass(); $this->website->expects($this->any())->method('getId')->willReturn(self::WEBSITE_ID); $this->store = $this->getMockBuilder('\\Magento\\Store\\Api\\Data\\StoreInterface')->disableOriginalConstructor()->getMockForAbstractClass(); $this->store->expects($this->any())->method('getId')->willReturn(self::STORE_ID); $this->storeManager = $this->getMockBuilder('\\Magento\\Store\\Model\\StoreManagerInterface')->disableOriginalConstructor()->getMock(); $this->storeManager->expects($this->any())->method('getWebsite')->willReturn($this->website); $this->storeManager->expects($this->any())->method('getStore')->willReturn($this->store); $this->attributeCollection = $this->getMockBuilder('\\Magento\\Catalog\\Model\\ResourceModel\\Product\\Attribute\\Collection')->disableOriginalConstructor()->getMock(); $attributeCollectionFactory = $this->getMockBuilder('\\Magento\\Catalog\\Model\\ResourceModel\\Product\\Attribute\\CollectionFactory')->setMethods(['create'])->disableOriginalConstructor()->getMock(); $attributeCollectionFactory->expects($this->once())->method('create')->willReturn($this->attributeCollection); $this->target = $objectManager->getObject('\\Magento\\CatalogSearch\\Model\\Search\\TableMapper', ['resource' => $this->resource, 'storeManager' => $this->storeManager, 'attributeCollectionFactory' => $attributeCollectionFactory]); $this->select = $this->getMockBuilder('\\Magento\\Framework\\DB\\Select')->disableOriginalConstructor()->getMock(); $this->request = $this->getMockBuilder('\\Magento\\Framework\\Search\\RequestInterface')->disableOriginalConstructor()->getMock(); }
/** * Restore max_heap_table_size value * * @throws \RuntimeException * @return void */ public function restore() { if (null === $this->currentMaxHeapTableSize) { throw new \RuntimeException('max_heap_table_size parameter is not set'); } $this->connection->query('SET SESSION max_heap_table_size = ' . $this->currentMaxHeapTableSize); }
/** * @dataProvider saveEntityIndexesDataProvider */ public function testSaveEntityIndexes($storeId, $entityIndexes, $expected) { if ($expected) { $this->connection->expects($this->once())->method('insertOnDuplicate')->with(null, $expected, ['data_index'])->willReturnSelf(); } $this->target->saveEntityIndexes($storeId, $entityIndexes); }
public function testResetSearchResult() { $this->resource->expects($this->once())->method('getTableName')->with('search_query', ResourceConnection::DEFAULT_CONNECTION)->willReturn('table_name_search_query'); $this->connection->expects($this->once())->method('update')->with('table_name_search_query', ['is_processed' => 0], ['is_processed != 0'])->willReturn(10); $result = $this->target->resetSearchResults(); $this->assertEquals($this->target, $result); }
/** * @param $inputData * @param $tableData * @param $preparedData * @param $finalData * @dataProvider executeDataProvider */ public function testExecute($inputData, $tableData, $preparedData, $finalData) { $this->connection->expects($this->any())->method('describeTable')->with('entity_table')->willReturn($tableData); $this->connection->expects($this->once())->method('insert')->with('entity_table', $preparedData); $actualData = $this->subject->execute('Test\\Entity\\Type', $inputData); $this->assertEquals($finalData, $actualData); }
/** * @dataProvider dataForGetStoreOptionValues */ public function testGetStoreOptionValues($values) { $this->block->expects($this->once())->method('getData')->with('store_option_values_1')->willReturn($values); if ($values === null) { $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $option = $this->getMock('\\Magento\\Eav\\Model\\ResourceModel\\Entity\\Attribute\\Option', ['getId', 'getValue', 'getLabel'], [], '', false); $attrOptionCollectionMock = $objectManager->getCollectionMock('\\Magento\\Eav\\Model\\ResourceModel\\Entity\\Attribute\\Option\\Collection', [$option, $option]); $this->attrOptionCollectionFactoryMock->expects($this->once())->method('create')->willReturn($attrOptionCollectionMock); $attribute = $this->getMock('\\Magento\\Eav\\Model\\ResourceModel\\Entity\\Attribute', ['getId'], [], '', false); $attribute->expects($this->once())->method('getId')->willReturn(23); $this->registryMock->expects($this->once())->method('registry')->with('entity_attribute')->willReturn($attribute); $attrOptionCollectionMock->expects($this->once())->method('setAttributeFilter')->with(23)->will($this->returnSelf()); $this->connectionMock->expects($this->any())->method('quoteInto')->willReturn('quoted_string_with_value'); $attrOptionCollectionMock->expects($this->any())->method('getConnection')->willReturn($this->connectionMock); $zendDbSelectMock = $this->getMock('Magento\\Framework\\DB\\Select', [], [], '', false); $attrOptionCollectionMock->expects($this->any())->method('getSelect')->willReturn($zendDbSelectMock); $zendDbSelectMock->expects($this->any())->method('joinLeft')->willReturnSelf(); $option->expects($this->at(0))->method('getId')->willReturn(14); $option->expects($this->at(1))->method('getValue')->willReturn('Blue'); $option->expects($this->at(2))->method('getId')->willReturn(14); $option->expects($this->at(3))->method('getLabel')->willReturn('#0000FF'); $option->expects($this->at(4))->method('getId')->willReturn(15); $option->expects($this->at(5))->method('getValue')->willReturn('Black'); $option->expects($this->at(6))->method('getId')->willReturn(15); $option->expects($this->at(7))->method('getLabel')->willReturn('#000000'); $values = [14 => 'Blue', 'swatch' => [14 => '#0000FF', 15 => '#000000'], 15 => 'Black']; } $result = $this->block->getStoreOptionValues(1); $this->assertEquals($result, $values); }
/** * @param int $storeId * @param bool $withAdmin * @param array $condition * @dataProvider dataProviderForTestAddStoreFilterIfStoreIsInt * @covers \Magento\UrlRewrite\Model\ResourceModel\UrlRewriteCollection */ public function testAddStoreFilterIfStoreIsInt($storeId, $withAdmin, $condition) { $store = $this->getMock('Magento\\Store\\Model\\Store', [], [], '', false); $store->expects($this->once())->method('getId')->will($this->returnValue($storeId)); $this->storeManager->expects($this->once())->method('getStore')->will($this->returnValue($store)); $this->connectionMock->expects($this->once())->method('prepareSqlCondition')->with('store_id', ['in' => $condition]); $this->collection->addStoreFilter($storeId, $withAdmin); }
/** * @param AdapterInterface $dbAdapter * * @return Select|string */ public function getIdsSelect($dbAdapter) { if ($this->getTable() && $this->getPkFieldName()) { $select = $dbAdapter->select()->from($this->getTable(), $this->getPkFieldName()); return $select; } return ''; }
/** * @param string $field * @param string $value * @param string $expectedResult * @dataProvider buildQueryDataProvider */ public function testBuildQuery($field, $value, $expectedResult) { $this->requestFilter->expects($this->once())->method('getField')->will($this->returnValue($field)); $this->requestFilter->expects($this->once())->method('getValue')->will($this->returnValue($value)); $this->adapter->expects($this->once())->method('quote')->will($this->returnArgument(0)); $actualResult = $this->filter->buildFilter($this->requestFilter); $this->assertEquals($expectedResult, $actualResult); }
/** * @param string $flatTable * @param bool $isFlatTableExists * @param string $flatDropName * @param string $temporaryFlatTableName * @param array $expectedRenameTablesArgument * @dataProvider moveDataProvider */ public function testMove($flatTable, $isFlatTableExists, $flatDropName, $temporaryFlatTableName, $expectedRenameTablesArgument) { $this->_connectionMock->expects($this->exactly(2))->method('dropTable')->with($flatDropName); $this->_connectionMock->expects($this->once())->method('isTableExists')->with($flatTable)->will($this->returnValue($isFlatTableExists)); $this->_connectionMock->expects($this->once())->method('renameTablesBatch')->with($expectedRenameTablesArgument); $this->_resourceMock->expects($this->any())->method('getConnection')->with('write')->will($this->returnValue($this->_connectionMock)); $model = $this->_objectManager->getObject('Magento\\Catalog\\Model\\Indexer\\Product\\Flat\\TableData', array('resource' => $this->_resourceMock)); $model->move($flatTable, $flatDropName, $temporaryFlatTableName); }
public function testSelectByCompositeKey() { $selectMock = $this->getMock('Magento\\Framework\\DB\\Select', [], [], '', false); $selectMock->expects($this->once())->method('from')->will($this->returnValue($selectMock)); $selectMock->expects($this->exactly(2))->method('where')->will($this->returnValue($selectMock)); $this->connectionMock->expects($this->once())->method('select')->willReturn($selectMock); $this->connectionMock->expects($this->once())->method('fetchRow'); $this->nonceResource->selectByCompositeKey('nonce', 5); }
/** * @expectedException \Magento\Setup\Exception * @expectedExceptionMessage Sorry, but we support MySQL version */ public function testCheckDatabaseConnectionIncompatible() { $this->connection ->expects($this->once()) ->method('fetchOne') ->with('SELECT version()') ->willReturn('5.5.40-0ubuntu0.12.04.1'); $this->dbValidator->checkDatabaseConnection('name', 'host', 'user', 'password'); }
/** * @expectedException \Exception */ public function testDeleteRaiseException() { $this->actionValidatorMock->expects($this->any())->method('isAllowed')->will($this->returnValue(true)); $this->adapterMock->expects($this->once())->method('beginTransaction'); $this->resourceMock->expects($this->once())->method('delete')->will($this->throwException(new \Exception())); $this->resourceMock->expects($this->never())->method('commit'); $this->resourceMock->expects($this->once())->method('rollBack'); $this->model->delete(); }
public function testGetFkName() { $tableName = 'table'; $refTable = 'ref_table'; $columnName = 'columnName'; $refColumnName = 'refColumnName'; $this->connection->expects($this->once())->method('getForeignKeyName')->with($tableName, $columnName, $refTable, $refColumnName)->will($this->returnValue('fkName')); $this->assertEquals('fkName', $this->setup->getFkName($tableName, $columnName, $refTable, $refColumnName)); }
/** * Check that entity has overridden url key for specific store * * @param int $storeId * @param int $entityId * @param string $entityType * @throws \InvalidArgumentException * @return bool */ public function doesEntityHaveOverriddenUrlKeyForStore($storeId, $entityId, $entityType) { $attribute = $this->eavConfig->getAttribute($entityType, 'url_key'); if (!$attribute) { throw new \InvalidArgumentException(sprintf('Cannot retrieve attribute for entity type "%s"', $entityType)); } $select = $this->connection->select()->from($attribute->getBackendTable(), 'store_id')->where('attribute_id = ?', $attribute->getId())->where('entity_id = ?', $entityId); return in_array($storeId, $this->connection->fetchCol($select)); }
/** * Method for FULLTEXT search in Mysql, will generated MATCH ($columns) AGAINST ('$expression' $mode) * * @param string|string[] $columns Columns which add to MATCH () * @param string $expression Expression which add to AGAINST () * @param string $mode * @return string */ public function getMatchQuery($columns, $expression, $mode = self::FULLTEXT_MODE_NATURAL) { if (is_array($columns)) { $columns = implode(', ', $columns); } $expression = $this->connection->quote($expression); $condition = self::MATCH . " ({$columns}) " . self::AGAINST . " ({$expression} {$mode})"; return $condition; }
public function testSelectTokenByCustomerId() { $selectMock = $this->getMock('Magento\\Framework\\DB\\Select', [], [], '', false); $selectMock->expects($this->once())->method('from')->will($this->returnValue($selectMock)); $selectMock->expects($this->exactly(2))->method('where')->will($this->returnValue($selectMock)); $this->connectionMock->expects($this->once())->method('select')->willReturn($selectMock); $this->connectionMock->expects($this->once())->method('fetchRow'); $this->tokenResource->selectTokenByCustomerId(5); }
/** * @magentoDataFixture Magento/Review/_files/customer_review_with_rating.php */ public function testAggregate() { $rating = $this->reviewCollection->getFirstItem(); $this->reviewResource->aggregate($rating); $select = $this->adapter->select()->from($this->resource->getTableName('review_entity_summary')); $result = $this->adapter->fetchRow($select); $this->assertEquals(1, $result['reviews_count']); $this->assertEquals(40, $result['rating_summary']); }
/** * @param Dimension $dimension * @param AdapterInterface $adapter * @return string */ private function generateExpression(Dimension $dimension, AdapterInterface $adapter) { $identifier = $dimension->getName(); $value = $dimension->getValue(); if (self::DEFAULT_DIMENSION_NAME === $identifier) { $identifier = self::STORE_FIELD_NAME; $value = $this->scopeResolver->getScope($value)->getId(); } return sprintf('%s = %s', $adapter->quoteIdentifier($identifier), $adapter->quote($value)); }
public function testQuery() { $selectResult = ['documents' => [['product_id' => 1, 'sku' => 'Product']], 'aggregations' => ['aggregation_name' => ['aggregation1' => [1, 3], 'aggregation2' => [2, 4]]]]; $this->connectionAdapter->expects($this->at(0))->method('fetchAssoc')->will($this->returnValue($selectResult['documents'])); $this->mapper->expects($this->once())->method('buildQuery')->with($this->request)->will($this->returnValue($this->select)); $this->responseFactory->expects($this->once())->method('create')->with($selectResult)->will($this->returnArgument(0)); $this->aggregatioBuilder->expects($this->once())->method('build')->willReturn($selectResult['aggregations']); $response = $this->adapter->query($this->request); $this->assertEquals($selectResult, $response); }
/** * Checks database privileges * * @param \Magento\Framework\DB\Adapter\AdapterInterface $connection * @param string $dbName * @return bool * @throws \Magento\Setup\Exception */ private function checkDatabasePrivileges(\Magento\Framework\DB\Adapter\AdapterInterface $connection, $dbName) { $grantInfo = $connection->query('SHOW GRANTS FOR current_user()')->fetchAll(\PDO::FETCH_NUM); foreach ($grantInfo as $grantRow) { if (preg_match('/(ALL|ALL\\sPRIVILEGES)\\sON\\s[^a-zA-Z\\d\\s]?(\\*|' . $dbName . ')/', $grantRow[0]) === 1) { return true; } } throw new \Magento\Setup\Exception('Database user does not have enough privileges.'); }
public function testSaveNumResults() { /** @var \Magento\Search\Model\Query|\PHPUnit_Framework_MockObject_MockObject $model */ $model = $this->getMockBuilder('Magento\\Search\\Model\\Query')->disableOriginalConstructor()->getMock(); $model->expects($this->any())->method('getStoreId')->willReturn(1); $model->expects($this->any())->method('getQueryText')->willReturn('queryText'); $model->expects($this->any())->method('getNumResults')->willReturn(30); $this->adapter->expects($this->once())->method('insertOnDuplicate'); $this->model->saveNumResults($model); }
public function testAddOrdersFilter() { $this->resourceConnectionMock->expects($this->exactly(2))->method('getConnection')->willReturn($this->connectionMock); $this->resourceConnectionMock->expects($this->once())->method('getTableName')->with('paypal_billing_agreement_order')->willReturn('pref_paypal_billing_agreement_order'); $this->connectionMock->expects($this->once())->method('select')->willReturn($this->selectMock); $this->selectMock->expects($this->once())->method('from')->with(['pbao' => 'pref_paypal_billing_agreement_order'], ['order_id'], null)->willReturnSelf(); $this->selectMock->expects($this->exactly(2))->method('where')->withConsecutive(['pbao.agreement_id IN(?)', [100]], ['main_table.entity_id IN (?)', [500]])->willReturnSelf(); $this->connectionMock->expects($this->once())->method('fetchCol')->with($this->selectMock, [])->willReturn([500]); $this->collectionMock->expects($this->once())->method('getSelect')->willReturn($this->selectMock); $this->assertEquals($this->agreementResource, $this->agreementResource->addOrdersFilter($this->collectionMock, 100)); }
public function testGetCILike() { $field = 'field'; $value = 'value'; $options = []; $this->_adapterMock->expects($this->once())->method('quoteIdentifier')->with($field)->will($this->returnArgument(0)); $this->_model->expects($this->once())->method('addLikeEscape')->with($value, $options)->will($this->returnArgument(0)); $result = $this->_model->getCILike($field, $value, $options); $this->assertInstanceOf('Zend_Db_Expr', $result); $this->assertEquals($field . ' LIKE ' . $value, (string) $result); }
/** * @param string $field * @param string $from * @param string $to * @param string $expectedResult * @dataProvider buildQueryDataProvider */ public function testBuildQuery($field, $from, $to, $expectedResult) { $this->requestFilter->expects($this->once())->method('getField')->will($this->returnValue($field)); $this->requestFilter->expects($this->once())->method('getFrom')->will($this->returnValue($from)); $this->requestFilter->expects($this->once())->method('getTo')->will($this->returnValue($to)); $this->adapter->expects($this->any())->method('quote')->will($this->returnCallback(function ($value) { return '\'' . $value . '\''; })); $actualResult = $this->filter->buildFilter($this->requestFilter); $this->assertEquals($expectedResult, $actualResult); }