| 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\ExchangeRate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use BenTools\Currency\Model\ExchangeRateInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use BenTools\Currency\Model\ExchangeRateNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use BenTools\Currency\Provider\ChainedExchangeRateProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use BenTools\Currency\Provider\ExchangeRateProviderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use BenTools\Currency\Tests\ProviderMockTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use DateTimeInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class ChainedExchangeRateProviderTest extends TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     use ProviderMockTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public function testGetFirstExchangeRate() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $provider = new ChainedExchangeRateProvider([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             $this->generateFakeProvider(1.2), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             $this->generateFakeProvider(1.3), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $this->assertEquals(1.2, $provider->getExchangeRate(new Currency('EUR'), new Currency('USD'))->getRatio()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public function testFallback() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $GBPtoEURProvider = new class implements ExchangeRateProviderInterface | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 33 |  |  |         { | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |             public function getExchangeRate(CurrencyInterface $sourceCurrency, CurrencyInterface $targetCurrency, DateTimeInterface $date = null): ExchangeRateInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |             { | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |                 if ('GBP' === $sourceCurrency->getCode() && 'EUR' === $targetCurrency->getCode()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |                     return new ExchangeRate($sourceCurrency, $targetCurrency, 1.15); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |                 throw new ExchangeRateNotFoundException($sourceCurrency, $targetCurrency); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $USDtoEURProvider = new class implements ExchangeRateProviderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             public function getExchangeRate(CurrencyInterface $sourceCurrency, CurrencyInterface $targetCurrency, DateTimeInterface $date = null): ExchangeRateInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 if ('USD' === $sourceCurrency->getCode() && 'EUR' === $targetCurrency->getCode()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                     return new ExchangeRate($sourceCurrency, $targetCurrency, 0.81); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 throw new ExchangeRateNotFoundException($sourceCurrency, $targetCurrency); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $provider = new ChainedExchangeRateProvider([$GBPtoEURProvider, $USDtoEURProvider]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $this->assertEquals(0.81, $provider->getExchangeRate(new Currency('USD'), new Currency('EUR'))->getRatio()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 62 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |  |