/
testcase.php
92 lines (61 loc) · 1.69 KB
/
testcase.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
<?php
require_once dirname( __FILE__ ) . '/constraints.php';
abstract class APP_UnitTestCase extends WP_UnitTestCase {
function setUp() {
parent::setUp();
do_action( 'appthemes_first_run' );
$this->catcher = new APP_Mail_Catcher;
}
function tearDown() {
parent::tearDown();
// hooks aren't automatically reset between tests
$this->catcher->detach();
}
function assertMailSentTo( $expected ) {
$results = wp_list_pluck( $this->catcher->get_bounty(), 'to' );
sort( $results );
sort( $expected );
$constraint = new PHPUnit_Framework_Constraint_IsEqual( $expected );
self::assertThat( $results, $constraint );
}
function assertPostCount( $expected ) {
self::assertThat( $GLOBALS['wp_query'], $this->postCount( $expected ) );
}
function assertArrayHasPairs( $expected, $actual ){
foreach( $expected as $key => $value ){
self::assertArrayHasKey( $key, $actual );
self::assertEquals( $value, $actual[ $key ] );
}
}
protected function postCount( $expected ) {
return new APP_Constraint_Post_Count( $expected );
}
}
class APP_Callback_Catcher {
protected $hook;
protected $history = array();
function __construct( $hook ) {
$this->hook = $hook;
add_action( $hook, array( $this, '_catch' ) );
}
function detach() {
remove_action( $this->hook, array( $this, '_catch' ) );
}
function _catch( $args ) {
$this->history[] = func_get_args();
}
function was_called() {
return ! empty( $this->history );
}
}
class APP_Mail_Catcher extends APP_Callback_Catcher {
function __construct() {
parent::__construct( '_wp_mail_sent' );
}
function _catch( $args ) {
$this->history[] = $args;
}
function get_bounty() {
return $this->history;
}
}