<?php

class Wiaas_Order_Project_Test extends Wiaas_Unit_Test_Case {

	/**
	 * @covers Wiaas_Order_Project::register_order_project_taxonomy()
	 */
	function test_order_project_taxonomy_created() {
		$taxonomy = get_taxonomy('shop_order_project');
		$this->assertInstanceOf(WP_Taxonomy::class, $taxonomy);
	}

	/**
	 * @covers Wiaas_Order_Project::add_order_project()
	 */
	function test_adding_order_project() {
		Wiaas_Order_Project::add_order_project('Test', true);

		$added_order_project = get_term_by('name', 'Test', 'shop_order_project');

		$this->assertInstanceOf(WP_Term::class, $added_order_project);
		$this->assertEquals($added_order_project->name, 'Test');

		$saved_available_flag = get_term_meta($added_order_project->term_id, '_wiaas_is_available', true);
		$this->assertEquals('yes', $saved_available_flag);

	}

	/**
	 * @covers Wiaas_Order_Project::is_order_project_available()
	 */
	function test_is_order_project_available() {
		Wiaas_Order_Project::add_order_project('Test', true);
		$added_order_project = get_term_by('name', 'Test', 'shop_order_project');

		$this->assertTrue(Wiaas_Order_Project::is_order_project_available($added_order_project));

		$this->assertTrue(Wiaas_Order_Project::is_order_project_available($added_order_project->term_id));
	}

	/**
	 * @covers Wiaas_Order_Project::set_is_order_project_available()
	 */
	function test_set_is_order_project_available() {
		Wiaas_Order_Project::add_order_project('Test', true);
		$added_order_project = get_term_by('name', 'Test', 'shop_order_project');

		Wiaas_Order_Project::set_is_order_project_available($added_order_project, false);
		$saved_available_flag = get_term_meta($added_order_project->term_id, '_wiaas_is_available', true);
		$this->assertEquals('no', $saved_available_flag);

		Wiaas_Order_Project::set_is_order_project_available($added_order_project->term_id, true);
		$saved_available_flag = get_term_meta($added_order_project->term_id, '_wiaas_is_available', true);
		$this->assertEquals('yes', $saved_available_flag);
	}

	/**
	 * @covers Wiaas_Order_Project::get_available_order_projects()
	 */
	function test_get_available_order_projects() {
		Wiaas_Order_Project::add_order_project('Test Available 1', true);
		Wiaas_Order_Project::add_order_project('Test Available 2', true);
		Wiaas_Order_Project::add_order_project('Test Available 3', true);

		Wiaas_Order_Project::add_order_project('Test Not Available 1', false);
		Wiaas_Order_Project::add_order_project('Test Not Available 2', false);
		Wiaas_Order_Project::add_order_project('Test Not Available 3', false);

		$available_projects = Wiaas_Order_Project::get_available_order_projects();

		$this->assertCount(3, $available_projects);

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

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