Skip to content

Commit 8093fb7

Browse files
author
Malte Riesch
committed
new function: assertArrayContainsPsv
1 parent 37c6d5a commit 8093fb7

File tree

4 files changed

+175
-3
lines changed

4 files changed

+175
-3
lines changed

lib/TestDbAcle/PhpUnit/AbstractTestCase.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,20 @@ protected function assertTableStateContains( $expectedPsv, $placeHolders = array
9393
$this->assertEquals($command->getExpectedData(), $command->getActualData(), $message);
9494
}
9595

96+
/**
97+
* Asserts that array contains the values specified. Placeholders can be
98+
* provided
99+
*
100+
* @param PsvString $expectedPsv expected values as PSV
101+
* @param array $actual actual values
102+
* @param array $placeHolders
103+
* @param String $message an optional assert message
104+
*/
105+
protected function assertArrayContainsPsv( $expectedPsv, $actual, $placeHolders = array(), $message = '' )
106+
{
107+
$command = new \TestDbAcle\Commands\FilterArrayByPsvCommand($expectedPsv, $actual, $placeHolders);
108+
$this->getDatabaseTestHelper()->runCommand($command);
109+
$this->assertEquals($command->getExpectedData(), $command->getActualData(), $message);
110+
}
96111

97112
}

lib/TestDbAcle/PhpUnit/Traits/DatabaseHelperTrait.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,19 @@ protected function assertTableStateContains( $expectedPsv, $placeHolders = array
8888
$this->assertEquals($command->getExpectedData(), $command->getActualData(), $message);
8989
}
9090

91-
91+
/**
92+
* Asserts that array contains the values specified. Placeholders can be
93+
* provided
94+
*
95+
* @param PsvString $expectedPsv expected values as PSV
96+
* @param array $actual actual values
97+
* @param array $placeHolders
98+
* @param String $message an optional assert message
99+
*/
100+
protected function assertArrayContainsPsv( $expectedPsv, $actual, $placeHolders = array(), $message = '' )
101+
{
102+
$command = new \TestDbAcle\Commands\FilterArrayByPsvCommand($expectedPsv, $actual, $placeHolders);
103+
$this->getDatabaseTestHelper()->runCommand($command);
104+
$this->assertEquals($command->getExpectedData(), $command->getActualData(), $message);
105+
}
92106
}

tests/Functional/tests/Php5_3/SmokeTest.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,79 @@ function test_AssertTableStateContains_handlesEmptyTables_raisesExceptionIfEmpty
304304
}
305305

