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

Commit 27a011c

Browse files
author
speckmaier
committed
test for compareArray used in withMessage
1 parent b43ca66 commit 27a011c

File tree

4 files changed

+145
-23
lines changed

4 files changed

+145
-23
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
},
3838
"require-dev": {
3939
"mockery/mockery": "^1.2",
40-
"orchestra/testbench": "^3.5"
40+
"orchestra/testbench": "^3.5",
41+
"phpunit/phpunit": "^7.0"
4142
}
4243
}

src/Test/TestsRabbitMQ.php

+44-22
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,7 @@ protected function onExchange($exchangeName)
7272
protected function withMessage( $expectedData ) {
7373
$this->rabbitMQ->shouldReceive( 'onExchange' )->with( $this->expectedExchangeName, $this->expectedRoutingKey )->once()->andReturnSelf();
7474
$this->rabbitMQ->shouldReceive( 'publish' )->withArgs( function ( $data ) use ( $expectedData ) {
75-
$anyMatch = false;
76-
77-
foreach ( $expectedData as $expectedKey => $expectedValue ) {
78-
79-
if ( !array_key_exists( $expectedKey, $data ) )
80-
return false;
81-
82-
if( is_float($data[ $expectedKey ]))
83-
$data[ $expectedKey ] = round($data[ $expectedKey ], 9);
84-
85-
if( is_float($expectedValue))
86-
$expectedValue = round($expectedValue, 9);
87-
88-
if ( $data[ $expectedKey ] != $expectedValue )
89-
return false;
90-
91-
$anyMatch = true;
92-
}
93-
94-
return $anyMatch;
75+
return $this->compareArray($expectedData, $data);
9576
} )->once()->andReturnSelf();
9677
}
9778

@@ -100,7 +81,48 @@ protected function withMessage( $expectedData ) {
10081
* @param array $expectedData
10182
*/
10283
protected function withAnyMessage() {
103-
$this->rabbitMQ->shouldReceive( 'onExchange' )->with( $this->expectedExchangeName, $this->expectedRoutingKey )->once()->andReturnSelf();
104-
$this->rabbitMQ->shouldReceive( 'publish' );
84+
$this->rabbitMQ->shouldReceive( 'onExchange' )
85+
->with(
86+
$this->expectedExchangeName,
87+
$this->expectedRoutingKey
88+
)
89+
->once()
90+
->andReturnSelf();
91+
$this->rabbitMQ->shouldReceive( 'publish' );
92+
}
93+
94+
private function compareArray($expectedData, $data)
95+
{
96+
$anyMatch = false;
97+
foreach ( $expectedData as $expectedKey => $expectedValue ) {
98+
99+
if ( !array_key_exists( $expectedKey, $data ) )
100+
return false;
101+
102+
if( $this->compare($expectedValue, $data[$expectedKey]) ) {
103+
$anyMatch = true;
104+
}
105+
}
106+
107+
return $anyMatch;
108+
}
109+
110+
private function compare($expectedValue, $actualValue)
111+
{
112+
if( is_float($actualValue))
113+
$actualValue = round($actualValue, 9);
114+
115+
if( is_float($expectedValue))
116+
$expectedValue = round($expectedValue, 9);
117+
118+
if( is_array($expectedValue) && is_array($actualValue) ) {
119+
return $this->compareArray($expectedValue, $actualValue);
120+
}
121+
122+
if ( $actualValue != $expectedValue ) {
123+
return false;
124+
}
125+
126+
return true;
105127
}
106128
}

tests/TestCase.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php namespace Ipunkt\RabbitMQTests;
2+
3+
/**
4+
* Class TestCase
5+
* @package Ipunkt\RabbitMQTests
6+
*/
7+
class TestCase extends \Orchestra\Testbench\TestCase
8+
{
9+
10+
}

tests/Unit/TestsRabbitMQTest.php

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php namespace Ipunkt\RabbitMQTests\Unit;
2+
3+
use Ipunkt\RabbitMQ\Test\TestsRabbitMQ;
4+
use Ipunkt\RabbitMQTests\TestCase;
5+
6+
/**
7+
* Class TestsRabbitMQTest
8+
* @package Ipunkt\RabbitMQTests\Unit
9+
*/
10+
class TestsRabbitMQTest extends TestCase
11+
{
12+
use TestsRabbitMQ;
13+
14+
/**
15+
* @test
16+
*/
17+
public function compare_array_works_with_simple_values()
18+
{
19+
$expected = [
20+
'a' => 5,
21+
];
22+
$actual = [
23+
'a' => 5,
24+
];
25+
26+
$result = $this->compareArray($expected, $actual);
27+
28+
$this->assertTrue($result);
29+
}
30+
31+
/**
32+
* @test
33+
*/
34+
public function compare_array_finds_differences_in_simple_values()
35+
{
36+
$expected = [
37+
'a' => 5,
38+
];
39+
$actual = [
40+
'a' => 6,
41+
];
42+
43+
$result = $this->compareArray($expected, $actual);
44+
45+
$this->assertFalse($result);
46+
}
47+
48+
/**
49+
* @test
50+
*/
51+
public function compare_array_works_with_nested_arrays()
52+
{
53+
$expected = [
54+
'a' => [
55+
'b' => 5,
56+
]
57+
];
58+
$actual = [
59+
'a' => [
60+
'b' => 5,
61+
]
62+
];
63+
64+
$result = $this->compareArray($expected, $actual);
65+
66+
$this->assertTrue($result);
67+
}
68+
69+
/**
70+
* @test
71+
*/
72+
public function compare_array_finds_differences_with_nested_arrays()
73+
{
74+
$expected = [
75+
'a' => [
76+
'b' => 5,
77+
]
78+
];
79+
$actual = [
80+
'a' => [
81+
'b' => 6,
82+
]
83+
];
84+
85+
$result = $this->compareArray($expected, $actual);
86+
87+
$this->assertFalse($result);
88+
}
89+
}

0 commit comments

Comments
 (0)