Completed
Push — add/wpcom-v2-delete-transient-... ( 7a7987...6ef09e )
by
unknown
374:29 queued 364:27
created

test_prepare_jitms_enqueues_assets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 49
rs 9.1127
c 0
b 0
f 0
1
<?php  // phpcs:disable
2
3
namespace Automattic\Jetpack;
4
5
use Automattic\Jetpack\JITMS\JITM;
6
use Automattic\Jetpack\JITMS\Pre_Connection_JITM;
7
use Brain\Monkey;
8
use Brain\Monkey\Actions;
9
use Brain\Monkey\Functions;
10
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
11
use PHPUnit\Framework\TestCase;
12
13
class Test_Jetpack_JITM extends TestCase {
14
	use MockeryPHPUnitIntegration;
15
16
	/**
17
	 * Set up.
18
	 *
19
	 * @before
20
	 */
21
	public function set_up() {
22
		Monkey\setUp();
23
24
		Functions\when( 'get_current_screen' )->justReturn( new \stdClass() );
25
		Functions\when( 'site_url' )->justReturn( 'unit-test' );
26
		Functions\when( 'wp_get_environment_type' )->justReturn( '' );
27
		Functions\when( 'current_user_can' )->justReturn( true );
28
	}
29
30
	/**
31
	 * Tear down.
32
	 *
33
	 * @after
34
	 */
35
	public function tear_down() {
36
		Monkey\tearDown();
37
	}
38
39 View Code Duplication
	public function test_jitm_enabled_by_default() {
40
		Functions\expect( 'apply_filters' )
41
			->once()
42
			->with(	'jetpack_just_in_time_msgs', true )
43
			->andReturn( true );
44
45
		$jitm = new JITM();
46
		$this->assertTrue( $jitm->register() );
47
	}
48
49 View Code Duplication
	public function test_jitm_disabled_by_filter() {
50
		Functions\expect( 'apply_filters' )
51
			->once()
52
			->with( 'jetpack_just_in_time_msgs', true )
53
			->andReturn( false );
54
55
		$jitm = new JITM();
56
		$this->assertFalse( $jitm->register() );
57
	}
58
59
	/**
60
	 * This is an example of a test which uses Mockery to tests a class static method.
61
	 *
62
	 * It requires the runInSeparateProcess tag so that the class isn't already autoloaded.
63
	 *
64
	 * @runInSeparateProcess
65
	 */
66
	public function test_prepare_jitms_enqueues_assets() {
67
		$mockAssets = \Mockery::mock( 'alias:Automattic\Jetpack\Assets' );
68
69
		// mock the static method and return a dummy value
70
		$mockAssets
71
			->shouldReceive( 'get_file_url_for_environment' )
72
			->andReturn( 'the_file_url' );
73
74
		$jitm = new JITM();
75
		$screen = (object) array( 'id' => 'jetpack_foo' ); // fake screen object
76
		$jitm->prepare_jitms( $screen );
77
78
		// Assert the action was added
79
		$this->assertNotFalse( has_action( 'admin_enqueue_scripts', array( $jitm, 'jitm_enqueue_files' ) ) );
80
81
		// Set up mocks for a bunch of methods called by the hook.
82
		Functions\expect( 'plugins_url' )->once()->andReturn( 'the_plugin_url' );
83
		Functions\when( 'esc_url_raw' )->justReturn( '' );
84
		Functions\when( 'esc_html__' )->justReturn( '' );
85
		Functions\when( 'wp_create_nonce' )->justReturn( '' );
86
		Functions\when( 'rest_url' )->justReturn( '' );
87
		Functions\expect( 'wp_register_style' )->once()->with(
88
			'jetpack-jitm-css',
89
			'the_plugin_url',
90
			false,
91
			\Mockery::type( 'string' )
92
		);
93
		Functions\expect( 'wp_style_add_data' )->with(
94
			'jetpack-jitm-css',
95
			\Mockery::type( 'string' ),
96
			\Mockery::type( 'string' )
97
		);
98
		Functions\expect( 'wp_enqueue_style' )->once()->with( 'jetpack-jitm-css' );
99
		Functions\expect( 'wp_enqueue_script' )->once()->with(
100
			'jetpack-jitm-new',
101
			'the_file_url',
102
			array( 'jquery' ),
103
			JITM::PACKAGE_VERSION,
104
			true
105
		);
106
		Functions\expect( 'wp_localize_script' )->once()->with(
107
			'jetpack-jitm-new',
108
			'jitm_config',
109
			\Mockery::type( 'array' )
110
		);
111
112
		// Do the action that we asserted was added.
113
		$jitm->jitm_enqueue_files();
114
	}
115
116
}
117