1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LAShowroom\TaxJarBundle\Tests; |
4
|
|
|
|
5
|
|
|
use LAShowroom\TaxJarBundle\Client; |
6
|
|
|
use LAShowroom\TaxJarBundle\Model\Address; |
7
|
|
|
use LAShowroom\TaxJarBundle\Model\LineItem; |
8
|
|
|
use LAShowroom\TaxJarBundle\Model\Order; |
9
|
|
|
use LAShowroom\TaxJarBundle\Model\Response\TaxBreakdown; |
10
|
|
|
use LAShowroom\TaxJarBundle\Model\Response\TaxDetail; |
11
|
|
|
use LAShowroom\TaxJarBundle\Model\Response\TaxResponse; |
12
|
|
|
use LAShowroom\TaxJarBundle\Tests\Model\OrderTest; |
13
|
|
|
use Prophecy\Argument; |
14
|
|
|
use Symfony\Component\Cache\Adapter\ArrayAdapter; |
15
|
|
|
use Symfony\Component\Cache\CacheItem; |
16
|
|
|
|
17
|
|
|
class ClientTest extends \PHPUnit_Framework_TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Client |
21
|
|
|
*/ |
22
|
|
|
private $client; |
23
|
|
|
|
24
|
|
|
protected function setup() |
25
|
|
|
{ |
26
|
|
|
$clientProphesy = $this->prophesize('\TaxJar\Client'); |
27
|
|
|
$clientProphesy |
28
|
|
|
->taxForOrder( |
29
|
|
|
Argument::type('array') |
30
|
|
|
) |
31
|
|
|
->willReturn( |
32
|
|
|
json_decode('{"order_total_amount":16.5,"shipping":1.5,"taxable_amount":15,"amount_to_collect":1.31,"rate":0.0875,"has_nexus":true,"freight_taxable":false,"tax_source":"destination","breakdown":{"taxable_amount":15,"tax_collectable":1.31,"combined_tax_rate":0.0875,"state_taxable_amount":15,"state_tax_rate":0.0625,"state_tax_collectable":0.94,"county_taxable_amount":15,"county_tax_rate":0.01,"county_tax_collectable":0.15,"city_taxable_amount":0,"city_tax_rate":0,"city_tax_collectable":0,"special_district_taxable_amount":15,"special_tax_rate":0.015,"special_district_tax_collectable":0.23,"line_items":[{"id":"1","taxable_amount":15,"tax_collectable":1.31,"combined_tax_rate":0.0875,"state_taxable_amount":15,"state_sales_tax_rate":0.0625,"state_amount":0.94,"county_taxable_amount":15,"county_tax_rate":0.01,"county_amount":0.15,"city_taxable_amount":0,"city_tax_rate":0,"city_amount":0,"special_district_taxable_amount":15,"special_tax_rate":0.015,"special_district_amount":0.23}]}}') |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
$this->client = new Client($clientProphesy->reveal()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testGetRatesForOrder() |
39
|
|
|
{ |
40
|
|
|
$order = OrderTest::getTestOrder(); |
41
|
|
|
|
42
|
|
|
$result = $this->client->getTaxesForOrder($order); |
43
|
|
|
|
44
|
|
|
$this->assertInstanceOf(TaxResponse::class, $result); |
45
|
|
|
$this->assertEquals(16.5, $result->getTotalAmount()); |
46
|
|
|
$this->assertEquals(1.5, $result->getShipping()); |
47
|
|
|
$this->assertEquals(15, $result->getTaxableAmount()); |
48
|
|
|
$this->assertEquals(1.31, $result->getAmountToCollect()); |
49
|
|
|
$this->assertEquals(0.0875, $result->getRate()); |
50
|
|
|
$this->assertEquals(true, $result->hasNexus()); |
51
|
|
|
$this->assertEquals(false, $result->isFreightTaxable()); |
52
|
|
|
$this->assertEquals('destination', $result->getTaxSource()); |
53
|
|
|
|
54
|
|
|
$this->assertInstanceOf(TaxBreakdown::class, $result->getTaxBreakdown()); |
55
|
|
|
$this->verifyTaxBreakDown($result->getTaxBreakdown()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function testCacheCold() |
59
|
|
|
{ |
60
|
|
|
$cache = new ArrayAdapter(); |
61
|
|
|
$this->assertFalse($cache->hasItem(OrderTest::getTestOrder()->getCacheKey())); |
62
|
|
|
|
63
|
|
|
$this->client->setCacheItemPool($cache); |
64
|
|
|
|
65
|
|
|
$result = $this->client->getTaxesForOrder(OrderTest::getTestOrder()); |
66
|
|
|
$this->assertInstanceOf(TaxResponse::class, $result); |
67
|
|
|
|
68
|
|
|
$this->assertTrue($cache->hasItem(OrderTest::getTestOrder()->getCacheKey())); |
69
|
|
|
$this->assertEquals($result, $cache->getItem(OrderTest::getTestOrder()->getCacheKey())->get()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testCacheWarm() |
73
|
|
|
{ |
74
|
|
|
$cache = new ArrayAdapter(); |
75
|
|
|
$this->assertFalse($cache->hasItem(OrderTest::getTestOrder()->getCacheKey())); |
76
|
|
|
$cacheItem = $cache->getItem(OrderTest::getTestOrder()->getCacheKey()); |
77
|
|
|
|
78
|
|
|
$result = $this->client->getTaxesForOrder(OrderTest::getTestOrder()); |
79
|
|
|
|
80
|
|
|
$cacheItem->set($result); |
81
|
|
|
$cache->save($cacheItem); |
82
|
|
|
|
83
|
|
|
$clientProphesy = $this->prophesize('\TaxJar\Client'); |
84
|
|
|
$clientProphesy |
85
|
|
|
->taxForOrder( |
86
|
|
|
Argument::type('array') |
87
|
|
|
)->shouldNotBeCalled(); |
88
|
|
|
|
89
|
|
|
$client = new Client($clientProphesy->reveal()); |
90
|
|
|
$client->setCacheItemPool($cache); |
91
|
|
|
|
92
|
|
|
$resultFromCache = $client->getTaxesForOrder(OrderTest::getTestOrder()); |
93
|
|
|
$this->assertInstanceOf(TaxResponse::class, $resultFromCache); |
94
|
|
|
|
95
|
|
|
$this->assertEquals($result, $resultFromCache); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function verifyTaxBreakDown(TaxBreakdown $taxBreakdown) |
99
|
|
|
{ |
100
|
|
|
$this->assertEquals(new TaxDetail(15, 0.0875, 1.31), $taxBreakdown->getTotalTax()); |
101
|
|
|
$this->assertEquals(new TaxDetail(15, 0.0625, 0.94), $taxBreakdown->getStateTax()); |
102
|
|
|
$this->assertEquals(new TaxDetail(15, 0.01, 0.15), $taxBreakdown->getCountyTax()); |
103
|
|
|
$this->assertEquals(new TaxDetail(0, 0, 0), $taxBreakdown->getCityTax()); |
104
|
|
|
$this->assertEquals(new TaxDetail(15, 0.015, 0.23), $taxBreakdown->getSpecialDistrictTax()); |
105
|
|
|
|
106
|
|
|
$lineItems = $taxBreakdown->getLineItems(); |
107
|
|
|
$this->assertCount(1, $lineItems); |
108
|
|
|
$lineItem = $lineItems[0]; |
109
|
|
|
|
110
|
|
|
$totaxTax = $lineItem->getTotalTax(); |
111
|
|
|
$this->assertEquals(15, $totaxTax->getAmount()); |
112
|
|
|
$this->assertEquals(0.0875, $totaxTax->getTaxRate()); |
113
|
|
|
$this->assertEquals(1.31, $totaxTax->getCollectable()); |
114
|
|
|
|
115
|
|
|
$this->assertEquals(new TaxDetail(15, 0.0625, 0.94), $lineItem->getStateTax()); |
116
|
|
|
$this->assertEquals(new TaxDetail(15, 0.01, 0.15), $lineItem->getCountyTax()); |
117
|
|
|
$this->assertEquals(new TaxDetail(0, 0, 0), $lineItem->getCityTax()); |
118
|
|
|
$this->assertEquals(new TaxDetail(15, 0.015, 0.23), $lineItem->getSpecialDistrictTax()); |
119
|
|
|
$this->assertEquals('1', $lineItem->getId()); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|