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
|
|
|
public 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
|
|
|
public function it_allows_to_define_the_family() |
26
|
|
|
{ |
27
|
|
|
$this->setFamily('Length')->shouldReturnAnInstanceOf('Akeneo\Bundle\MeasureBundle\Convert\MeasureConverter'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function it_throws_an_exception_if_an_unknown_family_is_set() |
31
|
|
|
{ |
32
|
|
|
$this |
33
|
|
|
->shouldThrow( |
34
|
|
|
new UnknownFamilyMeasureException() |
35
|
|
|
) |
36
|
|
|
->during('setFamily', ['foo']); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public 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
|
|
|
public 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
|
|
|
public 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
|
|
|
public 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', ['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', ['foo', Argument::any()]); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|