Completed
Push — fix/useBlockEditContext-usage ( 1e6b8b...d10938 )
by
unknown
199:36 queued 191:34
created

WP_Test_Autoloader::setup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php // phpcs:ignore WordPress.Files.FileName
2
/**
3
 * Autoloader test suite.
4
 *
5
 * @package automattic/jetpack-autoloader
6
 */
7
8
use PHPUnit\Framework\TestCase;
9
use Jetpack\TestCase_ABC\ClassName_ABC;
10
11
/**
12
 * Test suite class for the Autoloader.
13
 */
14
class WP_Test_Autoloader extends TestCase {
15
16
	/**
17
	 * Setup runs before each test.
18
	 */
19
	public function setup() {
20
		parent::setup();
21
		$this->classes_handler = new Classes_Handler( new Plugins_Handler(), new Version_Selector() );
22
		spl_autoload_register( 'autoloader' );
23
	}
24
25
	/**
26
	 * Tests whether enqueueing adds a class to the global array.
27
	 */
28 View Code Duplication
	public function test_enqueueing_adds_to_the_global_array() {
29
		$this->classes_handler->enqueue_package_class( 'className', '1', 'path_to_class' );
30
31
		global $jetpack_packages_classmap;
32
		$this->assertTrue( isset( $jetpack_packages_classmap['className'] ) );
33
		$this->assertEquals( $jetpack_packages_classmap['className']['version'], '1' );
34
		$this->assertEquals( $jetpack_packages_classmap['className']['path'], 'path_to_class' );
35
	}
36
37
	/**
38
	 * Tests whether enqueueing adds the latest class version to the global array.
39
	 */
40 View Code Duplication
	public function test_enqueueing_adds_the_latest_version_to_the_global_array() {
41
		$this->classes_handler->enqueue_package_class( 'className', '1', 'path_to_class' );
42
		$this->classes_handler->enqueue_package_class( 'className', '2', 'path_to_class_v2' );
43
44
		global $jetpack_packages_classmap;
45
		$this->assertTrue( isset( $jetpack_packages_classmap['className'] ) );
46
		$this->assertEquals( $jetpack_packages_classmap['className']['version'], '2' );
47
		$this->assertEquals( $jetpack_packages_classmap['className']['path'], 'path_to_class_v2' );
48
49
	}
50
51
	/**
52
	 * Tests whether enqueueing prioritizes the stable version of the class when the
53
	 * JETPACK_AUTOLOAD_DEV constant is not set. This test must be run before
54
	 * 'test_enqueueing_adds_the_dev_version_to_the_global_array' because that test
55
	 * sets JETPACK_AUTOLOAD_DEV.
56
	 */
57
	public function test_enqueueing_does_not_add_the_dev_version_to_the_global_array() {
58
59
		$this->classes_handler->enqueue_package_class( 'className', '1', 'path_to_class' );
60
		$this->classes_handler->enqueue_package_class( 'className', 'dev-howdy', 'path_to_class_dev' );
61
		$this->classes_handler->enqueue_package_class( 'className', '2', 'path_to_class_v2' );
62
63
		global $jetpack_packages_classmap;
64
		$this->assertTrue( isset( $jetpack_packages_classmap['className'] ) );
65
		$this->assertEquals( $jetpack_packages_classmap['className']['version'], '2' );
66
		$this->assertEquals( $jetpack_packages_classmap['className']['path'], 'path_to_class_v2' );
67
	}
68
69
	/**
70
	 * Tests whether enqueueing prioritizes the dev version of the class when the
71
	 * JETPACK_AUTOLOAD_DEV constant is set to true.
72
	 *
73
	 * @runInSeparateProcess
74
	 * @preserveGlobalState disabled
75
	 */
76 View Code Duplication
	public function test_enqueueing_adds_the_dev_version_to_the_global_array() {
77
		defined( 'JETPACK_AUTOLOAD_DEV' ) || define( 'JETPACK_AUTOLOAD_DEV', true );
78
79
		$this->classes_handler->enqueue_package_class( 'className', '1', 'path_to_class' );
80
		$this->classes_handler->enqueue_package_class( 'className', 'dev-howdy', 'path_to_class_dev' );
81
		$this->classes_handler->enqueue_package_class( 'className', '2', 'path_to_class_v2' );
82
83
		global $jetpack_packages_classmap;
84
		$this->assertTrue( isset( $jetpack_packages_classmap['className'] ) );
85
		$this->assertEquals( $jetpack_packages_classmap['className']['version'], 'dev-howdy' );
86
		$this->assertEquals( $jetpack_packages_classmap['className']['path'], 'path_to_class_dev' );
87
	}
88
89
	/**
90
	 * Tests whether enqueueing works with autoloading.
91
	 */
92
	public function test_enqueue_class_to_autoload_works_as_expected() {
93
		$this->classes_handler->enqueue_package_class( 'Jetpack\TestCase_ABC\ClassName_ABC', '1', dirname( __FILE__ ) . '/path_to_class.php' );
94
95
		$class = new ClassName_ABC();
96
97
		$this->assertTrue( $class->return_true() );
98
	}
99
}
100