<?php

/**
 * Class Wiaas_Countries_Test
 */
class Wiaas_Countries_Test extends Wiaas_Unit_Test_Case {

	var $product, $package, $current_country = 'Sweden';

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

		# set admin as current user
		wp_set_current_user(1);

		$this->product = $this->create_new_product();
		wp_set_object_terms($this->product->get_id(), $this->current_country, 'product_country', false);
		clean_object_term_cache( $this->product->get_id(), 'product_country' );


		$this->package = $this->create_new_package();
		wp_set_object_terms($this->package->get_id(), $this->current_country, 'product_country', false);
		clean_object_term_cache( $this->package->get_id(), 'product_country' );
	}


	/**
	 * @covers Wiaas_Countries::register_product_countries_taxonomy()
	 */
	function test_available_countries_created() {
		// test taxonomy is available
		$taxonomy = get_taxonomy('product_country');

		$this->assertInstanceOf(WP_Taxonomy::class, $taxonomy);

		$country_names = array_map(function($term) {
			return $term->name;
		}, get_terms(array( 'taxonomy' => 'product_country', 'hide_empty' => false )));

		$this->assertNotEmpty($country_names);

		$this->assertContains('Sweden', $country_names);
		$this->assertContains('Denmark', $country_names);
		$this->assertContains('Finland', $country_names);
	}

	/**
	 * @covers Wiaas_Countries::get_product_country()
	 */
	function test_get_product_country() {

		$retrieved_country = Wiaas_Countries::get_product_country($this->product);

		$this->assertNotNull($retrieved_country, 'Product has not country!');

		$this->assertEquals($retrieved_country['name'], $this->current_country, 'Retrieved product country is incorrect!');

	}

	/**
	 * @covers Wiaas_Countries::get_package_country()
	 */
	function test_get_package_country() {
		$retrieved_country = Wiaas_Countries::get_package_country($this->package);

		$this->assertNotNull($retrieved_country, 'Package has not country!');

		$this->assertEquals($retrieved_country['name'], $this->current_country, 'Retrieved package country is incorrect!');
	}

}