Included vendor/ to the project

This commit is contained in:
2017-09-13 09:35:32 +07:00
parent e72205ff09
commit 17f1643875
2483 changed files with 179073 additions and 0 deletions

View File

@@ -0,0 +1,305 @@
<?php
/*
* This file is part of DbUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\DbUnit\DataSet\DefaultTable;
use PHPUnit\DbUnit\DataSet\DefaultTableMetadata;
use PHPUnit\DbUnit\DataSet\ITable;
use PHPUnit\DbUnit\DataSet\ITableMetadata;
use PHPUnit\DbUnit\DataSet\QueryTable;
use PHPUnit\Framework\TestCase;
class Extensions_Database_DataSet_AbstractTableTest extends TestCase
{
/**
* @var QueryTable
*/
protected $table;
public function setUp()
{
$tableMetaData = new DefaultTableMetadata(
'table', ['id', 'column1']
);
$this->table = new DefaultTable($tableMetaData);
$this->table->addRow([
'id' => 1,
'column1' => 'randomValue'
]);
}
/**
* @param array $row
* @param bool $exists
* @dataProvider providerTableContainsRow
*/
public function testTableContainsRow($row, $exists)
{
$result = $this->table->assertContainsRow($row);
$this->assertEquals($exists, $result);
}
public function providerTableContainsRow()
{
return [
[['id' => 1, 'column1' => 'randomValue'], true],
[['id' => 1, 'column1' => 'notExistingValue'], false]
];
}
public function testMatchesWithNonMatchingMetaData()
{
$tableMetaData = $this->createMock(ITableMetadata::class);
$otherMetaData = $this->createMock(ITableMetadata::class);
$otherTable = $this->createMock(ITable::class);
$otherTable->expects($this->once())
->method('getTableMetaData')
->will($this->returnValue($otherMetaData));
$tableMetaData->expects($this->once())
->method('matches')
->with($otherMetaData)
->will($this->returnValue(false));
$table = new DefaultTable($tableMetaData);
$this->assertFalse($table->matches($otherTable));
}
public function testMatchesWithNonMatchingRowCount()
{
$tableMetaData = $this->createMock(ITableMetadata::class);
$otherMetaData = $this->createMock(ITableMetadata::class);
$otherTable = $this->createMock(ITable::class);
$table = $this->getMockBuilder(DefaultTable::class)
->setConstructorArgs([$tableMetaData])
->setMethods(['getRowCount'])
->getMock();
$otherTable->expects($this->once())
->method('getTableMetaData')
->will($this->returnValue($otherMetaData));
$otherTable->expects($this->once())
->method('getRowCount')
->will($this->returnValue(0));
$tableMetaData->expects($this->once())
->method('matches')
->with($otherMetaData)
->will($this->returnValue(true));
$table->expects($this->once())
->method('getRowCount')
->will($this->returnValue(1));
$this->assertFalse($table->matches($otherTable));
}
/**
* @param array $tableColumnValues
* @param array $otherColumnValues
* @param bool $matches
* @dataProvider providerMatchesWithColumnValueComparisons
*/
public function testMatchesWithColumnValueComparisons($tableColumnValues, $otherColumnValues, $matches)
{
$tableMetaData = $this->createMock(ITableMetadata::class);
$otherMetaData = $this->createMock(ITableMetadata::class);
$otherTable = $this->createMock(ITable::class);
$table = $this->getMockBuilder(DefaultTable::class)
->setConstructorArgs([$tableMetaData])
->setMethods(['getRowCount', 'getValue'])
->getMock();
$otherTable->expects($this->once())
->method('getTableMetaData')
->will($this->returnValue($otherMetaData));
$otherTable->expects($this->once())
->method('getRowCount')
->will($this->returnValue(count($otherColumnValues)));
$tableMetaData->expects($this->once())
->method('getColumns')
->will($this->returnValue(array_keys(reset($tableColumnValues))));
$tableMetaData->expects($this->once())
->method('matches')
->with($otherMetaData)
->will($this->returnValue(true));
$table->expects($this->any())
->method('getRowCount')
->will($this->returnValue(count($tableColumnValues)));
$tableMap = [];
$otherMap = [];
foreach ($tableColumnValues as $rowIndex => $rowData) {
foreach ($rowData as $columnName => $columnValue) {
$tableMap[] = [$rowIndex, $columnName, $columnValue];
$otherMap[] = [$rowIndex, $columnName, $otherColumnValues[$rowIndex][$columnName]];
}
}
$table->expects($this->any())
->method('getValue')
->will($this->returnValueMap($tableMap));
$otherTable->expects($this->any())
->method('getValue')
->will($this->returnValueMap($otherMap));
$this->assertSame($matches, $table->matches($otherTable));
}
public function providerMatchesWithColumnValueComparisons()
{
return [
// One row, one column, matches
[
[
['id' => 1],
],
[
['id' => 1],
],
true,
],
// One row, one column, does not match
[
[
['id' => 1],
],
[
['id' => 2],
],
false,
],
// Multiple rows, one column, matches
[
[
['id' => 1],
['id' => 2],
],
[
['id' => 1],
['id' => 2],
],
true,
],
// Multiple rows, one column, do not match
[
[
['id' => 1],
['id' => 2],
],
[
['id' => 1],
['id' => 3],
],
false,
],
// Multiple rows, multiple columns, matches
[
[
['id' => 1, 'name' => 'foo'],
['id' => 2, 'name' => 'bar'],
],
[
['id' => 1, 'name' => 'foo'],
['id' => 2, 'name' => 'bar'],
],
true,
],
// Multiple rows, multiple columns, do not match
[
[
['id' => 1, 'name' => 'foo'],
['id' => 2, 'name' => 'bar'],
],
[
['id' => 1, 'name' => 'foo'],
['id' => 2, 'name' => 'baz'],
],
false,
],
// Int and int as string must match
[
[
['id' => 42],
],
[
['id' => '42'],
],
true,
],
// Float and float as string must match
[
[
['id' => 15.3],
],
[
['id' => '15.3'],
],
true,
],
// Int and float must match
[
[
['id' => 18.00],
],
[
['id' => 18],
],
true,
],
// 0 and empty string must not match
[
[
['id' => 0],
],
[
['id' => ''],
],
false,
],
// 0 and null must not match
[
[
['id' => 0],
],
[
['id' => null],
],
false,
],
// empty string and null must not match
[
[
['id' => ''],
],
[
['id' => null],
],
false,
],
];
}
}

