Skip to content

Commit 1e45226

Browse files
committed
WIP
1 parent f75e31e commit 1e45226

9 files changed

+107
-34
lines changed

.travis.yml

+4-8
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,8 @@ env:
1010
global:
1111
- GIT_COMMITTED_AT=$(if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then git log -1 --pretty=format:%ct; else git log -1 --skip 1 --pretty=format:%ct; fi)
1212
matrix:
13-
- LARAVEL=5.5.* TESTBENCH=3.5.* PHPUNIT=6.* COMPOSER_FLAGS=""
14-
- LARAVEL=5.5.* TESTBENCH=3.5.* PHPUNIT=6.* COMPOSER_FLAGS="--prefer-lowest"
15-
- LARAVEL=5.8.* TESTBENCH=3.8.* PHPUNIT=7.* COMPOSER_FLAGS=""
16-
- LARAVEL=5.8.* TESTBENCH=3.8.* PHPUNIT=7.* COMPOSER_FLAGS="--prefer-lowest"
17-
- LARAVEL=6.* TESTBENCH=4.* PHPUNIT=8.* COMPOSER_FLAGS=""
18-
- LARAVEL=6.* TESTBENCH=4.* PHPUNIT=8.* COMPOSER_FLAGS="--prefer-lowest"
13+
- COMPOSER_FLAGS="--prefer-lowest"
14+
- COMPOSER_FLAGS=""
1915

2016
matrix:
2117
fast_finish: true
@@ -31,13 +27,13 @@ cache:
3127

3228
before_script:
3329
- travis_retry composer self-update
34-
- travis_retry composer install ${COMPOSER_FLAGS} --no-interaction
30+
- travis_retry composer install --no-interaction
3531
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
3632
- chmod +x ./cc-test-reporter
3733
- ./cc-test-reporter before-build
3834

3935
script:
40-
- vendor/bin/phpunit --coverage-text --coverage-clover build/logs/clover.xml
36+
- vendor/bin/phpunit --exclude-group integration --coverage-text --coverage-clover build/logs/clover.xml
4137

4238
after_script:
4339
- if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT; fi

phpunit.xml.dist

-4
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,4 @@
2626
<log type="coverage-text" target="build/coverage.txt"/>
2727
<log type="coverage-clover" target="build/logs/clover.xml"/>
2828
</logging>
29-
<php>
30-
<env name="DB_CONNECTION" value="sqlite" force="true"/>
31-
<env name="DB_DATABASE" value=":memory:" force="true"/>
32-
</php>
3329
</phpunit>

src/Currencylayer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function live($source, ...$currencies)
5252
*
5353
* @return array|float
5454
*/
55-
public function rateFor($source, $date, ...$currencies)
55+
public function rate($source, $date, ...$currencies)
5656
{
5757
if (! $source instanceof Currency) {
5858
$source = Currency::firstOrCreate(['code' => $source]);

src/LaravelCurrencylayerServiceProvider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function register()
4040
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'currencylayer');
4141

4242
$this->app->singleton('currencylayer', function () {
43-
return new Currencylayer;
43+
return new Currencylayer(new client(config('currencylayer.access_key')));
4444
});
4545
}
4646
}

src/Models/Currency.php

-5
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,4 @@ public function rates(): HasMany
2020
{
2121
return $this->hasMany(Rate::class, 'source_currency_id');
2222
}
23-
24-
public function rate(Currency $target, $date = null)
25-
{
26-
return $this->targetRates()->where('target_currency_id', $target->id)->orderByDesc('rate_for')->first();
27-
}
2823
}

tests/CurrencylayerTest.php

+20-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,29 @@
33
namespace Orkhanahmadov\LaravelCurrencylayer\Tests;
44

55
use Carbon\Carbon;
6+
use OceanApplications\currencylayer\client;
7+
use Orkhanahmadov\LaravelCurrencylayer\Currencylayer;
68
use Orkhanahmadov\LaravelCurrencylayer\Models\Currency;
79
use Orkhanahmadov\LaravelCurrencylayer\Models\Rate;
810

