1
exercism fetch php book-store

book-store_test.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php

include_once 'book-store.php';

/**
 * Calculate lowest price for shopping basket only
 * containing books from a single series. There is no
 * discount advantage for having more than one copy of
 * any single book in a grouping.
 */

class BookStoreTest extends PHPUnit\Framework\TestCase
{

    /**
     * A basket containing only a single book.
     * Target grouping: [[1]]
     */
    public function testSingleBook()
    {
        $basket = [1];
        $this->assertEquals(8.0, total($basket));
    }

    /**
     * A basket containing only two of the same book.
     * Target grouping: [[2], [2]]
     */
    public function testTwoSame()
    {
        $basket = [2, 2];
        $this->assertEquals(16.0, total($basket));
    }

    /**
     * No charge to carry around an empty basket.
     * Target grouping: []
     */
    public function testEmpty()
    {
        $basket = [];
        $this->assertEquals(0.0, total($basket));
    }

    /**
     * A basket containing only two different books.
     * Target grouping: [[1, 2]]
     */
    public function testTwoDifferent()
    {
        $basket = [1, 2];
        $this->assertEquals(15.2, total($basket));
    }

    /**
     * A basket of three different books.
     * Target grouping: [[1, 2, 3]]
     */
    public function testThreeDifferent()
    {
        $basket = [1, 2, 3];
        $this->assertEquals(21.60, total($basket));
    }

    /**
     * A basket of four different books.
     * Target grouping: [[1, 2, 3, 4]]
     */
    public function testFourDifferent()
    {
        $basket = [1, 2, 3, 4];
        $this->assertEquals(25.60, total($basket));
    }

    /**
     * A basket of five different books.
     * Target grouping: [[1, 2, 3, 4, 5]]
     */
    public function testFiveDifferent()
    {
        $basket = [1, 2, 3, 4, 5];
        $this->assertEquals(30.00, total($basket));
    }

    /**
     * A basket containing eight books consisting of a
     * pair each of the first three books plus one copy
     * each of the last two books. Please pay careful
     * attention to this particular target grouping, it
     * is not intuitive, but does grant the largest
     * discount.
     * Target grouping: [[1, 2, 3, 4], [1, 2, 3, 5]]
     */
    public function testEight()
    {
        $basket = [1, 1, 2, 2, 3, 3, 4, 5];
        $this->assertEquals(51.20, total($basket));
    }

    /**
     * A basket containing nine books consisting of a
     * pair each of the first four books plus one of
     * the last book.
     * Target grouping: [[1, 2, 3, 4, 5], [1, 2, 3, 4]],
     */
    public function testFourPairsPlusOne()
    {
        $basket = [1, 1, 2, 2, 3, 3, 4, 4, 5];
        $this->assertEquals(55.60, total($basket));
    }

    /**
     * A basket containing ten books consisting of two
     * copies of each book in the series.
     * Target grouping: [[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]
     */
    public function testFivePairs()
    {
        $basket = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5];
        $this->assertEquals(60.00, total($basket));
    }

    /**
     * A basket containing eleven books consisting
     * of three copies of the first book plus two each
     * of the remaining four books in the series.
     * Target grouping: [[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1]]
     */
    public function testFivePairsPlusOne()
    {
        $basket = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1];
        $this->assertEquals(68.00, total($basket));
    }

    /**
     * A basket containing twelve books consisting of
     * three copies of the first two books, plus two
     * each of the remaining three books in the series.
     * Target grouping: [[1, 2, 3, 4, 5], [1, 2, 3, 4, 5], [1, 2]]
     */
    public function testTwelve()
    {
        $basket = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 2];
        $this->assertEquals(75.20, total($basket));
    }
}