View File

@@ -0,0 +1,188 @@
<?php
/*
* This file is part of DbUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\DbUnit\DataSet\CompositeDataSet;
use PHPUnit\DbUnit\DataSet\DefaultDataSet;
use PHPUnit\DbUnit\DataSet\DefaultTable;
use PHPUnit\DbUnit\DataSet\DefaultTableMetadata;
use PHPUnit\DbUnit\TestCase;
class Extensions_Database_DataSet_CompositeDataSetTest extends \PHPUnit\Framework\TestCase
{
protected $expectedDataSet1;
protected $expectedDataSet2;
protected $expectedDataSet3;
public function setUp()
{
$table1MetaData = new DefaultTableMetadata(
'table1', ['table1_id', 'column1', 'column2', 'column3', 'column4']
);
$table2MetaData = new DefaultTableMetadata(
'table2', ['table2_id', 'column5', 'column6', 'column7', 'column8']
);
$table3MetaData = new DefaultTableMetadata(
'table3', ['table3_id', 'column9', 'column10', 'column11', 'column12']
);
$table1 = new DefaultTable($table1MetaData);
$table2 = new DefaultTable($table2MetaData);
$table3 = new DefaultTable($table3MetaData);
$table1->addRow([
'table1_id' => 1,
'column1' => 'tgfahgasdf',
'column2' => 200,
'column3' => 34.64,
'column4' => 'yghkf;a hahfg8ja h;'
]);
$table1->addRow([
'table1_id' => 2,
'column1' => 'hk;afg',
'column2' => 654,
'column3' => 46.54,
'column4' => '24rwehhads'
]);
$table1->addRow([
'table1_id' => 3,
'column1' => 'ha;gyt',
'column2' => 462,
'column3' => 1654.4,
'column4' => 'asfgklg'
]);
$table2->addRow([
'table2_id' => 1,
'column5' => 'fhah',
'column6' => 456,
'column7' => 46.5,
'column8' => 'fsdb, ghfdas'
]);
$table2->addRow([
'table2_id' => 2,
'column5' => 'asdhfoih',
'column6' => 654,
'column7' => 'blah',
'column8' => '43asd "fhgj" sfadh'
]);
$table2->addRow([
'table2_id' => 3,
'column5' => 'ajsdlkfguitah',
'column6' => 654,
'column7' => 'blah',
'column8' => 'thesethasdl
asdflkjsadf asdfsadfhl "adsf, halsdf" sadfhlasdf'
]);
$table3->addRow([
'table3_id' => 1,
'column9' => 'sfgsda',
'column10' => 16,
'column11' => 45.57,
'column12' => 'sdfh .ds,ajfas asdf h'
]);
$table3->addRow([
'table3_id' => 2,
'column9' => 'afdstgb',
'column10' => 41,
'column11' => 46.645,
'column12' => '87yhasdf sadf yah;/a '
]);
$table3->addRow([
'table3_id' => 3,
'column9' => 'gldsf',
'column10' => 46,
'column11' => 123.456,
'column12' => '0y8hosnd a/df7y olgbjs da'
]);
$this->expectedDataSet1 = new DefaultDataSet([$table1, $table2]);
$this->expectedDataSet2 = new DefaultDataSet([$table3]);
$this->expectedDataSet3 = new DefaultDataSet([$table1, $table2, $table3]);
}
public function testCompositeDataSet()
{
$actual = new CompositeDataSet([$this->expectedDataSet1, $this->expectedDataSet2]);
TestCase::assertDataSetsEqual($this->expectedDataSet3, $actual);
}
public function testCompatibleTablesInDifferentDataSetsNonDuplicateRows()
{
$compatibleTable = new DefaultTable(
$this->expectedDataSet3->getTable('table3')->getTableMetaData()
);
$compatibleTable->addRow([
'table3_id' => 4,
'column9' => 'asdasd',
'column10' => 17,
'column11' => 42.57,
'column12' => 'askldja'
]);
$compositeDataSet = new CompositeDataSet([
new DefaultDataSet([$compatibleTable]),
$this->expectedDataSet2
]);
$this->assertEquals(4, $compositeDataSet->getTable('table3')->getRowCount());
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage There is already a table named table3 with different table definition
*/
public function testExceptionOnIncompatibleTablesSameTableNames()
{
$inCompatibleTableMetaData = new DefaultTableMetadata(
'table3', ['table3_id', 'column13', 'column14', 'column15', 'column16']
);
$inCompatibleTable = new DefaultTable($inCompatibleTableMetaData);
$inCompatibleTable->addRow([
'column13' => 'asdasda asdasd',
'column14' => 'aiafsjas asd',
'column15' => 'asdasdasd',
'column16' => 2141
]);
$compositeDataSet = new CompositeDataSet([
$this->expectedDataSet2,
new DefaultDataSet([$inCompatibleTable])
]);
}
/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage There is already a table named table3 with different table definition
*/
public function testExceptionOnIncompatibleTablesSameTableNames2()
{
$inCompatibleTableMetaData = new DefaultTableMetadata(
'table3', ['table3_id', 'column13', 'column14', 'column15', 'column16']
);
$inCompatibleTable = new DefaultTable($inCompatibleTableMetaData);
$inCompatibleTable->addRow([
'column13' => 'asdasda asdasd',
'column14' => 'aiafsjas asd',
'column15' => 'asdasdasd',
'column16' => 2141
]);
$compositeDataSet = new CompositeDataSet([
new DefaultDataSet([$inCompatibleTable]),
$this->expectedDataSet2
]);
}
}

