1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace spec\Akeneo\Bundle\MeasureBundle\Convert; |
4
|
|
|
|
5
|
|
|
use Akeneo\Bundle\MeasureBundle\Exception\UnknownFamilyMeasureException; |
6
|
|
|
use Akeneo\Bundle\MeasureBundle\Exception\UnknownMeasureException; |
7
|
|
|
use Akeneo\Bundle\MeasureBundle\Family\WeightFamilyInterface; |
8
|
|
|
use PhpSpec\ObjectBehavior; |
9
|
|
|
use Prophecy\Argument; |
10
|
|
|
use Symfony\Component\Yaml\Yaml; |
11
|
|
|
|
12
|
|
|
class MeasureConverterSpec extends ObjectBehavior |
13
|
|
|
{ |
14
|
|
View Code Duplication |
function let() |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
$filename = realpath(dirname(__FILE__) .'/../Resources/config/measure-test.yml'); |
17
|
|
|
if (!file_exists($filename)) { |
18
|
|
|
throw new \Exception(sprintf('Config file "%s" does not exist', $filename)); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
$config = Yaml::parse(file_get_contents($filename)); |
22
|
|
|
$this->beConstructedWith($config); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
function it_allows_to_define_the_family() |
26
|
|
|
{ |
27
|
|
|
$this->setFamily('Length')->shouldReturnAnInstanceOf('Akeneo\Bundle\MeasureBundle\Convert\MeasureConverter'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
function it_throws_an_exception_if_an_unknown_family_is_set() |
31
|
|
|
{ |
32
|
|
|
$this |
33
|
|
|
->shouldThrow( |
34
|
|
|
new UnknownFamilyMeasureException() |
35
|
|
|
) |
36
|
|
|
->during('setFamily', array('foo')); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
function it_converts_a_value_from_a_base_unit_to_a_final_unit() |
40
|
|
|
{ |
41
|
|
|
$this->setFamily('Weight'); |
42
|
|
|
$this->convert( |
43
|
|
|
WeightFamilyInterface::KILOGRAM, |
44
|
|
|
WeightFamilyInterface::MILLIGRAM, |
45
|
|
|
1 |
46
|
|
|
)->shouldReturn((double) 1000000); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
function it_converts_a_value_to_a_standard_unit() |
50
|
|
|
{ |
51
|
|
|
$this->setFamily('Weight'); |
52
|
|
|
$this->convertBaseToStandard( |
53
|
|
|
WeightFamilyInterface::MILLIGRAM, |
54
|
|
|
1000 |
55
|
|
|
)->shouldReturn((double) 1); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
function it_converts_a_standard_value_to_a_final_unit() |
59
|
|
|
{ |
60
|
|
|
$this->setFamily('Weight'); |
61
|
|
|
$this->convertStandardToResult( |
62
|
|
|
WeightFamilyInterface::KILOGRAM, |
63
|
|
|
10 |
64
|
|
|
)->shouldReturn((double) 0.01); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
function it_throws_an_exception_if_the_unit_measure_does_not_exist() |
68
|
|
|
{ |
69
|
|
|
$this->setFamily('Weight'); |
70
|
|
|
$this |
71
|
|
|
->shouldThrow( |
72
|
|
|
new UnknownMeasureException( |
73
|
|
|
'Could not find metric unit "foo" in family "Weight"' |
74
|
|
|
) |
75
|
|
|
) |
76
|
|
|
->during('convertBaseToStandard', array('foo', Argument::any())); |
77
|
|
|
|
78
|
|
|
$this |
79
|
|
|
->shouldThrow( |
80
|
|
|
new UnknownMeasureException( |
81
|
|
|
'Could not find metric unit "foo" in family "Weight"' |
82
|
|
|
) |
83
|
|
|
) |
84
|
|
|
->during('convertStandardToResult', array('foo', Argument::any())); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.