Completed
Push — improve/rename-anti-spam-in-si... ( e5a567...f08131 )
by
unknown
75:27 queued 67:29
created

StatsTest::test_add_get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 43
rs 9.232
c 0
b 0
f 0
1
<?php // phpcs:ignore Squiz.Commenting.FileComment.Missing
2
3
namespace Automattic\Jetpack;
4
5
use PHPUnit\Framework\TestCase;
6
7
/**
8
 * Test A8c_Mc_Stats class
9
 */
10
class StatsTest extends TestCase {
11
12
	/**
13
	 * Test add and get_current_status methods
14
	 */
15
	public function test_add_get() {
16
17
		$stats = new A8c_Mc_Stats();
18
19
		$stats->add( 'group', 'test' );
20
21
		$check = $stats->get_current_stats();
22
23
		$this->assertCount( 1, $check );
24
		$this->assertArrayHasKey( 'group', $check );
25
		$this->assertContains( 'test', $check['group'] );
26
		$this->assertCount( 1, $check['group'] );
27
28
		$stats->add( 'group', 'test2' );
29
30
		$check = $stats->get_current_stats();
31
32
		$this->assertCount( 1, $check );
33
		$this->assertArrayHasKey( 'group', $check );
34
		$this->assertContains( 'test', $check['group'] );
35
		$this->assertContains( 'test2', $check['group'] );
36
		$this->assertCount( 2, $check['group'] );
37
38
		$stats->add( 'group2', 'test3' );
39
40
		$check = $stats->get_current_stats();
41
42
		$this->assertCount( 2, $check );
43
		$this->assertArrayHasKey( 'group', $check );
44
		$this->assertArrayHasKey( 'group2', $check );
45
		$this->assertContains( 'test', $check['group'] );
46
		$this->assertContains( 'test2', $check['group'] );
47
		$this->assertContains( 'test3', $check['group2'] );
48
		$this->assertCount( 2, $check['group'] );
49
		$this->assertCount( 1, $check['group2'] );
50
51
		// test errors.
52
53
		$this->assertFalse( $stats->add( 'group2', 'test3' ) );
54
		$this->assertFalse( $stats->add( true, 'test3' ) );
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
55
		$this->assertFalse( $stats->add( array( 123 ), 'test3' ) );
0 ignored issues
show
Documentation introduced by
array(123) is of type array<integer,integer,{"0":"integer"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
56
57
	}
58
59
	/**
60
	 * Test get group query args
61
	 */
62
	public function test_get_group_query_args() {
63
64
		$stats = new A8c_Mc_Stats();
65
		$stats->add( 'group', 'test' );
66
		$stats->add( 'group', 'test2' );
67
68
		$this->assertEmpty( $stats->get_group_query_args( 'group2' ) );
69
70
		$check = $stats->get_group_query_args( 'group' );
71
72
		$this->assertCount( 1, $check );
73
		$this->assertArrayHasKey( 'x_jetpack-group', $check );
74
		$this->assertEquals( 'test,test2', $check['x_jetpack-group'] );
75
76
	}
77
78
}
79