View File

@@ -0,0 +1,86 @@
<?php
/*
* This file is part of DbUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\DbUnit\DataSet\CsvDataSet;
use PHPUnit\DbUnit\DataSet\DefaultDataSet;
use PHPUnit\DbUnit\DataSet\DefaultTable;
use PHPUnit\DbUnit\DataSet\DefaultTableMetadata;
use PHPUnit\DbUnit\TestCase;
class Extensions_Database_DataSet_CsvDataSetTest extends \PHPUnit\Framework\TestCase
{
protected $expectedDataSet;
public function testCSVDataSet()
{
$table1MetaData = new DefaultTableMetadata(
'table1', ['table1_id', 'column1', 'column2', 'column3', 'column4']
);
$table2MetaData = new DefaultTableMetadata(
'table2', ['table2_id', 'column5', 'column6', 'column7', 'column8']
);
$table1 = new DefaultTable($table1MetaData);
$table2 = new DefaultTable($table2MetaData);
$table1->addRow([
'table1_id' => 1,
'column1' => 'tgfahgasdf',
'column2' => 200,
'column3' => 34.64,
'column4' => 'yghkf;a hahfg8ja h;'
]);
$table1->addRow([
'table1_id' => 2,
'column1' => 'hk;afg',
'column2' => 654,
'column3' => 46.54,
'column4' => '24rwehhads'
]);
$table1->addRow([
'table1_id' => 3,
'column1' => 'ha;gyt',
'column2' => 462,
'column3' => 1654.4,
'column4' => 'asfgklg'
]);
$table2->addRow([
'table2_id' => 1,
'column5' => 'fhah',
'column6' => 456,
'column7' => 46.5,
'column8' => 'fsdb, ghfdas'
]);
$table2->addRow([
'table2_id' => 2,
'column5' => 'asdhfoih',
'column6' => 654,
'column7' => 'blah',
'column8' => '43asd "fhgj" sfadh'
]);
$table2->addRow([
'table2_id' => 3,
'column5' => 'ajsdlkfguitah',
'column6' => 654,
'column7' => 'blah',
'column8' => 'thesethasdl
asdflkjsadf asdfsadfhl "adsf, halsdf" sadfhlasdf'
]);
$expectedDataSet = new DefaultDataSet([$table1, $table2]);
$csvDataSet = new CsvDataSet();
$csvDataSet->addTable('table1', dirname(__FILE__) . '/../_files/CsvDataSets/table1.csv');
$csvDataSet->addTable('table2', dirname(__FILE__) . '/../_files/CsvDataSets/table2.csv');
TestCase::assertDataSetsEqual($expectedDataSet, $csvDataSet);
}
}

View File

@@ -0,0 +1,90 @@
<?php
/*
* This file is part of DbUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\DbUnit\Constraint\DataSetIsEqual;
use PHPUnit\DbUnit\DataSet\Filter;
use PHPUnit\DbUnit\DataSet\FlatXmlDataSet;
use PHPUnit\Framework\TestCase;
class Extensions_Database_DataSet_FilterTest extends TestCase
{
protected $expectedDataSet;
public function setUp()
{
$this->expectedDataSet = new FlatXmlDataSet(
dirname(__FILE__) . '/../_files/XmlDataSets/FilteredTestFixture.xml'
);
}
public function testDeprecatedFilteredDataSetConstructor()
{
$constraint = new DataSetIsEqual($this->expectedDataSet);
$dataSet = new FlatXmlDataSet(
dirname(__FILE__) . '/../_files/XmlDataSets/FilteredTestComparison.xml'
);
$filteredDataSet = new Filter($dataSet, [
'table1' => ['table1_id'],
'table2' => '*',
'table3' => 'table3_id'
]);
self::assertThat($filteredDataSet, $constraint);
}
public function testExcludeFilteredDataSet()
{
$constraint = new DataSetIsEqual($this->expectedDataSet);
$dataSet = new FlatXmlDataSet(
dirname(__FILE__) . '/../_files/XmlDataSets/FilteredTestComparison.xml'
);
$filteredDataSet = new Filter($dataSet);
$filteredDataSet->addExcludeTables(['table2']);
$filteredDataSet->setExcludeColumnsForTable('table1', ['table1_id']);
$filteredDataSet->setExcludeColumnsForTable('table3', ['table3_id']);
self::assertThat($filteredDataSet, $constraint);
}
public function testIncludeFilteredDataSet()
{
$constraint = new DataSetIsEqual($this->expectedDataSet);
$dataSet = new FlatXmlDataSet(
dirname(__FILE__) . '/../_files/XmlDataSets/FilteredTestComparison.xml'
);
$filteredDataSet = new Filter($dataSet);
$filteredDataSet->addIncludeTables(['table1', 'table3']);
$filteredDataSet->setIncludeColumnsForTable('table1', ['column1', 'column2', 'column3', 'column4']);
$filteredDataSet->setIncludeColumnsForTable('table3', ['column9', 'column10', 'column11', 'column12']);
self::assertThat($filteredDataSet, $constraint);
}
public function testIncludeExcludeMixedDataSet()
{
$constraint = new DataSetIsEqual($this->expectedDataSet);
$dataSet = new FlatXmlDataSet(
dirname(__FILE__) . '/../_files/XmlDataSets/FilteredTestComparison.xml'
);
$filteredDataSet = new Filter($dataSet);
$filteredDataSet->addIncludeTables(['table1', 'table3']);
$filteredDataSet->setExcludeColumnsForTable('table1', ['table1_id']);
$filteredDataSet->setIncludeColumnsForTable('table3', ['column9', 'column10', 'column11', 'column12']);
self::assertThat($filteredDataSet, $constraint);
}
}

View File

@@ -0,0 +1,98 @@
<?php
/*
* This file is part of DbUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\DbUnit\Database\DefaultConnection;
use PHPUnit\DbUnit\DataSet\DefaultTable;
use PHPUnit\DbUnit\DataSet\DefaultTableMetadata;
use PHPUnit\DbUnit\DataSet\ITable;
use PHPUnit\DbUnit\DataSet\QueryDataSet;
use PHPUnit\DbUnit\TestCase;
class Extensions_Database_DataSet_QueryDataSetTest extends TestCase
{
/**
* @var QueryDataSet
*/
protected $dataSet;
protected $pdo;
/**
* @return DefaultConnection
*/
protected function getConnection()
{
return $this->createDefaultDBConnection($this->pdo, 'test');
}
protected function getDataSet()
{
return $this->createFlatXMLDataSet(dirname(__FILE__) . '/../_files/XmlDataSets/QueryDataSetTest.xml');
}
public function setUp()
{
$this->pdo = DBUnitTestUtility::getSQLiteMemoryDB();
parent::setUp();
$this->dataSet = new QueryDataSet($this->getConnection());
$this->dataSet->addTable('table1');
$this->dataSet->addTable('query1', '
SELECT
t1.column1 tc1, t2.column5 tc2
FROM
table1 t1
JOIN table2 t2 ON t1.table1_id = t2.table2_id
');
}
public function testGetTable()
{
$expectedTable1 = $this->getConnection()->createDataSet(['table1'])->getTable('table1');
$expectedTable2 = new DefaultTable(
new DefaultTableMetadata('query1', ['tc1', 'tc2'])
);
$expectedTable2->addRow(['tc1' => 'bar', 'tc2' => 'blah']);
$this->assertTablesEqual($expectedTable1, $this->dataSet->getTable('table1'));
$this->assertTablesEqual($expectedTable2, $this->dataSet->getTable('query1'));
}
public function testGetTableNames()
{
$this->assertEquals(['table1', 'query1'], $this->dataSet->getTableNames());
}
public function testCreateIterator()
{
$expectedTable1 = $this->getConnection()->createDataSet(['table1'])->getTable('table1');
$expectedTable2 = new DefaultTable(
new DefaultTableMetadata('query1', ['tc1', 'tc2'])
);
$expectedTable2->addRow(['tc1' => 'bar', 'tc2' => 'blah']);
foreach ($this->dataSet as $i => $table) {
/* @var $table ITable */
switch ($table->getTableMetaData()->getTableName()) {
case 'table1':
$this->assertTablesEqual($expectedTable1, $table);
break;
case 'query1':
$this->assertTablesEqual($expectedTable2, $table);
break;
default:
$this->fail('Proper keys not present from the iterator');
}
}
}
}

