-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathabstract-esp-tests.php
118 lines (106 loc) · 3.33 KB
/
abstract-esp-tests.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/**
* Abstract test class to test the common pieces of ESPs.
*
* @package Newspack_Newsletters
*/
/**
* Abstract test class to test the common pieces of ESPs.
*
* This class is intended to test the consistency of the providers abstraction layer. It will mostly test
* if the common methods behave the same way across different ESPs, under the same circumstances.
*
* It will not assert specific values, but it will assert that the expected endpoints are reached and that
* the results are in the same format.
*
* Specific tests for each ESP are still required to test the specific behavior of each provider.
*/
abstract class Abstract_ESP_Tests extends WP_UnitTestCase {
/**
* The current provider slug.
*
* @var string
*/
protected static $provider;
/**
* The endpoints reached during the test. Populate this var as mock endpoints are reached.
*
* @var array
*/
protected static $endpoints_reached = [];
/**
* The endopints we expect the test to reach. Populate this var before the test is executed.
*
* @var array
*/
protected static $expected_endpoints_reached = [];
/**
* Set up
*/
public function set_up() {
Newspack_Newsletters::set_service_provider( static::$provider );
self::$endpoints_reached = [];
self::$expected_endpoints_reached = [];
}
/**
* Sets up the required filters for the get_contact_lists test
*
* @param string $email The email to search for.
* @return void
*/
abstract public function set_up_test_get_contact_lists( $email );
/**
* Tears down the required filters for the get_contact_lists test
*
* @param string $email The email to search for.
* @return void
*/
abstract public function tear_down_test_get_contact_lists( $email );
/**
* Data provider for test_get_contact_lists.
*/
public function get_contact_lists_provider() {
return [
'Should return 4 lists for the contact' => [
'email' => '[email protected]',
'expected' => 4,
],
'Should return 2 lists for the contact' => [
'email' => '[email protected]',
'expected' => 2,
],
'Should return no lists for the contact' => [
'email' => '[email protected]',
'expected' => 0,
],
'Should behave as a non-existent contact' => [
'email' => '[email protected]',
'expected' => 0,
],
'Should simulate an error in the API request' => [
'email' => '[email protected]',
'expected' => false,
],
];
}
/**
* Test get_contact_lists.
*
* @param string $email The email to search for.
* @param int|false $expected The expected number of lists or false if an error is expected.
*
* @dataProvider get_contact_lists_provider
*/
public function test_get_contact_lists( $email, $expected ) {
$provider = Newspack_Newsletters::get_service_provider();
$this->set_up_test_get_contact_lists( $email );
$lists = $provider->get_contact_lists( $email );
$this->assertIsArray( $lists );
$this->assertCount( (int) $expected, $lists ); // get_contact_lists return an empty array in case of an error.
foreach ( $lists as $list ) {
$this->assertIsString( $list );
}
$this->assertSame( self::$expected_endpoints_reached, self::$endpoints_reached, 'The expected endpoints were not reached or didn\'t include the expected parameters' );
$this->tear_down_test_get_contact_lists( $email );
}
}