Shipping_Methods::test_get_shipping_methods()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 0
dl 0
loc 26
rs 9.7
c 0
b 0
f 0
1
<?php
2
/**
3
 * Tests for the Shipping Methods REST API.
4
 *
5
 * @package WooCommerce\Tests\API
6
 * @since 3.5.0
7
 */
8
9
class Shipping_Methods extends WC_REST_Unit_Test_Case {
0 ignored issues
show
Bug introduced by
The type WC_REST_Unit_Test_Case was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
11
	/**
12
	 * Setup our test server, endpoints, and user info.
13
	 */
14
	public function setUp() {
15
		parent::setUp();
16
		$this->endpoint = new WC_REST_Shipping_Methods_Controller();
0 ignored issues
show
Bug Best Practice introduced by
The property endpoint does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
17
		$this->user     = $this->factory->user->create(
0 ignored issues
show
Bug Best Practice introduced by
The property user does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
18
			array(
19
				'role' => 'administrator',
20
			)
21
		);
22
	}
23
24
	/**
25
	 * Test route registration.
26
	 *
27
	 * @since 3.5.0
28
	 */
29
	public function test_register_routes() {
30
		$routes = $this->server->get_routes();
31
		$this->assertArrayHasKey( '/wc/v3/shipping_methods', $routes );
32
		$this->assertArrayHasKey( '/wc/v3/shipping_methods/(?P<id>[\w-]+)', $routes );
33
	}
34
35
	/**
36
	 * Test getting all shipping methods.
37
	 *
38
	 * @since 3.5.0
39
	 */
40
	public function test_get_shipping_methods() {
41
		wp_set_current_user( $this->user );
42
43
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping_methods' ) );
44
		$methods  = $response->get_data();
45
46
		$this->assertEquals( 200, $response->get_status() );
47
		$this->assertContains(
48
			array(
49
				'id'          => 'free_shipping',
50
				'title'       => 'Free shipping',
51
				'description' => 'Free shipping is a special method which can be triggered with coupons and minimum spends.',
52
				'_links'      => array(
53
					'self'       => array(
54
						array(
55
							'href' => rest_url( '/wc/v3/shipping_methods/free_shipping' ),
56
						),
57
					),
58
					'collection' => array(
59
						array(
60
							'href' => rest_url( '/wc/v3/shipping_methods' ),
61
						),
62
					),
63
				),
64
			),
65
			$methods
66
		);
67
	}
68
69
	/**
70
	 * Tests to make sure shipping methods cannot viewed without valid permissions.
71
	 *
72
	 * @since 3.5.0
73
	 */
74
	public function test_get_shipping_methods_without_permission() {
75
		wp_set_current_user( 0 );
76
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping_methods' ) );
77
		$this->assertEquals( 401, $response->get_status() );
78
	}
79
80
	/**
81
	 * Tests getting a single shipping method.
82
	 *
83
	 * @since 3.5.0
84
	 */
85
	public function test_get_shipping_method() {
86
		wp_set_current_user( $this->user );
87
88
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping_methods/local_pickup' ) );
89
		$method   = $response->get_data();
90
91
		$this->assertEquals( 200, $response->get_status() );
92
		$this->assertEquals(
93
			array(
94
				'id'          => 'local_pickup',
95
				'title'       => 'Local pickup',
96
				'description' => 'Allow customers to pick up orders themselves. By default, when using local pickup store base taxes will apply regardless of customer address.',
97
			),
98
			$method
99
		);
100
	}
101
102
	/**
103
	 * Tests getting a single shipping method without the correct permissions.
104
	 *
105
	 * @since 3.5.0
106
	 */
107
	public function test_get_shipping_method_without_permission() {
108
		wp_set_current_user( 0 );
109
110
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping_methods/local_pickup' ) );
111
		$this->assertEquals( 401, $response->get_status() );
112
	}
113
114
	/**
115
	 * Tests getting a shipping method with an invalid ID.
116
	 *
117
	 * @since 3.5.0
118
	 */
119
	public function test_get_shipping_method_invalid_id() {
120
		wp_set_current_user( $this->user );
121
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/shipping_methods/fake_method' ) );
122
		$this->assertEquals( 404, $response->get_status() );
123
	}
124
125
	/**
126
	 * Test the shipping method schema.
127
	 *
128
	 * @since 3.5.0
129
	 */
130
	public function test_shipping_method_schema() {
131
		wp_set_current_user( $this->user );
132
133
		$request    = new WP_REST_Request( 'OPTIONS', '/wc/v3/shipping_methods' );
134
		$response   = $this->server->dispatch( $request );
135
		$data       = $response->get_data();
136
		$properties = $data['schema']['properties'];
137
138
		$this->assertEquals( 3, count( $properties ) );
139
		$this->assertArrayHasKey( 'id', $properties );
140
		$this->assertArrayHasKey( 'title', $properties );
141
		$this->assertArrayHasKey( 'description', $properties );
142
	}
143
}
144