View File

@@ -0,0 +1,103 @@
<?php
/*
* This file is part of DbUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\DbUnit\Database\DefaultConnection;
use PHPUnit\DbUnit\DataSet\DefaultTable;
use PHPUnit\DbUnit\DataSet\DefaultTableMetadata;
use PHPUnit\DbUnit\DataSet\QueryTable;
use PHPUnit\Framework\TestCase;
class Extensions_Database_DataSet_QueryTableTest extends TestCase
{
/**
* @var QueryTable
*/
protected $table;
public function setUp()
{
$query = "
SELECT
'value1' as col1,
'value2' as col2,
'value3' as col3
UNION SELECT
'value4' as col1,
'value5' as col2,
'value6' as col3
";
$this->table = new QueryTable(
'table1',
$query,
new DefaultConnection(new PDO('sqlite::memory:'), 'test')
);
}
public static function providerTestGetValue()
{
return [
[0, 'col1', 'value1'],
[0, 'col2', 'value2'],
[0, 'col3', 'value3'],
[1, 'col1', 'value4'],
[1, 'col2', 'value5'],
[1, 'col3', 'value6'],
];
}
public function testGetTableMetaData()
{
$metaData = new DefaultTableMetadata('table1', ['col1', 'col2', 'col3']);
$this->assertEquals($metaData, $this->table->getTableMetaData());
}
public function testGetRowCount()
{
$this->assertEquals(2, $this->table->getRowCount());
}
/**
* @dataProvider providerTestGetValue
*/
public function testGetValue($row, $column, $value)
{
$this->assertEquals($value, $this->table->getValue($row, $column));
}
public function testGetRow()
{
$this->assertEquals(['col1' => 'value1', 'col2' => 'value2', 'col3' => 'value3'], $this->table->getRow(0));
}
public function testAssertEquals()
{
$expected_table = new DefaultTable(new DefaultTableMetadata('table1', ['col1', 'col2', 'col3']));
$expected_table->addRow(['col1' => 'value1', 'col2' => 'value2', 'col3' => 'value3']);
$expected_table->addRow(['col1' => 'value4', 'col2' => 'value5', 'col3' => 'value6']);
$this->assertTrue($this->table->matches($expected_table));
}
public function testAssertEqualsFails()
{
$expected_table = new DefaultTable(new DefaultTableMetadata('table1', ['col1', 'col2', 'col3']));
$expected_table->addRow(['col1' => 'value1', 'col2' => 'value2', 'col3' => 'value3']);
$expected_table->addRow(['col1' => 'value4', 'col2' => 'value5', 'col3' => 'value6']);
$expected_table->addRow(['col1' => 'value7', 'col2' => 'value8', 'col3' => 'value9']);
$this->assertFalse($this->table->matches($expected_table));
}
public function testAssertRowContains()
{
$this->assertTrue($this->table->assertContainsRow(
['col1' => 'value1', 'col2' => 'value2', 'col3' => 'value3']
));
}
}

