<?php

class Wiaas_Package_Option_Groups_Test extends Wiaas_Unit_Test_Case {

	/**
	 * @covers Wiaas_Package_Option_Groups::register_option_package_type()
	 */
	function test_option_package_type_registered() {
		$package_types = Wiaas_Package_Type::get_available_package_types();

		$this->assertContains('option', $package_types);
	}

	/**
	 * @covers Wiaas_Package_Option_Groups::set_package_option_groups()
	 * @covers Wiaas_Package_Option_Groups::get_package_option_groups()
	 */
	function test_adding_package_option_group() {
		$package = $this->create_new_package();

		$option_package1 = $this->create_new_package();
		Wiaas_Package_Type::set_package_type($option_package1->get_id(), 'option');

		$option_package2 = $this->create_new_package();
		Wiaas_Package_Type::set_package_type($option_package2->get_id(), 'option');

		$option_group1 = array(
			'id' => 'option1',
			'name' => 'Option 1',
			'default' => $option_package1->get_id(),
			'options' => array( $option_package1->get_id() )
		);

		$option_group2 = array(
			'id' => 'option2',
			'name' => 'Option 2',
			'default' => $option_package2->get_id(),
			'options' => array( $option_package2->get_id() )
		);

		Wiaas_Package_Option_Groups::set_package_option_groups($package, array(
			$option_group1,
			$option_group2
		));

		$added_option_groups = Wiaas_Package_Option_Groups::get_package_option_groups($package);

		$this->assertNotEmpty($added_option_groups);
		$this->assertCount(2, $added_option_groups);

		foreach ($added_option_groups as $index => $added_option_group) {
			$this->assertNotEmpty($added_option_group);

			$this->assertArrayHasKey('id', $added_option_group);
			$this->assertArrayHasKey('name', $added_option_group);
			$this->assertArrayHasKey('default', $added_option_group);
			$this->assertArrayHasKey('options', $added_option_group);

			$this->assertEquals($added_option_group['id'], 'option' . ($index + 1));
			$this->assertEquals($added_option_group['name'], 'Option ' . ($index + 1));
			$this->assertCount(1, $added_option_group['options']);

			$added_option = $added_option_group['options'][0];
			$this->assertNotNull($added_option);

			$relevant_option = null;
			if ($index === 0) {
				$relevant_option = $option_package1;
			} else {
				$relevant_option = $option_package2;
			}

			$this->assertEquals($added_option_group['default'], $relevant_option->get_id());
			$this->assertEquals($added_option->get_id(), $relevant_option->get_id());
		}
	}

	/**
	 * @covers Wiaas_Package_Option_Groups::get_group_name_for_package_option()
	 */
	function test_get_group_name_for_package_option() {
		$package = $this->create_new_package();

		$option_package = $this->create_new_package();
		Wiaas_Package_Type::set_package_type($option_package->get_id(), 'option');

		$option_group = array(
			'id' => 'option1',
			'name' => 'Option 1',
			'default' => $option_package->get_id(),
			'options' => array( $option_package->get_id() )
		);

		Wiaas_Package_Option_Groups::set_package_option_groups($package, array( $option_group ));

		$option_group_name = Wiaas_Package_Option_Groups::get_group_name_for_package_option($package, $option_package);

		$this->assertEquals($option_group_name, $option_group['name']);
	}
}