911
class CurrencylayerTest extends TestCase
1012
{
13+
/**
14+
* @var Currencylayer
15+
*/
16+
private $service;
17+
18+
protected function setUp(): void
19+
{
20+
parent::setUp();
21+
22+
$this->app->bind(client::class, function () {
23+
return new FakeClient();
24+
});
25+
26+
$this->service = app(Currencylayer::class);
27+
}
28+
1129
public function testLiveWithSingleTarget()
1230
{
1331
$this->assertSame(0, Currency::count());
@@ -67,7 +85,7 @@ public function testRateForWithSingleTarget()
6785
$this->assertSame(0, Currency::count());
6886
$this->assertSame(0, Rate::count());
6987

70-
$rate = $this->service->rateFor('USD', Carbon::today(), 'AED');
88+
$rate = $this->service->rate('USD', Carbon::today(), 'AED');
7189

7290
$this->assertSame(3.67266, $rate);
7391
}
@@ -77,7 +95,7 @@ public function testRateForWithMultipleTargets()
7795
$this->assertSame(0, Currency::count());
7896
$this->assertSame(0, Rate::count());
7997

80-
$rates = $this->service->rateFor('USD', Carbon::today()->format('Y-m-d'), 'AED', 'AMD');
98+
$rates = $this->service->rate('USD', Carbon::today()->format('Y-m-d'), 'AED', 'AMD');
8199

82100
$this->assertTrue(is_array($rates));
83101
$this->assertSame(3.67266, $rates['AED']);

tests/FacadeTest.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Orkhanahmadov\LaravelCurrencylayer\Tests;
4+
5+
use Orkhanahmadov\LaravelCurrencylayer\Currencylayer;
6+
use Orkhanahmadov\LaravelCurrencylayer\CurrencylayerFacade;
7+
use Orkhanahmadov\LaravelCurrencylayer\Models\Currency;
8+
use Orkhanahmadov\LaravelCurrencylayer\Models\Rate;
9+
10+
class FacadeTest extends TestCase
11+
{
12+
public function testFacadeWithFake()
13+
{
14+
$this->app->singleton('currencylayer', function () {
15+
return new Currencylayer(new FakeClient());
16+
});
17+
18+
$rate = CurrencylayerFacade::live('USD', 'AED');
19+
20+
$this->assertSame(3.673103, $rate);
21+
}
22+
23+
/**
24+
* @group integration
25+
*/
26+
public function testFacadeWithRealClient()
27+
{
28+
CurrencylayerFacade::live('USD', 'AED');
29+
30+
$this->assertSame(2, Currency::count());
31+
$this->assertSame(1, Rate::count());
32+
}
33+
}

tests/IntegrationTest.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Orkhanahmadov\LaravelCurrencylayer\Tests;
4+
5+
use Carbon\Carbon;
6+
use Orkhanahmadov\LaravelCurrencylayer\Currencylayer;
7+
use Orkhanahmadov\LaravelCurrencylayer\Models\Currency;
8+
use Orkhanahmadov\LaravelCurrencylayer\Models\Rate;
9+
10+
/**
11+
* @group integration
12+
*/
13+
class IntegrationTest extends TestCase
14+
{
15+
/**
16+
* @var Currencylayer
17+
*/
18+
private $service;
19+
20+
protected function setUp(): void
21+
{
22+
parent::setUp();
23+
24+
$this->service = app(Currencylayer::class);
25+
}
26+
27+
public function testLive()
28+
{
29+
$this->assertSame(0, Currency::count());
30+
$this->assertSame(0, Rate::count());
31+
32+
$this->service->live('USD', 'EUR');
33+
34+
$this->assertSame(2, Currency::count());
35+
$this->assertSame(1, Rate::count());
36+
}
37+
38+
public function testRate()
39+
{
40+
$this->assertSame(0, Currency::count());
41+
$this->assertSame(0, Rate::count());
42+
43+
$this->service->rate('USD', Carbon::yesterday(), 'EUR');
44+
45+
$this->assertSame(2, Currency::count());
46+
$this->assertSame(1, Rate::count());
47+
}
48+
}

tests/TestCase.php

-13
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,18 @@
22

33
namespace Orkhanahmadov\LaravelCurrencylayer\Tests;
44

5-
use OceanApplications\currencylayer\client;
6-
use Orkhanahmadov\LaravelCurrencylayer\Currencylayer;
75
use Orchestra\Testbench\TestCase as Orchestra;
86
use Orkhanahmadov\LaravelCurrencylayer\LaravelCurrencylayerServiceProvider;
97

108
abstract class TestCase extends Orchestra
119
{
12-
/**
13-
* @var Currencylayer
14-
*/
15-
protected $service;
16-
1710
protected function setUp(): void
1811
{
1912
parent::setUp();
2013

2114
$this->setUpDatabase($this->app);
2215

2316
$this->withFactories(__DIR__.'/../database/factories');
24-
25-
$this->app->bind(client::class, function () {
26-
return new FakeClient();
27-
});
28-
29-
$this->service = app(Currencylayer::class);
3017
}
3118

3219
/**

0 commit comments

Comments
 (0)