View File

@@ -0,0 +1,282 @@
<?php
/*
* This file is part of DbUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\DbUnit\DataSet\DefaultDataSet;
use PHPUnit\DbUnit\DataSet\DefaultTable;
use PHPUnit\DbUnit\DataSet\DefaultTableMetadata;
use PHPUnit\DbUnit\DataSet\ReplacementDataSet;
use PHPUnit\DbUnit\TestCase;
class Extensions_Database_DataSet_ReplacementDataSetTest extends \PHPUnit\Framework\TestCase
{
/**
* @var DefaultDataSet
*/
protected $startingDataSet;
public function setUp()
{
$table1MetaData = new DefaultTableMetadata(
'table1', ['table1_id', 'column1', 'column2', 'column3', 'column4']
);
$table2MetaData = new DefaultTableMetadata(
'table2', ['table2_id', 'column5', 'column6', 'column7', 'column8']
);
$table1 = new DefaultTable($table1MetaData);
$table2 = new DefaultTable($table2MetaData);
$table1->addRow([
'table1_id' => 1,
'column1' => 'My name is %%%name%%%',
'column2' => 200,
'column3' => 34.64,
'column4' => 'yghkf;a hahfg8ja h;'
]);
$table1->addRow([
'table1_id' => 2,
'column1' => 'hk;afg',
'column2' => 654,
'column3' => 46.54,
'column4' => '24rwehhads'
]);
$table1->addRow([
'table1_id' => 3,
'column1' => 'ha;gyt',
'column2' => 462,
'column3' => 1654.4,
'column4' => '[NULL]'
]);
$table2->addRow([
'table2_id' => 1,
'column5' => 'fhah',
'column6' => 456,
'column7' => 46.5,
'column8' => 'My name is %%%name%%%'
]);
$table2->addRow([
'table2_id' => 2,
'column5' => 'asdhfoih',
'column6' => 654,
'column7' => '[NULL]',
'column8' => '43asdfhgj'
]);
$table2->addRow([
'table2_id' => 3,
'column5' => 'ajsdlkfguitah',
'column6' => 654,
'column7' => '[NULL]',
'column8' => '[NULL] not really'
]);
$this->startingDataSet = new DefaultDataSet([$table1, $table2]);
}
public function testNoReplacement()
{
TestCase::assertDataSetsEqual(
$this->startingDataSet,
new ReplacementDataSet($this->startingDataSet)
);
}
public function testFullReplacement()
{
$table1MetaData = new DefaultTableMetadata(
'table1', ['table1_id', 'column1', 'column2', 'column3', 'column4']
);
$table2MetaData = new DefaultTableMetadata(
'table2', ['table2_id', 'column5', 'column6', 'column7', 'column8']
);
$table1 = new DefaultTable($table1MetaData);
$table2 = new DefaultTable($table2MetaData);
$table1->addRow([
'table1_id' => 1,
'column1' => 'My name is %%%name%%%',
'column2' => 200,
'column3' => 34.64,
'column4' => 'yghkf;a hahfg8ja h;'
]);
$table1->addRow([
'table1_id' => 2,
'column1' => 'hk;afg',
'column2' => 654,
'column3' => 46.54,
'column4' => '24rwehhads'
]);
$table1->addRow([
'table1_id' => 3,
'column1' => 'ha;gyt',
'column2' => 462,
'column3' => 1654.4,
'column4' => null
]);
$table2->addRow([
'table2_id' => 1,
'column5' => 'fhah',
'column6' => 456,
'column7' => 46.5,
'column8' => 'My name is %%%name%%%'
]);
$table2->addRow([
'table2_id' => 2,
'column5' => 'asdhfoih',
'column6' => 654,
'column7' => null,
'column8' => '43asdfhgj'
]);
$table2->addRow([
'table2_id' => 3,
'column5' => 'ajsdlkfguitah',
'column6' => 654,
'column7' => null,
'column8' => '[NULL] not really'
]);
$expected = new DefaultDataSet([$table1, $table2]);
$actual = new ReplacementDataSet($this->startingDataSet);
$actual->addFullReplacement('[NULL]', null);
TestCase::assertDataSetsEqual($expected, $actual);
}
public function testSubStrReplacement()
{
$table1MetaData = new DefaultTableMetadata(
'table1', ['table1_id', 'column1', 'column2', 'column3', 'column4']
);
$table2MetaData = new DefaultTableMetadata(
'table2', ['table2_id', 'column5', 'column6', 'column7', 'column8']
);
$table1 = new DefaultTable($table1MetaData);
$table2 = new DefaultTable($table2MetaData);
$table1->addRow([
'table1_id' => 1,
'column1' => 'My name is Mike Lively',
'column2' => 200,
'column3' => 34.64,
'column4' => 'yghkf;a hahfg8ja h;'
]);
$table1->addRow([
'table1_id' => 2,
'column1' => 'hk;afg',
'column2' => 654,
'column3' => 46.54,
'column4' => '24rwehhads'
]);
$table1->addRow([
'table1_id' => 3,
'column1' => 'ha;gyt',
'column2' => 462,
'column3' => 1654.4,
'column4' => '[NULL]'
]);
$table2->addRow([
'table2_id' => 1,
'column5' => 'fhah',
'column6' => 456,
'column7' => 46.5,
'column8' => 'My name is Mike Lively'
]);
$table2->addRow([
'table2_id' => 2,
'column5' => 'asdhfoih',
'column6' => 654,
'column7' => '[NULL]',
'column8' => '43asdfhgj'
]);
$table2->addRow([
'table2_id' => 3,
'column5' => 'ajsdlkfguitah',
'column6' => 654,
'column7' => '[NULL]',
'column8' => '[NULL] not really'
]);
$expected = new DefaultDataSet([$table1, $table2]);
$actual = new ReplacementDataSet($this->startingDataSet);
$actual->addSubStrReplacement('%%%name%%%', 'Mike Lively');
TestCase::assertDataSetsEqual($expected, $actual);
}
public function testConstructorReplacements()
{
$table1MetaData = new DefaultTableMetadata(
'table1', ['table1_id', 'column1', 'column2', 'column3', 'column4']
);
$table2MetaData = new DefaultTableMetadata(
'table2', ['table2_id', 'column5', 'column6', 'column7', 'column8']
);
$table1 = new DefaultTable($table1MetaData);
$table2 = new DefaultTable($table2MetaData);
$table1->addRow([
'table1_id' => 1,
'column1' => 'My name is Mike Lively',
'column2' => 200,
'column3' => 34.64,
'column4' => 'yghkf;a hahfg8ja h;'
]);
$table1->addRow([
'table1_id' => 2,
'column1' => 'hk;afg',
'column2' => 654,
'column3' => 46.54,
'column4' => '24rwehhads'
]);
$table1->addRow([
'table1_id' => 3,
'column1' => 'ha;gyt',
'column2' => 462,
'column3' => 1654.4,
'column4' => null
]);
$table2->addRow([
'table2_id' => 1,
'column5' => 'fhah',
'column6' => 456,
'column7' => 46.5,
'column8' => 'My name is Mike Lively'
]);
$table2->addRow([
'table2_id' => 2,
'column5' => 'asdhfoih',
'column6' => 654,
'column7' => null,
'column8' => '43asdfhgj'
]);
$table2->addRow([
'table2_id' => 3,
'column5' => 'ajsdlkfguitah',
'column6' => 654,
'column7' => null,
'column8' => '[NULL] not really'
]);
$expected = new DefaultDataSet([$table1, $table2]);
$actual = new ReplacementDataSet(
$this->startingDataSet,
['[NULL]' => null],
['%%%name%%%' => 'Mike Lively']
);
TestCase::assertDataSetsEqual($expected, $actual);
}
}

