1
|
|
|
<?php |
2
|
|
|
namespace Automattic\Jetpack\Partners; |
3
|
|
|
|
4
|
|
|
use Automattic\Jetpack\Constants as Jetpack_Constants; |
5
|
|
|
use phpmock\Mock; |
6
|
|
|
use phpmock\MockBuilder; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
class Test_Affiliate extends TestCase { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Mock affiliate code. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $aff_code = 'abc123'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Test teardown. |
20
|
|
|
*/ |
21
|
|
|
public function tearDown() { |
22
|
|
|
Mock::disableAll(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
View Code Duplication |
function test_affiliate_code_missing() { |
26
|
|
|
$this->mock_function_with_args( 'get_option', [ |
27
|
|
|
[ |
28
|
|
|
'jetpack_affiliate_code', |
29
|
|
|
'', |
30
|
|
|
'' |
31
|
|
|
] |
32
|
|
|
] ); |
33
|
|
|
|
34
|
|
|
$this->mock_function_with_args( 'apply_filters', [ |
35
|
|
|
[ |
36
|
|
|
'jetpack_affiliate_code', |
37
|
|
|
get_option( 'jetpack_affiliate_code', '' ), |
38
|
|
|
get_option( 'jetpack_affiliate_code', '' ) |
39
|
|
|
] |
40
|
|
|
] ); |
41
|
|
|
|
42
|
|
|
$this->assertEmpty( Affiliate::init()->get_affiliate_code() ); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
View Code Duplication |
function test_affiliate_code_exists() { |
46
|
|
|
$this->mock_function_with_args( 'get_option', [ |
47
|
|
|
[ |
48
|
|
|
'jetpack_affiliate_code', |
49
|
|
|
'', |
50
|
|
|
$this->aff_code |
51
|
|
|
] |
52
|
|
|
] ); |
53
|
|
|
|
54
|
|
|
$this->mock_function_with_args( 'apply_filters', [ |
55
|
|
|
[ |
56
|
|
|
'jetpack_affiliate_code', |
57
|
|
|
get_option( 'jetpack_affiliate_code', '' ), |
58
|
|
|
get_option( 'jetpack_affiliate_code', '' ) |
59
|
|
|
] |
60
|
|
|
] ); |
61
|
|
|
|
62
|
|
|
$this->assertEquals( 'abc123', Affiliate::init()->get_affiliate_code() ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Mock a global function with particular arguments and make it return a certain value. |
67
|
|
|
* |
68
|
|
|
* @param string $function_name Name of the function. |
69
|
|
|
* @param array $args Array of argument sets, last value of each set is used as a return value. |
70
|
|
|
* @return phpmock\Mock The mock object. |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
protected function mock_function_with_args( $function_name, $args = array() ) { |
73
|
|
|
$builder = new MockBuilder(); |
74
|
|
|
$builder->setNamespace( __NAMESPACE__ ) |
75
|
|
|
->setName( $function_name ) |
76
|
|
|
->setFunction( |
77
|
|
|
function() use ( &$args ) { |
78
|
|
|
$current_args = func_get_args(); |
79
|
|
|
|
80
|
|
|
foreach ( $args as $arg ) { |
81
|
|
|
if ( array_slice( $arg, 0, -1 ) === $current_args ) { |
82
|
|
|
return array_pop( $arg ); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$mock = $builder->build(); |
89
|
|
|
$mock->enable(); |
90
|
|
|
|
91
|
|
|
return $mock; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
|