|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace TDD\Test; |
| 4 | + |
| 5 | +require dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'autoload.php'; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use TDD\Receipt; |
| 9 | + |
| 10 | +class ReceiptTest extends TestCase{ |
| 11 | + |
| 12 | + private $receipt; |
| 13 | + private $formatter; |
| 14 | + public function setUp() |
| 15 | + { |
| 16 | + $this->formatter = $this->getMockBuilder('TDD\Formatter') |
| 17 | + ->setMethods(['currencyAmount']) |
| 18 | + ->getMock(); |
| 19 | + |
| 20 | + $this->formatter->expects($this->any()) |
| 21 | + ->method('currencyAmount') |
| 22 | + ->with($this->anything()) |
| 23 | + ->will($this->returnArgument(0)); |
| 24 | + $this->receipt = new Receipt($this->formatter); |
| 25 | + } |
| 26 | + |
| 27 | + public function tearDown() |
| 28 | + { |
| 29 | + unset($this->receipt); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @dataProvider provideSubTotal |
| 34 | + */ |
| 35 | + public function testSubTotal($items, $expected) |
| 36 | + { |
| 37 | + $coupon = null; |
| 38 | + $output = $this->receipt->subTotal($items, $coupon); |
| 39 | + |
| 40 | + $this->assertEquals( |
| 41 | + $expected, |
| 42 | + $output, |
| 43 | + "When summing the total should equals 10 {$expected}" |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * The data provider for testSubTotal |
| 49 | + */ |
| 50 | + public function provideSubTotal() |
| 51 | + { |
| 52 | + return [ |
| 53 | + [[1, 2, 5, 8], 16], |
| 54 | + [[-1, 2, 5, 8], 14], |
| 55 | + [[1, 2, 8], 11], |
| 56 | + ]; |
| 57 | + } |
| 58 | + |
| 59 | + public function testSubTotalAndCoupon() |
| 60 | + { |
| 61 | + $items = [0, 2, 5, 8]; |
| 62 | + $coupon = 0.20; |
| 63 | + $output = $this->receipt->subTotal($items, $coupon); |
| 64 | + |
| 65 | + $this->assertEquals( |
| 66 | + 12, |
| 67 | + $output, |
| 68 | + 'When summing the total should equals 12' |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + public function testSubTotalException() |
| 74 | + { |
| 75 | + $items = [0, 2, 5, 8]; |
| 76 | + $coupon = 1.20; |
| 77 | + $this->expectException('BadMethodCallException'); |
| 78 | + $this->receipt->subTotal($items, $coupon); |
| 79 | + } |
| 80 | + |
| 81 | + public function testPostTaxTotal() |
| 82 | + { |
| 83 | + $items = [1, 2, 5, 8]; |
| 84 | + $tax = 0.20; |
| 85 | + $coupon = null; |
| 86 | + $receipt = $this->getMockBuilder('TDD\Receipt') |
| 87 | + ->setMethods(['tax', 'subTotal']) |
| 88 | + ->setConstructorArgs([$this->formatter]) |
| 89 | + ->getMock(); |
| 90 | + |
| 91 | + $receipt->expects($this->once()) |
| 92 | + ->method('subTotal') |
| 93 | + ->with($items, $coupon) |
| 94 | + ->will($this->returnValue(10.00)); |
| 95 | + |
| 96 | + $receipt->expects($this->once()) |
| 97 | + ->method('tax') |
| 98 | + ->with(10.00) |
| 99 | + ->will($this->returnValue(1.00)); |
| 100 | + |
| 101 | + $result = $receipt->postTaxTotal([1,2,5,8], null); |
| 102 | + $this->assertEquals(11.00, $result); |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + public function testTax() |
| 107 | + { |
| 108 | + $inputAmount = 10.00; |
| 109 | + $this->receipt->tax = 0.10; |
| 110 | + $output = $this->receipt->tax($inputAmount); |
| 111 | + $this->assertEquals( |
| 112 | + 1.00, |
| 113 | + $output, |
| 114 | + 'The Tax calculation should equal 1.00' |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | +} |
0 commit comments