diff --git a/.gitignore b/.gitignore index af6052f..082d69c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,3 @@ -/.settings -/.buildpath -/.project /cov /vendor /composer.phar diff --git a/.travis.yml b/.travis.yml index 7d42f01..1814bec 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,9 +6,12 @@ php: - "5.3" - "hhvm" +# Allow to use container infrastructure +sudo: false + before_script: - composer self-update - composer update -script: phpunit --coverage-text +script: ./bin/phpspec run spec/ diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 29c3ca4..d36999d 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -1,10 +1,12 @@ parameters: + akeneo_measure.convert.measure_converter.class: Akeneo\Bundle\MeasureBundle\Convert\MeasureConverter + akeneo_measure.manager.measure_manager.class: Akeneo\Bundle\MeasureBundle\Manager\MeasureManager akeneo_measure.measures_config: ~ services: akeneo_measure.measure_converter: - class: Akeneo\Bundle\MeasureBundle\Convert\MeasureConverter + class: %akeneo_measure.convert.measure_converter.class% arguments: [%akeneo_measure.measures_config%] akeneo_measure.manager: - class: Akeneo\Bundle\MeasureBundle\Manager\MeasureManager + class: %akeneo_measure.manager.measure_manager.class% diff --git a/Tests/Unit/Convert/MeasureConverterTest.php b/Tests/Unit/Convert/MeasureConverterTest.php deleted file mode 100644 index 37bf332..0000000 --- a/Tests/Unit/Convert/MeasureConverterTest.php +++ /dev/null @@ -1,326 +0,0 @@ -initializeConfig($configFile); - - $this->converter = $this->initializeConverter($config); - } - - /** - * Initialize a configuration - * @param string $filepath - * - * @throws \Exception - * - * @return array - */ - protected function initializeConfig($filepath) - { - if (!file_exists($filepath)) { - throw new \Exception("Config file $filepath not exists"); - } - - return Yaml::parse(file_get_contents($filepath)); - } - - /** - * Initialize converter - * @param array $config - * - * @throws \Exception - * - * @return \Akeneo\Bundle\MeasureBundle\Convert\MeasureConverter - */ - protected function initializeConverter($config) - { - return new MeasureConverter($config); - } - - /** - * Test converter with data in provider method - * @param string $family MeasureFamily for conversion - * @param string $baseUnit Base unit used by base value - * @param string $convertedUnit Converted unit asked - * @param number $baseValue Base value - * @param number $expectedValue Expected value after conversion - * - * @dataProvider provider - */ - public function testConvert($family, $baseUnit, $convertedUnit, $baseValue, $expectedValue) - { - $this->converter->setFamily($family); - - $convertedValue = $this->converter->convert($baseUnit, $convertedUnit, $baseValue); - $this->assertEquals($expectedValue, $convertedValue); - - $resultValue = $this->converter->convert($convertedUnit, $baseUnit, $convertedValue); - $this->assertEquals($baseValue, $resultValue); - } - - /** - * Data provider - * @SuppressWarnings(PHPMD.ExcessiveMethodLength) - * - * @return array - * - * @static - */ - public static function provider() - { - return array( - // Area tests - array( - AreaFamilyInterface::FAMILY, AreaFamilyInterface::SQUARE_METER, AreaFamilyInterface::HECTARE, 200, 0.02 - ), - array( - AreaFamilyInterface::FAMILY, - AreaFamilyInterface::SQUARE_INCH, - AreaFamilyInterface::SQUARE_MIL, - 10, - 10000000 - ), - array( - AreaFamilyInterface::FAMILY, - AreaFamilyInterface::SQUARE_FURLONG, - AreaFamilyInterface::SQUARE_MILE, - 100, - 1.5625062462063 - ), - // Binary tests - array( - BinaryFamilyInterface::FAMILY, BinaryFamilyInterface::BYTE, BinaryFamilyInterface::KILOBYTE, 1024, 1 - ), - array( - BinaryFamilyInterface::FAMILY, - BinaryFamilyInterface::GIGABYTE, - BinaryFamilyInterface::BIT, - 2, - 17179869184 - ), - // Frequency tests - array( - FrequencyFamilyInterface::FAMILY, - FrequencyFamilyInterface::HERTZ, - FrequencyFamilyInterface::KILOHERTZ, - 10000, - 10 - ), - array( - FrequencyFamilyInterface::FAMILY, - FrequencyFamilyInterface::TERAHERTZ, - FrequencyFamilyInterface::MEGAHERTZ, - 2, - 2000000 - ), - // Length tests - array( - LengthFamilyInterface::FAMILY, - LengthFamilyInterface::MILLIMETER, - LengthFamilyInterface::CENTIMETER, - 50, - 5 - ), - array( - LengthFamilyInterface::FAMILY, - LengthFamilyInterface::INCH, - LengthFamilyInterface::MILE, - 15000, - 0.23674242424242 - ), - array( - LengthFamilyInterface::FAMILY, - LengthFamilyInterface::KILOMETER, - LengthFamilyInterface::YARD, - 10, - 10936.132983377 - ), - // Power tests - array( - PowerFamilyInterface::FAMILY, PowerFamilyInterface::WATT, PowerFamilyInterface::KILOWATT, 10000, 10 - ), - array( - PowerFamilyInterface::FAMILY, PowerFamilyInterface::TERAWATT, PowerFamilyInterface::MEGAWATT, 2, 2000000 - ), - array( - PowerFamilyInterface::FAMILY, PowerFamilyInterface::GIGAWATT, PowerFamilyInterface::TERAWATT, 5, 0.005 - ), - // Speed tests - array( - SpeedFamilyInterface::FAMILY, - SpeedFamilyInterface::KILOMETER_PER_HOUR, - SpeedFamilyInterface::METER_PER_SECOND, - 36, - 10 - ), - array( - SpeedFamilyInterface::FAMILY, - SpeedFamilyInterface::FOOT_PER_SECOND, - SpeedFamilyInterface::MILE_PER_HOUR, - 17, - 11.590909090909 - ), - array( - SpeedFamilyInterface::FAMILY, - SpeedFamilyInterface::YARD_PER_HOUR, - SpeedFamilyInterface::METER_PER_MINUTE, - 26, - 0.39624 - ), - // Temperature tests - array( - TemperatureFamilyInterface::FAMILY, - TemperatureFamilyInterface::CELSIUS, - TemperatureFamilyInterface::FAHRENHEIT, - 20, - 68 - ), - array( - TemperatureFamilyInterface::FAMILY, - TemperatureFamilyInterface::REAUMUR, - TemperatureFamilyInterface::RANKINE, - 6, - 505.17 - ), - array( - TemperatureFamilyInterface::FAMILY, - TemperatureFamilyInterface::FAHRENHEIT, - TemperatureFamilyInterface::REAUMUR, - 26720.33, - 11861.48 - ), - // Volume tests - array( - VolumeFamilyInterface::FAMILY, VolumeFamilyInterface::CUBIC_METER, VolumeFamilyInterface::LITER, 5, 5000 - ), - array( - VolumeFamilyInterface::FAMILY, - VolumeFamilyInterface::MILLILITER, - VolumeFamilyInterface::PINT, - 568.26125, - 1 - ), - array( - VolumeFamilyInterface::FAMILY, - VolumeFamilyInterface::CUBIC_INCH, - VolumeFamilyInterface::OUNCE, - 12, - 6.9209283170784 - ), - // Weight tests - array( - WeightFamilyInterface::FAMILY, - WeightFamilyInterface::GRAM, - WeightFamilyInterface::OUNCE, - 3059, - 107.90304960377 - ), - array( - WeightFamilyInterface::FAMILY, - WeightFamilyInterface::POUND, - WeightFamilyInterface::KILOGRAM, - 2, - 0.90718474 - ), - array( - WeightFamilyInterface::FAMILY, - WeightFamilyInterface::GRAIN, - WeightFamilyInterface::MARC, - 500, - 0.13237775280899 - ), - ); - } - - /** - * Test related exception - * @expectedException Akeneo\Bundle\MeasureBundle\Exception\UnknownFamilyMeasureException - */ - public function testUnknownFamilyMeasureException() - { - $this->converter->setFamily('test-family'); - } - - /** - * Test related exception in base to standard method - * @expectedException Akeneo\Bundle\MeasureBundle\Exception\UnknownMeasureException - */ - public function testUnknownMeasureExceptionBaseToStandard() - { - $this->converter->setFamily(WeightFamilyInterface::FAMILY); - $this->converter->convert('test-unit', WeightFamilyInterface::GRAM, 50); - } - - /** - * Test related exception in standard to final method - * @expectedException Akeneo\Bundle\MeasureBundle\Exception\UnknownMeasureException - */ - public function testUnknownMeasureExceptionStandardToFinal() - { - $this->converter->setFamily(WeightFamilyInterface::FAMILY); - $this->converter->convert(WeightFamilyInterface::GRAM, 'test-unit', 50); - } - - /** - * Test related exception in base to standard method - * @expectedException Akeneo\Bundle\MeasureBundle\Exception\UnknownOperatorException - */ - public function testUnknownOperatorExceptionBaseToStandard() - { - $configFile = dirname(__FILE__) .'/../Files/measure-test.yml'; - $config = $this->initializeConfig($configFile); - - $converter = $this->initializeConverter($config); - $converter->setFamily(LengthFamilyInterface::FAMILY); - $converter->convert(LengthFamilyInterface::METER, LengthFamilyInterface::CENTIMETER, 100); - } - - /** - * Test related exception in standard to final method - * @expectedException Akeneo\Bundle\MeasureBundle\Exception\UnknownOperatorException - */ - public function testUnknownOperatorExceptionStandardToFinal() - { - $configFile = dirname(__FILE__) .'/../Files/measure-test.yml'; - $config = $this->initializeConfig($configFile); - - $converter = $this->initializeConverter($config); - $converter->setFamily(LengthFamilyInterface::FAMILY); - $converter->convert(LengthFamilyInterface::CENTIMETER, LengthFamilyInterface::METER, 100); - } -} diff --git a/Tests/Unit/DependencyInjection/ConfigurationTest.php b/Tests/Unit/DependencyInjection/ConfigurationTest.php deleted file mode 100644 index 835ae35..0000000 --- a/Tests/Unit/DependencyInjection/ConfigurationTest.php +++ /dev/null @@ -1,22 +0,0 @@ -assertTrue($configuration->getConfigTreeBuilder() instanceof TreeBuilder); - } -} diff --git a/Tests/Unit/Files/measure-test.yml b/Tests/Unit/Files/measure-test.yml deleted file mode 100644 index 5fc4bde..0000000 --- a/Tests/Unit/Files/measure-test.yml +++ /dev/null @@ -1,10 +0,0 @@ -measures_config: - Length: - standard: METER - units: - CENTIMETER: - convert: [{'div': 0.01}] - format: cm - METER: - convert: [{'test': 1}] - format: m diff --git a/Tests/Unit/Manager/MeasureManagerTest.php b/Tests/Unit/Manager/MeasureManagerTest.php deleted file mode 100644 index 170b4ce..0000000 --- a/Tests/Unit/Manager/MeasureManagerTest.php +++ /dev/null @@ -1,68 +0,0 @@ -measureManager = new MeasureManager; - $this->measureManager->setMeasureConfig( - array( - 'WEIGHT' => array( - 'standard' => 'KILOGRAM', - 'units' => array( - 'KILOGRAM' => array('symbol' => 'kg'), - 'GRAM' => array('symbol' => 'g') - ) - ) - ) - ); - } - - public function testGetUnitForFamily() - { - $this->assertEquals( - array( - 'KILOGRAM' => 'kg', - 'GRAM' => 'g', - ), - $this->measureManager->getUnitSymbolsForFamily('WEIGHT') - ); - } - - public function testInvalidFamilyWhenGettingUnitForFamily() - { - try { - $this->measureManager->getUnitSymbolsForFamily('LENGTH'); - } catch (\InvalidArgumentException $e) { - $this->assertEquals('Undefined measure family "LENGTH"', $e->getMessage()); - - return; - } - - $this->fail('An InvalidArgumentException has not been raised.'); - } - - public function testGetStandardUnitForFamily() - { - $this->assertEquals( - 'KILOGRAM', - $this->measureManager->getStandardUnitForFamily('WEIGHT') - ); - } - - public function testUnitExistsInFamily() - { - $this->assertEquals( - true, - $this->measureManager->unitExistsInFamily('kg', 'WEIGHT') - ); - $this->assertEquals( - false, - $this->measureManager->unitExistsInFamily('KG', 'WEIGHT') - ); - } -} diff --git a/Tests/bootstrap.php b/Tests/bootstrap.php deleted file mode 100644 index 51bede3..0000000 --- a/Tests/bootstrap.php +++ /dev/null @@ -1,2 +0,0 @@ -=5.3.3", "symfony/symfony": ">=2.1.0" }, + "require-dev": { + "phpspec/phpspec": "2.5.*" + }, + "config": { + "bin-dir": "bin" + }, "autoload": { "psr-0": { "Akeneo\\Bundle\\MeasureBundle": "" } }, diff --git a/phpunit.xml.dist b/phpunit.xml.dist deleted file mode 100644 index 731ed33..0000000 --- a/phpunit.xml.dist +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - - ./Tests - - - - - - . - - ./vendor - ./Tests - - - - - \ No newline at end of file diff --git a/spec/Akeneo/Bundle/MeasureBundle/Convert/MeasureConverterSpec.php b/spec/Akeneo/Bundle/MeasureBundle/Convert/MeasureConverterSpec.php new file mode 100644 index 0000000..48046a2 --- /dev/null +++ b/spec/Akeneo/Bundle/MeasureBundle/Convert/MeasureConverterSpec.php @@ -0,0 +1,86 @@ +beConstructedWith($config); + } + + function it_allows_to_define_the_family() + { + $this->setFamily('Length')->shouldReturnAnInstanceOf('Akeneo\Bundle\MeasureBundle\Convert\MeasureConverter'); + } + + function it_throws_an_exception_if_an_unknown_family_is_set() + { + $this + ->shouldThrow( + new UnknownFamilyMeasureException() + ) + ->during('setFamily', array('foo')); + } + + function it_converts_a_value_from_a_base_unit_to_a_final_unit() + { + $this->setFamily('Weight'); + $this->convert( + WeightFamilyInterface::KILOGRAM, + WeightFamilyInterface::MILLIGRAM, + 1 + )->shouldReturn((double) 1000000); + } + + function it_converts_a_value_to_a_standard_unit() + { + $this->setFamily('Weight'); + $this->convertBaseToStandard( + WeightFamilyInterface::MILLIGRAM, + 1000 + )->shouldReturn((double) 1); + } + + function it_converts_a_standard_value_to_a_final_unit() + { + $this->setFamily('Weight'); + $this->convertStandardToResult( + WeightFamilyInterface::KILOGRAM, + 10 + )->shouldReturn((double) 0.01); + } + + function it_throws_an_exception_if_the_unit_measure_does_not_exist() + { + $this->setFamily('Weight'); + $this + ->shouldThrow( + new UnknownMeasureException( + 'Could not find metric unit "foo" in family "Weight"' + ) + ) + ->during('convertBaseToStandard', array('foo', Argument::any())); + + $this + ->shouldThrow( + new UnknownMeasureException( + 'Could not find metric unit "foo" in family "Weight"' + ) + ) + ->during('convertStandardToResult', array('foo', Argument::any())); + } +} diff --git a/spec/Akeneo/Bundle/MeasureBundle/DependencyInjection/ConfigurationSpec.php b/spec/Akeneo/Bundle/MeasureBundle/DependencyInjection/ConfigurationSpec.php new file mode 100644 index 0000000..e5f3997 --- /dev/null +++ b/spec/Akeneo/Bundle/MeasureBundle/DependencyInjection/ConfigurationSpec.php @@ -0,0 +1,13 @@ +getConfigTreeBuilder()->shouldBeAnInstanceOf('Symfony\Component\Config\Definition\Builder\TreeBuilder'); + } +} diff --git a/spec/Akeneo/Bundle/MeasureBundle/Manager/MeasureManagerSpec.php b/spec/Akeneo/Bundle/MeasureBundle/Manager/MeasureManagerSpec.php new file mode 100644 index 0000000..77d2182 --- /dev/null +++ b/spec/Akeneo/Bundle/MeasureBundle/Manager/MeasureManagerSpec.php @@ -0,0 +1,57 @@ + + * @copyright 2014 Akeneo SAS (http://www.akeneo.com) + * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) + */ +class MeasureManagerSpec extends ObjectBehavior +{ + function let() + { + $filename = realpath(dirname(__FILE__) .'/../Resources/config/measure-test.yml'); + if (!file_exists($filename)) { + throw new \Exception(sprintf('Config file "%s" does not exist', $filename)); + } + + $config = Yaml::parse(file_get_contents($filename)); + + $this->setMeasureConfig($config['measures_config']); + } + + function it_throws_an_exception_when_try_to_get_symbols_of_unknown_family() + { + $this + ->shouldThrow( + new \InvalidArgumentException('Undefined measure family "foo"') + ) + ->during('getUnitSymbolsForFamily', array('foo')); + } + + function it_returns_unit_symbols_list_from_a_family() + { + $this + ->getUnitSymbolsForFamily(WeightFamilyInterface::FAMILY) + ->shouldReturn( + array( + 'MILLIGRAM' => 'mg', + 'GRAM' => 'g', + 'KILOGRAM' => 'kg' + ) + ); + } + + function it_returns_standard_unit_for_a_family() + { + $this + ->getStandardUnitForFamily(WeightFamilyInterface::FAMILY) + ->shouldReturn(WeightFamilyInterface::GRAM); + } +} diff --git a/spec/Akeneo/Bundle/MeasureBundle/Resources/config/measure-test.yml b/spec/Akeneo/Bundle/MeasureBundle/Resources/config/measure-test.yml new file mode 100644 index 0000000..ff09d80 --- /dev/null +++ b/spec/Akeneo/Bundle/MeasureBundle/Resources/config/measure-test.yml @@ -0,0 +1,22 @@ +measures_config: + Length: + standard: METER + units: + CENTIMETER: + convert: [{'div': 0.01}] + format: cm + METER: + convert: [{'test': 1}] + format: m + Weight: + standard: GRAM + units: + MILLIGRAM: + convert: [{'mul': 0.001}] + symbol: mg + GRAM: + convert: [{'mul': 1}] + symbol: g + KILOGRAM: + convert: [{'mul': 1000}] + symbol: kg