ShippingMethods::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 4
rs 10
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
namespace Automattic\WooCommerce\RestApi\UnitTests\Tests\Version4;
10
11
defined( 'ABSPATH' ) || exit;
12
13
use \WP_REST_Request;
14
use \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...
15
16
class ShippingMethods extends WC_REST_Unit_Test_Case {
17
18
	/**
19
	 * User variable.
20
	 *
21
	 * @var WP_User
0 ignored issues
show
Bug introduced by
The type Automattic\WooCommerce\R...\Tests\Version4\WP_User was not found. Did you mean WP_User? If so, make sure to prefix the type with \.
Loading history...
22
	 */
23
	protected static $user;
24
25
	/**
26
	 * Setup once before running tests.
27
	 *
28
	 * @param object $factory Factory object.
29
	 */
30
	public static function wpSetUpBeforeClass( $factory ) {
31
		self::$user = $factory->user->create(
32
			array(
33
				'role' => 'administrator',
34
			)
35
		);
36
	}
37
38
	/**
39
	 * Setup our test server, endpoints, and user info.
40
	 */
41
	public function setUp() {
42
		parent::setUp();
43
		wp_set_current_user( self::$user );
0 ignored issues
show
Bug introduced by
self::user of type Automattic\WooCommerce\R...\Tests\Version4\WP_User is incompatible with the type integer expected by parameter $id of wp_set_current_user(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
		wp_set_current_user( /** @scrutinizer ignore-type */ self::$user );
Loading history...
44
		$this->zones = array();
0 ignored issues
show
Bug Best Practice introduced by
The property zones does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
45
	}
46
47
	/**
48
	 * Test route registration.
49
	 *
50
	 * @since 3.5.0
51
	 */
52
	public function test_register_routes() {
53
		$routes = $this->server->get_routes();
54
		$this->assertArrayHasKey( '/wc/v4/shipping_methods', $routes );
55
		$this->assertArrayHasKey( '/wc/v4/shipping_methods/(?P<id>[\w-]+)', $routes );
56
	}
57
58
	/**
59
	 * Test getting all shipping methods.
60
	 *
61
	 * @since 3.5.0
62
	 */
63
	public function test_get_shipping_methods() {
64
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v4/shipping_methods' ) );
65
		$methods  = $response->get_data();
66
67
		$this->assertEquals( 200, $response->get_status() );
68
		$this->assertContains(
69
			array(
70
				'id'          => 'free_shipping',
71
				'title'       => 'Free shipping',
72
				'description' => 'Free shipping is a special method which can be triggered with coupons and minimum spends.',
73
				'_links'      => array(
74
					'self'       => array(
75
						array(
76
							'href' => rest_url( '/wc/v4/shipping_methods/free_shipping' ),
77
						),
78
					),
79
					'collection' => array(
80
						array(
81
							'href' => rest_url( '/wc/v4/shipping_methods' ),
82
						),
83
					),
84
				),
85
			),
86
			$methods
87
		);
88
	}
89
90
	/**
91
	 * Tests to make sure shipping methods cannot viewed without valid permissions.
92
	 *
93
	 * @since 3.5.0
94
	 */
95
	public function test_get_shipping_methods_without_permission() {
96
		wp_set_current_user( 0 );
97
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v4/shipping_methods' ) );
98
		$this->assertEquals( 401, $response->get_status() );
99
	}
100
101
	/**
102
	 * Tests getting a single shipping method.
103
	 *
104
	 * @since 3.5.0
105
	 */
106
	public function test_get_shipping_method() {
107
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v4/shipping_methods/local_pickup' ) );
108
		$method   = $response->get_data();
109
110
		$this->assertEquals( 200, $response->get_status() );
111
		$this->assertEquals(
112
			array(
113
				'id'          => 'local_pickup',
114
				'title'       => 'Local pickup',
115
				'description' => 'Allow customers to pick up orders themselves. By default, when using local pickup store base taxes will apply regardless of customer address.',
116
			),
117
			$method
118
		);
119
	}
120
121
	/**
122
	 * Tests getting a single shipping method without the correct permissions.
123
	 *
124
	 * @since 3.5.0
125
	 */
126
	public function test_get_shipping_method_without_permission() {
127
		wp_set_current_user( 0 );
128
129
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v4/shipping_methods/local_pickup' ) );
130
		$this->assertEquals( 401, $response->get_status() );
131
	}
132
133
	/**
134
	 * Tests getting a shipping method with an invalid ID.
135
	 *
136
	 * @since 3.5.0
137
	 */
138
	public function test_get_shipping_method_invalid_id() {
139
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v4/shipping_methods/fake_method' ) );
140
		$this->assertEquals( 404, $response->get_status() );
141
	}
142
143
	/**
144
	 * Test the shipping method schema.
145
	 *
146
	 * @since 3.5.0
147
	 */
148
	public function test_shipping_method_schema() {
149
		$request    = new WP_REST_Request( 'OPTIONS', '/wc/v4/shipping_methods' );
150
		$response   = $this->server->dispatch( $request );
151
		$data       = $response->get_data();
152
		$properties = $data['schema']['properties'];
153
154
		$this->assertEquals( 3, count( $properties ) );
155
		$this->assertArrayHasKey( 'id', $properties );
156
		$this->assertArrayHasKey( 'title', $properties );
157
		$this->assertArrayHasKey( 'description', $properties );
158
	}
159
}
160