|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BenTools\Currency\Tests\Provider; |
|
4
|
|
|
|
|
5
|
|
|
use BenTools\Currency\Model\Currency; |
|
6
|
|
|
use BenTools\Currency\Model\CurrencyInterface; |
|
7
|
|
|
use BenTools\Currency\Model\ExchangeRateInterface; |
|
8
|
|
|
use BenTools\Currency\Provider\DoctrineORMProvider; |
|
9
|
|
|
use BenTools\DoctrineStatic\ManagerRegistry; |
|
10
|
|
|
use BenTools\DoctrineStatic\ObjectManager; |
|
11
|
|
|
use BenTools\DoctrineStatic\ObjectRepository; |
|
12
|
|
|
use PHPUnit\Framework\TestCase; |
|
13
|
|
|
|
|
14
|
|
|
class DoctrineORMProviderTest extends TestCase |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var ExchangeRateInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $prototype; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var ManagerRegistry |
|
24
|
|
|
*/ |
|
25
|
|
|
private $doctrine; |
|
26
|
|
|
|
|
27
|
|
|
public function setUp() |
|
28
|
|
|
{ |
|
29
|
|
|
$prototype = $this->prototype(); |
|
30
|
|
|
$this->doctrine = new ManagerRegistry([ |
|
31
|
|
|
'default' => new ObjectManager([ |
|
32
|
|
|
new ObjectRepository(get_class($prototype)), |
|
33
|
|
|
]) |
|
34
|
|
|
]); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testGetExchangeRate() |
|
38
|
|
|
{ |
|
39
|
|
|
$className = get_class($this->prototype); |
|
40
|
|
|
$yesterday = new \DateTime('yesterday'); |
|
41
|
|
|
$provider = new DoctrineORMProvider($this->doctrine, $className); |
|
42
|
|
|
$exchangeRate = $this->prototype('USD', 'EUR', 1.12, $yesterday); |
|
43
|
|
|
$em = $this->doctrine->getManager(); |
|
44
|
|
|
$em->persist($exchangeRate); |
|
45
|
|
|
$em->flush(); |
|
46
|
|
|
|
|
47
|
|
|
$this->assertSame($exchangeRate, $provider->getExchangeRate(new Currency('USD'), new Currency('EUR'), $yesterday)); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param null $sourceCurrencyCode |
|
52
|
|
|
* @param null $targetCurrencyCode |
|
53
|
|
|
* @param float|null $ratio |
|
54
|
|
|
* @param \DateTimeInterface|null $day |
|
55
|
|
|
* @return ExchangeRateInterface |
|
56
|
|
|
*/ |
|
57
|
|
|
private function prototype($sourceCurrencyCode = null, $targetCurrencyCode = null, float $ratio = null, \DateTimeInterface $day = null): ExchangeRateInterface |
|
58
|
|
|
{ |
|
59
|
|
|
if (null === $this->prototype) { |
|
60
|
|
|
$this->prototype = new class($sourceCurrencyCode, $targetCurrencyCode, $ratio) implements ExchangeRateInterface |
|
61
|
|
|
{ |
|
62
|
|
|
|
|
63
|
|
|
public $id, $sourceCurrencyCode, $targetCurrencyCode, $ratio, $day; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* constructor. |
|
67
|
|
|
* @param CurrencyInterface $sourceCurrency |
|
|
|
|
|
|
68
|
|
|
* @param CurrencyInterface $targetCurrency |
|
|
|
|
|
|
69
|
|
|
* @param float $ratio |
|
70
|
|
|
* @param \DateTimeInterface|null $day |
|
71
|
|
|
*/ |
|
72
|
|
|
public function __construct($sourceCurrencyCode = null, $targetCurrencyCode = null, float $ratio = null, \DateTimeInterface $day = null) |
|
73
|
|
|
{ |
|
74
|
|
|
if (null !== $sourceCurrencyCode && null !== $targetCurrencyCode) { |
|
75
|
|
|
$this->id = sprintf('%s%s', $sourceCurrencyCode, $targetCurrencyCode); |
|
76
|
|
|
} |
|
77
|
|
|
$this->sourceCurrencyCode = $sourceCurrencyCode; |
|
78
|
|
|
$this->targetCurrencyCode = $targetCurrencyCode; |
|
79
|
|
|
$this->ratio = $ratio; |
|
80
|
|
|
$this->day = $day; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function getRatio(): float |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->ratio; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function getSourceCurrency(): CurrencyInterface |
|
89
|
|
|
{ |
|
90
|
|
|
return new Currency($this->sourceCurrencyCode); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function getTargetCurrency(): CurrencyInterface |
|
94
|
|
|
{ |
|
95
|
|
|
return new Currency($this->targetCurrencyCode); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function new($sourceCurrencyCode = null, $targetCurrencyCode = null, float $ratio = null, \DateTimeInterface $day = null): self |
|
99
|
|
|
{ |
|
100
|
|
|
return new self($sourceCurrencyCode, $targetCurrencyCode, $ratio, $day); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
}; |
|
104
|
|
|
return $this->prototype; |
|
105
|
|
|
} |
|
106
|
|
|
return $this->prototype->new($sourceCurrencyCode, $targetCurrencyCode, $ratio, $day); |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$irelandis not defined by the methodfinale(...).The most likely cause is that the parameter was changed, but the annotation was not.