306306
}
307+
308+
/**
309+
* @covers \TestDbAcle\Commands\FilterTableStateByPsvCommand::execute()
310+
* @covers \TestDbAcle\Commands\FilterTableStateByPsvCommand::FilterArrayByPsvCommand()
311+
* @covers \TestDbAcle::getDefaultFactories()
312+
*/
313+
function test_assertArrayContainsPsv()
314+
{
315+
316+
$exampleArray = array(
317+
array('address_id' => 5, 'company' => 10, 'foo' =>1000),
318+
array('address_id' => 7, 'company' => 17, 'foo' =>1007),
319+
);
320+
321+
$this->assertArrayContainsPsv("
322+
address_id |company
323+
5 |10
324+
7 |17
325+
", $exampleArray, array(), "tests pass if the information in the array is the same for the relevant keys");
326+
327+
328+
$this->assertArrayContainsPsv("
329+
address_id |company
330+
5 |COMPANY_1
331+
ADDRESS_1 |17
332+
", $exampleArray, array( 'COMPANY_1' => 10, 'ADDRESS_1' => 7), "tests pass if the information in the array is the same for the relevant keys -0 using placeholders");
333+
334+
335+
try{
336+
$this->assertArrayContainsPsv("
337+
address_id |company
338+
5 |10
339+
7 |16
340+
", $exampleArray);
341+
$this->fail("assertTableStateContains fails if the information in the array is different for the relevant keys");
342+
} catch(\PHPUnit_Framework_ExpectationFailedException $e){
343+
//do nothing
344+
}
345+
346+
try{
347+
$this->assertArrayContainsPsv("
348+
address_id |company
349+
5 |COMPANY_1
350+
ADDRESS_1 |17
351+
", $exampleArray, array( 'COMPANY_1' => 10, 'ADDRESS_1' => 8));
352+
$this->fail("assertTableStateContains fails if the information in the array is different for the relevant keys - using placeholders");
353+
} catch(\PHPUnit_Framework_ExpectationFailedException $e){
354+
//do nothing
355+
}
356+
357+
358+
try{
359+
$this->assertArrayContainsPsv("
360+
address_id |company
361+
", $exampleArray);
362+
$this->fail("assertTableStateContains fails if the information in the array is empty but actual results are not");
363+
} catch(\PHPUnit_Framework_ExpectationFailedException $e){
364+
//do nothing
365+
}
366+
367+
try{
368+
$this->assertArrayContainsPsv("
369+
address_id |company
370+
1 |5
371+
", array());
372+
$this->fail("assertTableStateContains fails if the information in the array is not but actual results are");
373+
} catch(\PHPUnit_Framework_ExpectationFailedException $e){
374+
//do nothing
375+
}
376+
377+
}
378+
379+
307380
}
308381

309382
class ExampleService

tests/Functional/tests/Php5_4/TraitsSmokeTest.php

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class TraitsSmokeTest extends \TestDbAcleTests\Functional\FunctionalBaseTestCase
66

77
function setup()
88
{
9-
9+
$this->getPdo()->query('DROP TEMPORARY TABLE IF EXISTS address');
1010
$this->getPdo()->query('CREATE TEMPORARY TABLE `address` (
1111
`address_id` int(11) NOT NULL AUTO_INCREMENT,
1212
`company` varchar(100) NOT NULL,
@@ -20,7 +20,7 @@ function setup()
2020
PRIMARY KEY (`address_id`)
2121
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8');
2222

23-
23+
$this->getPdo()->query('DROP TEMPORARY TABLE IF EXISTS user');
2424
$this->getPdo()->query('CREATE TEMPORARY TABLE `user` (
2525
`user_id` int(11) NOT NULL AUTO_INCREMENT,
2626
`name` varchar(100) NOT NULL,
@@ -85,5 +85,75 @@ function test_simpleSetupAndAssertTableStateContains()
8585

8686
}
8787

88+
/**
89+
* @covers \TestDbAcle\Commands\FilterTableStateByPsvCommand::execute()
90+
* @covers \TestDbAcle\Commands\FilterTableStateByPsvCommand::FilterArrayByPsvCommand()
91+
* @covers \TestDbAcle::getDefaultFactories()
92+
*/
93+
function test_assertArrayContainsPsv()
94+
{
95+
96+
$exampleArray = array(
97+
array('address_id' => 5, 'company' => 10, 'foo' =>1000),
98+
array('address_id' => 7, 'company' => 17, 'foo' =>1007),
99+
);
100+
101+
$this->assertArrayContainsPsv("
102+
address_id |company
103+
5 |10
104+
7 |17
105+
", $exampleArray, array(), "tests pass if the information in the array is the same for the relevant keys");
106+
107+
108+
$this->assertArrayContainsPsv("
109+
address_id |company
110+
5 |COMPANY_1
111+
ADDRESS_1 |17
112+
", $exampleArray, array( 'COMPANY_1' => 10, 'ADDRESS_1' => 7), "tests pass if the information in the array is the same for the relevant keys -0 using placeholders");
113+
114+
115+
try{
116+
$this->assertArrayContainsPsv("
117+
address_id |company
118+
5 |10
119+
7 |16
120+
", $exampleArray);
121+
$this->fail("assertTableStateContains fails if the information in the array is different for the relevant keys");
122+
} catch(\PHPUnit_Framework_ExpectationFailedException $e){
123+
//do nothing
124+
}
125+
126+
try{
127+
$this->assertArrayContainsPsv("
128+
address_id |company
129+
5 |COMPANY_1
130+
ADDRESS_1 |17
131+
", $exampleArray, array( 'COMPANY_1' => 10, 'ADDRESS_1' => 8));
132+
$this->fail("assertTableStateContains fails if the information in the array is different for the relevant keys - using placeholders");
133+
} catch(\PHPUnit_Framework_ExpectationFailedException $e){
134+
//do nothing
135+
}
136+
137+
138+
try{
139+
$this->assertArrayContainsPsv("
140+
address_id |company
141+
", $exampleArray);
142+
$this->fail("assertTableStateContains fails if the information in the array is empty but actual results are not");
143+
} catch(\PHPUnit_Framework_ExpectationFailedException $e){
144+
//do nothing
145+
}
146+
147+
try{
148+
$this->assertArrayContainsPsv("
149+
address_id |company
150+
1 |5
151+
", array());
152+
$this->fail("assertTableStateContains fails if the information in the array is not but actual results are");
153+
} catch(\PHPUnit_Framework_ExpectationFailedException $e){
154+
//do nothing
155+
}
156+
157+
}
88158

89159
}

0 commit comments

Comments
 (0)