public function test_process_withReplace_OneIdentifiedByFieldOnly() { $mockPdoFacade = \Mockery::mock('\\TestDbAcle\\Db\\Mysql\\Pdo\\PdoFacade'); $dataTree = new \TestDbAcle\Psv\PsvTree(); $dataTree->addTable(new \TestDbAcle\Psv\Table\Table('user', array(array("user_id" => "10", "first_name" => "john", "last_name" => "miller"), array("user_id" => "20", "first_name" => "stu", "last_name" => "Smith"), array("user_id" => "30", "first_name" => "stuart", "last_name" => "Smith")), new \TestDbAcle\Psv\Table\Meta(array('mode' => 'replace', 'identifiedBy' => array('first_name'))))); $mockPdoFacade->shouldReceive('recordExists')->once()->with("user", array('first_name' => 'john'))->andReturn(true)->ordered(); $mockPdoFacade->shouldReceive('executeSql')->once()->with("UPDATE `user` SET `user_id`='10', `first_name`='john', `last_name`='miller' WHERE `first_name`='john'")->ordered(); $mockPdoFacade->shouldReceive('recordExists')->once()->with("user", array('first_name' => 'stu'))->andReturn(true)->ordered(); $mockPdoFacade->shouldReceive('executeSql')->once()->with("UPDATE `user` SET `user_id`='20', `first_name`='stu', `last_name`='Smith' WHERE `first_name`='stu'")->ordered(); $mockPdoFacade->shouldReceive('recordExists')->once()->with("user", array('first_name' => 'stuart'))->andReturn(false)->ordered(); $mockPdoFacade->shouldReceive('executeSql')->once()->with("INSERT INTO `user` ( `user_id`, `first_name`, `last_name` ) VALUES ( '30', 'stuart', 'Smith' )")->ordered(); $dataInserter = new \TestDbAcle\Db\DataInserter\DataInserter($mockPdoFacade, new \TestDbAcle\Db\DataInserter\Factory\UpsertBuilderFactory($mockPdoFacade)); $dataInserter->process($dataTree); }
/** * * Parses a psv tree such that a structure such as * [expression1|mode:replace;identifiedBy:first_name,last_name] * id |first_name |last_name * 10 |john |miller * 20 |stu |Smith * * [expression2] * col1 |col2 |col3 * 1 |moo |foo * 30 |miaow |boo * * Into following array * * array("expression1" => array( * 'meta' => array( * 'mode'=> 'replace', * 'identifiedBy' => array('first_name','last_name') * ), * 'data' => array( * array("id" => "10", * "first_name" => "john", * "last_name" => "miller"), * array("id" => "20", * "first_name" => "stu", * "last_name" => "Smith")) * ), * "expression2" => array( * 'meta' => array(), * 'data' => array( * array("col1" => "1", * "col2" => "moo", * "col3" => "foo"), * array("col1" => "30", * "col2" => "miaow", * "col3" => "boo"))), * 'empty' => array('meta'=>array(),'data'=>array()) * ) * * @param string $psvContent the content to be parsed * @return \TestDbAcle\Psv\PsvTree the parsed content */ public function parsePsvTree($psvContent) { $parsedTree = array(); $parsedTree = new \TestDbAcle\Psv\PsvTree(); $contentSplitByOpeningBracket = preg_split('/\\n\\s*(?<!\\\\)\\[/', $psvContent); foreach ($contentSplitByOpeningBracket as $startOfTableContent) { if (trim($startOfTableContent) === '') { continue; } list($actualContentForTable, $expression) = $this->extractExpressionAndContent($startOfTableContent); list($tableName, $meta) = $this->parseIntoTableAndMeta($expression); $parsedTree->addTable(new Table\Table($tableName, $this->parsePsv($actualContentForTable), new Table\Meta($meta))); } return $parsedTree; }
public function execute() { $actualData = array(); $parsedTree = new \TestDbAcle\Psv\PsvTree(); $parsedTree->addTable(new \TestDbAcle\Psv\Table\Table('default', $this->parser->parsePsv($this->sourcePsv))); $filteredParsedTree = $this->filterQueue->filterDataTree($parsedTree); $expectedData = $filteredParsedTree->getTable('default')->toArray(); if (count($expectedData) == 0) { $this->expectedData = $expectedData; $this->actualData = $this->actualDataUnfiltered; return; } $allowedColumns = array_keys($expectedData[0]); foreach ($this->actualDataUnfiltered as $row) { $actualData[] = array_intersect_key($row, array_flip($allowedColumns)); } $this->expectedData = $expectedData; $this->actualData = $actualData; }
function test_filterDataTree_withTwoRowFilters() { $dataTree = new \TestDbAcle\Psv\PsvTree(); $dataTree->addTable(new \TestDbAcle\Psv\Table\Table('user', array(array('user.row1'), array('user.row2')))); $dataTree->addTable(new \TestDbAcle\Psv\Table\Table('stuff', array(array('stuff.row1'), array('stuff.row2')))); $dataTree->addTable(new \TestDbAcle\Psv\Table\Table('emptyTable')); $mockRowFilter1 = \Mockery::mock('\\TestDbAcle\\Filter\\RowFilter'); $mockRowFilter1->shouldReceive('filter')->once()->with('user', array("user.row1"))->ordered()->andReturn(array('user.filtered_filter1_row1')); $mockRowFilter1->shouldReceive('filter')->once()->with('user', array("user.row2"))->ordered()->andReturn(array('user.filtered_filter1_row2')); $mockRowFilter1->shouldReceive('filter')->once()->with('stuff', array("stuff.row1"))->ordered()->andReturn(array('stuff.filtered_filter1_row1')); $mockRowFilter1->shouldReceive('filter')->once()->with('stuff', array("stuff.row2"))->ordered()->andReturn(array('stuff.filtered_filter1_row2')); $mockRowFilter2 = \Mockery::mock('\\TestDbAcle\\Filter\\RowFilter'); $mockRowFilter2->shouldReceive('filter')->once()->with('user', array("user.filtered_filter1_row1"))->ordered()->andReturn(array('user.filtered_filter2_row1')); $mockRowFilter2->shouldReceive('filter')->once()->with('user', array("user.filtered_filter1_row2"))->ordered()->andReturn(array('user.filtered_filter2_row2')); $mockRowFilter2->shouldReceive('filter')->once()->with('stuff', array("stuff.filtered_filter1_row1"))->ordered()->andReturn(array('stuff.filtered_filter2_row1')); $mockRowFilter2->shouldReceive('filter')->once()->with('stuff', array("stuff.filtered_filter1_row2"))->ordered()->andReturn(array('stuff.filtered_filter2_row2')); $this->filterQueue->addRowFilter($mockRowFilter1); $this->filterQueue->addRowFilter($mockRowFilter2); $filteredTableList = $this->filterQueue->filterDataTree($dataTree); $this->assertEquals(array(array('user.filtered_filter2_row1'), array('user.filtered_filter2_row2')), $filteredTableList->getTable('user')->toArray()); $this->assertEquals(array(array('stuff.filtered_filter2_row1'), array('stuff.filtered_filter2_row2')), $filteredTableList->getTable('stuff')->toArray()); $this->assertEquals(array(), $filteredTableList->getTable('emptyTable')->toArray()); }