View File

@@ -0,0 +1,482 @@
<?php
/*
* This file is part of DbUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\DbUnit\DataSet\DefaultTable;
use PHPUnit\DbUnit\DataSet\DefaultTableMetadata;
use PHPUnit\DbUnit\DataSet\ITable;
use PHPUnit\DbUnit\DataSet\ITableMetadata;
use PHPUnit\DbUnit\DataSet\ReplacementTable;
use PHPUnit\DbUnit\TestCase;
class Extensions_Database_DataSet_ReplacementTableTest extends \PHPUnit\Framework\TestCase
{
/**
* @var DefaultTable
*/
protected $startingTable;
public function setUp()
{
$tableMetaData = new DefaultTableMetadata(
'table1', ['table1_id', 'column1', 'column2', 'column3', 'column4']
);
$table = new DefaultTable($tableMetaData);
$table->addRow([
'table1_id' => 1,
'column1' => 'My name is %%%name%%%',
'column2' => 200,
'column3' => 34.64,
'column4' => 'yghkf;a hahfg8ja h;'
]);
$table->addRow([
'table1_id' => 2,
'column1' => 'hk;afg',
'column2' => 654,
'column3' => 46.54,
'column4' => '24rwehhads'
]);
$table->addRow([
'table1_id' => 3,
'column1' => 'ha;gyt',
'column2' => 462,
'column3' => '[NULL] not really',
'column4' => '[NULL]'
]);
$this->startingTable = $table;
}
public function testNoReplacement()
{
TestCase::assertTablesEqual(
$this->startingTable,
new ReplacementTable($this->startingTable)
);
}
public function testFullReplacement()
{
$tableMetaData = new DefaultTableMetadata(
'table1', ['table1_id', 'column1', 'column2', 'column3', 'column4']
);
$table = new DefaultTable($tableMetaData);
$table->addRow([
'table1_id' => 1,
'column1' => 'My name is %%%name%%%',
'column2' => 200,
'column3' => 34.64,
'column4' => 'yghkf;a hahfg8ja h;'
]);
$table->addRow([
'table1_id' => 2,
'column1' => 'hk;afg',
'column2' => 654,
'column3' => 46.54,
'column4' => '24rwehhads'
]);
$table->addRow([
'table1_id' => 3,
'column1' => 'ha;gyt',
'column2' => 462,
'column3' => '[NULL] not really',
'column4' => null
]);
$actual = new ReplacementTable($this->startingTable);
$actual->addFullReplacement('[NULL]', null);
TestCase::assertTablesEqual($table, $actual);
}
public function testSubStrReplacement()
{
$tableMetaData = new DefaultTableMetadata(
'table1', ['table1_id', 'column1', 'column2', 'column3', 'column4']
);
$table = new DefaultTable($tableMetaData);
$table->addRow([
'table1_id' => 1,
'column1' => 'My name is Mike Lively',
'column2' => 200,
'column3' => 34.64,
'column4' => 'yghkf;a hahfg8ja h;'
]);
$table->addRow([
'table1_id' => 2,
'column1' => 'hk;afg',
'column2' => 654,
'column3' => 46.54,
'column4' => '24rwehhads'
]);
$table->addRow([
'table1_id' => 3,
'column1' => 'ha;gyt',
'column2' => 462,
'column3' => '[NULL] not really',
'column4' => '[NULL]'
]);
$actual = new ReplacementTable($this->startingTable);
$actual->addSubStrReplacement('%%%name%%%', 'Mike Lively');
TestCase::assertTablesEqual($table, $actual);
}
public function testConstructorReplacements()
{
$tableMetaData = new DefaultTableMetadata(
'table1', ['table1_id', 'column1', 'column2', 'column3', 'column4']
);
$table = new DefaultTable($tableMetaData);
$table->addRow([
'table1_id' => 1,
'column1' => 'My name is Mike Lively',
'column2' => 200,
'column3' => 34.64,
'column4' => 'yghkf;a hahfg8ja h;'
]);
$table->addRow([
'table1_id' => 2,
'column1' => 'hk;afg',
'column2' => 654,
'column3' => 46.54,
'column4' => '24rwehhads'
]);
$table->addRow([
'table1_id' => 3,
'column1' => 'ha;gyt',
'column2' => 462,
'column3' => '[NULL] not really',
'column4' => null
]);
$actual = new ReplacementTable(
$this->startingTable,
['[NULL]' => null],
['%%%name%%%' => 'Mike Lively']
);
TestCase::assertTablesEqual($table, $actual);
}
public function testGetRow()
{
$actual = new ReplacementTable(
$this->startingTable,
['[NULL]' => null],
['%%%name%%%' => 'Mike Lively']
);
$this->assertEquals(
[
'table1_id' => 1,
'column1' => 'My name is Mike Lively',
'column2' => 200,
'column3' => 34.64,
'column4' => 'yghkf;a hahfg8ja h;'
],
$actual->getRow(0)
);
$this->assertEquals(
[
'table1_id' => 3,
'column1' => 'ha;gyt',
'column2' => 462,
'column3' => '[NULL] not really',
'column4' => null
],
$actual->getRow(2)
);
}
public function testGetValue()
{
$actual = new ReplacementTable(
$this->startingTable,
['[NULL]' => null],
['%%%name%%%' => 'Mike Lively']
);
$this->assertNull($actual->getValue(2, 'column4'));
$this->assertEquals('My name is Mike Lively', $actual->getValue(0, 'column1'));
}
public function testMatchesWithNonMatchingMetaData()
{
$tableMetaData = $this->createMock(ITableMetadata::class);
$otherMetaData = $this->createMock(ITableMetadata::class);
$table = $this->createMock(ITable::class);
$otherTable = $this->createMock(ITable::class);
$table->expects($this->once())
->method('getTableMetaData')
->will($this->returnValue($tableMetaData));
$otherTable->expects($this->once())
->method('getTableMetaData')
->will($this->returnValue($otherMetaData));
$tableMetaData->expects($this->once())
->method('matches')
->with($otherMetaData)
->will($this->returnValue(false));
$replacementTable = new ReplacementTable($table);
$this->assertFalse($replacementTable->matches($otherTable));
}
public function testMatchesWithNonMatchingRowCount()
{
$tableMetaData = $this->createMock(ITableMetadata::class);
$otherMetaData = $this->createMock(ITableMetadata::class);
$table = $this->createMock(ITable::class);
$otherTable = $this->createMock(ITable::class);
$replacementTable = $this->getMockBuilder(ReplacementTable::class)
->setConstructorArgs([$table])
->setMethods(['getRowCount'])
->getMock();
$table->expects($this->once())
->method('getTableMetaData')
->will($this->returnValue($tableMetaData));
$otherTable->expects($this->once())
->method('getTableMetaData')
->will($this->returnValue($otherMetaData));
$otherTable->expects($this->once())
->method('getRowCount')
->will($this->returnValue(0));
$tableMetaData->expects($this->once())
->method('matches')
->with($otherMetaData)
->will($this->returnValue(true));
$replacementTable->expects($this->once())
->method('getRowCount')
->will($this->returnValue(1));
$this->assertFalse($replacementTable->matches($otherTable));
}
/**
* @param array $tableColumnValues
* @param array $otherColumnValues
* @param bool $matches
* @dataProvider providerMatchesWithColumnValueComparisons
*/
public function testMatchesWithColumnValueComparisons($tableColumnValues, $otherColumnValues, $matches)
{
$tableMetaData = $this->createMock(ITableMetadata::class);
$otherMetaData = $this->createMock(ITableMetadata::class);
$table = $this->createMock(ITable::class);
$otherTable = $this->createMock(ITable::class);
$table->expects($this->once())
->method('getTableMetaData')
->will($this->returnValue($tableMetaData));
$otherTable->expects($this->once())
->method('getTableMetaData')
->will($this->returnValue($otherMetaData));
$otherTable->expects($this->once())
->method('getRowCount')
->will($this->returnValue(count($otherColumnValues)));
$tableMetaData->expects($this->once())
->method('getColumns')
->will($this->returnValue(array_keys(reset($tableColumnValues))));
$tableMetaData->expects($this->once())
->method('matches')
->with($otherMetaData)
->will($this->returnValue(true));
$replacementTable = $this->getMockBuilder(ReplacementTable::class)
->setConstructorArgs([$table])
->setMethods(['getRowCount', 'getValue'])
->getMock();
$replacementTable->expects($this->any())
->method('getRowCount')
->will($this->returnValue(count($tableColumnValues)));
$tableMap = [];
$otherMap = [];
foreach ($tableColumnValues as $rowIndex => $rowData) {
foreach ($rowData as $columnName => $columnValue) {
$tableMap[] = [$rowIndex, $columnName, $columnValue];
$otherMap[] = [$rowIndex, $columnName, $otherColumnValues[$rowIndex][$columnName]];
}
}
$replacementTable->expects($this->any())
->method('getValue')
->will($this->returnValueMap($tableMap));
$otherTable->expects($this->any())
->method('getValue')
->will($this->returnValueMap($otherMap));
$this->assertSame($matches, $replacementTable->matches($otherTable));
}
public function providerMatchesWithColumnValueComparisons()
{
return [
// One row, one column, matches
[
[
['id' => 1],
],
[
['id' => 1],
],
true,
],
// One row, one column, does not match
[
[
['id' => 1],
],
[
['id' => 2],
],
false,
],
// Multiple rows, one column, matches
[
[
['id' => 1],
['id' => 2],
],
[
['id' => 1],
['id' => 2],
],
true,
],
// Multiple rows, one column, do not match
[
[
['id' => 1],
['id' => 2],
],
[
['id' => 1],
['id' => 3],
],
false,
],
// Multiple rows, multiple columns, matches
[
[
['id' => 1, 'name' => 'foo'],
['id' => 2, 'name' => 'bar'],
],
[
['id' => 1, 'name' => 'foo'],
['id' => 2, 'name' => 'bar'],
],
true,
],
// Multiple rows, multiple columns, do not match
[
[
['id' => 1, 'name' => 'foo'],
['id' => 2, 'name' => 'bar'],
],
[
['id' => 1, 'name' => 'foo'],
['id' => 2, 'name' => 'baz'],
],
false,
],
// Int and int as string must match
[
[
['id' => 42],
],
[
['id' => '42'],
],
true,
],
// Float and float as string must match
[
[
['id' => 15.3],
],
[
['id' => '15.3'],
],
true,
],
// Int and float must match
[
[
['id' => 18.00],
],
[
['id' => 18],
],
true,
],
// 0 and empty string must not match
[
[
['id' => 0],
],
[
['id' => ''],
],
false,
],
// 0 and null must not match
[
[
['id' => 0],
],
[
['id' => null],
],
false,
],
// empty string and null must not match
[
[
['id' => ''],
],
[
['id' => null],
],
false,
],
];
}
}

