Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 32b91da

Browse files
Advance PHPUnit testing with code coverage
0 parents commit 32b91da

File tree

7 files changed

+296
-0
lines changed

7 files changed

+296
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"# PHPUnit-Advance-Testing"

‎composer.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"require-dev": {
3+
"phpunit/phpunit": "^5.5"
4+
},
5+
"autoload": {
6+
"psr-4": {
7+
"TDD\\": "src/"
8+
}
9+
},
10+
"autoload-dev": {
11+
"psr-4": {
12+
"TDD\\Test\\": "tests/"
13+
}
14+
}
15+
}

‎phpunit.xml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
colors="true"
4+
processIsolation="false"
5+
stopOnFailure="false"
6+
syntaxCheck="false"
7+
>
8+
<php>
9+
<ini name="memory_limit" value="-1"/>
10+
<ini name="apc.enable_cli" value="1"/>
11+
</php>
12+
13+
<!-- Add any additional test suites you want to run here -->
14+
<testsuites>
15+
<testsuite name="app">
16+
<directory>./tests</directory>
17+
</testsuite>
18+
<testsuite name="receipt">
19+
<directory>./tests</directory>
20+
<exclude>./tests/ReceiptItems.php</exclude>
21+
</testsuite>
22+
</testsuites>
23+
24+
<filter>
25+
<!--
26+
<blacklist>
27+
<directory suffix=".php">/path/to/files</directory>
28+
<file>/path/to/file</file>
29+
<exclude>
30+
<directory suffix=".php">/path/to/files</directory>
31+
<file>/path/to/file</file>
32+
</exclude>
33+
</blacklist>
34+
-->
35+
<whitelist processUncoveredFilesFromWhitelist="true">
36+
<directory suffix=".php">./src</directory>
37+
<!--
38+
<file>/path/to/file</file>
39+
<exclude>
40+
<directory suffix=".php">/path/to/files</directory>
41+
<file>/path/to/file</file>
42+
</exclude>
43+
-->
44+
</whitelist>
45+
</filter>
46+
47+
<logging>
48+
<log
49+
type="coverage-html"
50+
target="./tmp/coverage/html/"
51+
charset="UTF-8"
52+
highlight="true"
53+
lowUpperBound="60"
54+
highLowerBound="90"
55+
/>
56+
<log
57+
type="coverage-clover"
58+
target="./tmp/coverage/clover.xml"
59+
/>
60+
</logging>
61+
62+
</phpunit>

‎src/Formatter.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace TDD;
4+
5+
6+
class Formatter{
7+
8+
public function currencyAmount($input)
9+
{
10+
return round($input, 2);
11+
}
12+
}

‎src/Receipt.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace TDD;
4+
use BadMethodCallException;
5+
6+
class Receipt {
7+
8+
private $formatter;
9+
public function __construct($formatter)
10+
{
11+
$this->formatter = $formatter;
12+
}
13+
14+
public function subTotal(array $items = [], $coupon)
15+
{
16+
if($coupon > 1.00){
17+
throw new BadMethodCallException('Coupon must be less than or equal to 1.00');
18+
}
19+
$sum = array_sum($items);
20+
if(!is_null($coupon))
21+
{
22+
return $sum - ($sum*$coupon);
23+
}
24+
return $sum;
25+
}
26+
27+
public function tax($amount)
28+
{
29+
return $this->formatter->currencyAmount($amount * $this->tax);
30+
}
31+
32+
public function postTaxTotal($items, $coupon)
33+
{
34+
$subtotal = $this->subTotal($items, $coupon);
35+
return $subtotal + $this->tax($subtotal);
36+
}
37+
38+
39+
}

‎tests/FormatterTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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\Formatter;
9+
10+
11+
class FormatterTest extends TestCase{
12+
13+
private $formatter;
14+
public function setUp()
15+
{
16+
$this->formatter = new Formatter();
17+
}
18+
19+
public function tearDown()
20+
{
21+
unset($this->formatter);
22+
}
23+
/**
24+
* @dataProvider provideCurrencyAmount
25+
* @param $input
26+
* @param $expected
27+
* @param $msg
28+
*/
29+
public function testCurrencyAmount($input, $expected, $msg)
30+
{
31+
$this->assertSame(
32+
$expected,
33+
$this->formatter->currencyAmount($input),
34+
$msg
35+
);
36+
}
37+
38+
public function provideCurrencyAmount()
39+
{
40+
return [
41+
[1, 1.00, '1 should be transformed into 1.00'],
42+
[1.1, 1.10, '1 should be transformed into 1.10'],
43+
[1.11, 1.11, '1 should be transformed into 1.11'],
44+
[1.111, 1.11, '1 should be transformed into 1.11'],
45+
];
46+
}
47+
}

‎tests/ReceiptTest.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /