Skip to content
This repository has been archived by the owner on Jun 13, 2023. It is now read-only.

Commit

Permalink
test for compareArray used in withMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
speckmaier committed Oct 8, 2020
1 parent b43ca66 commit 27a011c
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 23 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
},
"require-dev": {
"mockery/mockery": "^1.2",
"orchestra/testbench": "^3.5"
"orchestra/testbench": "^3.5",
"phpunit/phpunit": "^7.0"
}
}
66 changes: 44 additions & 22 deletions src/Test/TestsRabbitMQ.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,7 @@ protected function onExchange($exchangeName)
protected function withMessage( $expectedData ) {
$this->rabbitMQ->shouldReceive( 'onExchange' )->with( $this->expectedExchangeName, $this->expectedRoutingKey )->once()->andReturnSelf();
$this->rabbitMQ->shouldReceive( 'publish' )->withArgs( function ( $data ) use ( $expectedData ) {
$anyMatch = false;

foreach ( $expectedData as $expectedKey => $expectedValue ) {

if ( !array_key_exists( $expectedKey, $data ) )
return false;

if( is_float($data[ $expectedKey ]))
$data[ $expectedKey ] = round($data[ $expectedKey ], 9);

if( is_float($expectedValue))
$expectedValue = round($expectedValue, 9);

if ( $data[ $expectedKey ] != $expectedValue )
return false;

$anyMatch = true;
}

return $anyMatch;
return $this->compareArray($expectedData, $data);
} )->once()->andReturnSelf();
}

Expand All @@ -100,7 +81,48 @@ protected function withMessage( $expectedData ) {
* @param array $expectedData
*/
protected function withAnyMessage() {
$this->rabbitMQ->shouldReceive( 'onExchange' )->with( $this->expectedExchangeName, $this->expectedRoutingKey )->once()->andReturnSelf();
$this->rabbitMQ->shouldReceive( 'publish' );
$this->rabbitMQ->shouldReceive( 'onExchange' )
->with(
$this->expectedExchangeName,
$this->expectedRoutingKey
)
->once()
->andReturnSelf();
$this->rabbitMQ->shouldReceive( 'publish' );
}

private function compareArray($expectedData, $data)
{
$anyMatch = false;
foreach ( $expectedData as $expectedKey => $expectedValue ) {

if ( !array_key_exists( $expectedKey, $data ) )
return false;

if( $this->compare($expectedValue, $data[$expectedKey]) ) {
$anyMatch = true;
}
}

return $anyMatch;
}

private function compare($expectedValue, $actualValue)
{
if( is_float($actualValue))
$actualValue = round($actualValue, 9);

if( is_float($expectedValue))
$expectedValue = round($expectedValue, 9);

if( is_array($expectedValue) && is_array($actualValue) ) {
return $this->compareArray($expectedValue, $actualValue);
}

if ( $actualValue != $expectedValue ) {
return false;
}

return true;
}
}
10 changes: 10 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php namespace Ipunkt\RabbitMQTests;

/**
* Class TestCase
* @package Ipunkt\RabbitMQTests
*/
class TestCase extends \Orchestra\Testbench\TestCase
{

}
89 changes: 89 additions & 0 deletions tests/Unit/TestsRabbitMQTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php namespace Ipunkt\RabbitMQTests\Unit;

use Ipunkt\RabbitMQ\Test\TestsRabbitMQ;
use Ipunkt\RabbitMQTests\TestCase;

/**
* Class TestsRabbitMQTest
* @package Ipunkt\RabbitMQTests\Unit
*/
class TestsRabbitMQTest extends TestCase
{
use TestsRabbitMQ;

/**
* @test
*/
public function compare_array_works_with_simple_values()
{
$expected = [
'a' => 5,
];
$actual = [
'a' => 5,
];

$result = $this->compareArray($expected, $actual);

$this->assertTrue($result);
}

/**
* @test
*/
public function compare_array_finds_differences_in_simple_values()
{
$expected = [
'a' => 5,
];
$actual = [
'a' => 6,
];

$result = $this->compareArray($expected, $actual);

$this->assertFalse($result);
}

/**
* @test
*/
public function compare_array_works_with_nested_arrays()
{
$expected = [
'a' => [
'b' => 5,
]
];
$actual = [
'a' => [
'b' => 5,
]
];

$result = $this->compareArray($expected, $actual);

$this->assertTrue($result);
}

/**
* @test
*/
public function compare_array_finds_differences_with_nested_arrays()
{
$expected = [
'a' => [
'b' => 5,
]
];
$actual = [
'a' => [
'b' => 6,
]
];

$result = $this->compareArray($expected, $actual);

$this->assertFalse($result);
}
}

0 comments on commit 27a011c

Please sign in to comment.