View File

@@ -0,0 +1,106 @@
<?php
/*
* This file is part of DbUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
use PHPUnit\DbUnit\Constraint\DataSetIsEqual;
use PHPUnit\DbUnit\DataSet\DefaultDataSet;
use PHPUnit\DbUnit\DataSet\DefaultTable;
use PHPUnit\DbUnit\DataSet\DefaultTableMetadata;
use PHPUnit\DbUnit\DataSet\FlatXmlDataSet;
use PHPUnit\DbUnit\DataSet\MysqlXmlDataSet;
use PHPUnit\DbUnit\DataSet\XmlDataSet;
use PHPUnit\Framework\TestCase;
class Extensions_Database_DataSet_XmlDataSetsTest extends TestCase
{
protected $expectedDataSet;
public function setUp()
{
$table1MetaData = new DefaultTableMetadata(
'table1', ['table1_id', 'column1', 'column2', 'column3', 'column4']
);
$table2MetaData = new DefaultTableMetadata(
'table2', ['table2_id', 'column5', 'column6', 'column7', 'column8']
);
$table1 = new DefaultTable($table1MetaData);
$table2 = new DefaultTable($table2MetaData);
$table1->addRow([
'table1_id' => 1,
'column1' => 'tgfahgasdf',
'column2' => 200,
'column3' => 34.64,
'column4' => 'yghkf;a hahfg8ja h;'
]);
$table1->addRow([
'table1_id' => 2,
'column1' => 'hk;afg',
'column2' => 654,
'column3' => 46.54,
'column4' => '24rwehhads'
]);
$table1->addRow([
'table1_id' => 3,
'column1' => 'ha;gyt',
'column2' => 462,
'column3' => 1654.4,
'column4' => 'asfgklg'
]);
$table2->addRow([
'table2_id' => 1,
'column5' => 'fhah',
'column6' => 456,
'column7' => 46.5,
'column8' => 'fsdbghfdas'
]);
$table2->addRow([
'table2_id' => 2,
'column5' => 'asdhfoih',
'column6' => 654,
'column7' => null,
'column8' => '43asdfhgj'
]);
$table2->addRow([
'table2_id' => 3,
'column5' => 'ajsdlkfguitah',
'column6' => 654,
'column7' => null,
'column8' => null
]);
$this->expectedDataSet = new DefaultDataSet([$table1, $table2]);
}
public function testFlatXmlDataSet()
{
$constraint = new DataSetIsEqual($this->expectedDataSet);
$xmlFlatDataSet = new FlatXmlDataSet(dirname(__FILE__) . '/../_files/XmlDataSets/FlatXmlDataSet.xml');
self::assertThat($xmlFlatDataSet, $constraint);
}
public function testXmlDataSet()
{
$constraint = new DataSetIsEqual($this->expectedDataSet);
$xmlDataSet = new XmlDataSet(dirname(__FILE__) . '/../_files/XmlDataSets/XmlDataSet.xml');
self::assertThat($xmlDataSet, $constraint);
}
public function testMysqlXmlDataSet()
{
$constraint = new DataSetIsEqual($this->expectedDataSet);
$mysqlXmlDataSet = new MysqlXmlDataSet(dirname(__FILE__) . '/../_files/XmlDataSets/MysqlXmlDataSet.xml');
self::assertThat($mysqlXmlDataSet, $constraint);
}
}