<?php

class Wiaas_Order_Project_API_Test extends Wiaas_API_Unit_Test_Case {

	function setUp() {
		parent::setUp();
	}

	/**
	 * @covers Wiaas_Order_Projects_API::get_order_projects()
	 */
	function test_get_order_projects_as_guest() {
		$this->check_endpoint_forbidden_for_guest(
			new WP_REST_Request( 'GET', '/wiaas/order-projects')
		);
	}

	/**
	 * @covers Wiaas_Order_Projects_API::get_order_projects()
	 */
	function test_get_order_projects_as_customer() {
		Wiaas_Order_Project::add_order_project('Test Available 1', true);
		Wiaas_Order_Project::add_order_project('Test Available 2', true);

		$customer_id = $this->create_new_customer();
		wp_set_current_user($customer_id);

		$data = $this->dispatch_endpoint_request(
			new WP_REST_Request( 'GET', '/wiaas/order-projects')
		);
		$this->assertNotNull($data);
		$this->assertTrue(is_array($data));

		$this->assertCount(2, $data);
		foreach ($data as $project) {
			$this->assertArrayHasKey('id', $project);
			$this->assertArrayHasKey('name', $project);

			$this->assertContains($project['name'], array(
				'Test Available 1',
				'Test Available 2',
			));
		}
	}

	/**
	 * @covers Wiaas_Order_Projects_API::create_order_project()
	 */
	function test_create_order_project_as_guest() {
		$request = new WP_REST_Request( 'POST', '/wiaas/order-projects');
		$request->set_body_params(array(
			'name' => 'Test'
		));
		$this->check_endpoint_forbidden_for_guest($request);
	}

	/**
	 * @covers Wiaas_Order_Projects_API::create_order_project()
	 */
	function test_create_valid_order_project_as_customer() {
		$customer_id = $this->create_new_customer();
		wp_set_current_user($customer_id);

		$request = new WP_REST_Request( 'POST', '/wiaas/order-projects');
		$request->set_body_params(array(
			'name' => 'Test Project'
		));
		$data = $this->dispatch_endpoint_request($request);

		$this->assertNotNull($data);
		$this->assertTrue(is_array($data));

		$this->assertArrayHasKey('messages', $data);
		$this->assertNotEmpty($data['messages']);

		$message = $data['messages'][0];
		$this->assertArrayHasKey('code', $message);
		$this->assertEquals('success', $message['code']);
	}

}