1+ <?php
2+
3+ namespace PhpDesignPatterns \Tests \Structural \Flyweight ;
4+
5+ use PhpDesignPatterns \Structural \Flyweight \Smartphone ;
6+
7+ class SmartphoneTest extends \PHPUnit_Framework_TestCase
8+ {
9+ /**
10+ * Instance of Smartphone.
11+ *
12+ * @var PhoneInterface
13+ */
14+ private $ phone ;
15+
16+ /**
17+ * Setup each test case basic variables.
18+ */
19+ public function setUp ()
20+ {
21+ $ this ->phone = new Smartphone ;
22+ }
23+
24+ /**
25+ * Destroy variables after each test case.
26+ */
27+ public function tearDown ()
28+ {
29+ $ this ->phone = null ;
30+ }
31+
32+ /**
33+ * Test get basic phone price.
34+ */
35+ public function testGetBasicPrice ()
36+ {
37+ $ this ->assertEquals (Smartphone::PHONE_PRICE , $ this ->phone ->getBasicPrice ());
38+ }
39+
40+ /**
41+ * Test get accessories price when no accessories.
42+ */
43+ public function testGetAccessoriesPriceWhenNoAccessories ()
44+ {
45+ $ this ->assertEquals (0.0 , $ this ->phone ->getAccessoriesPrice ());
46+ }
47+
48+ /**
49+ * Test get accessories price.
50+ */
51+ public function testGetAccessoriesPrice ()
52+ {
53+ $ mock_accessory = $ this ->getMock ('\\PhpDesignPatterns \\Structural \\Flyweight \\AccessoryFlyweightInterface ' , array ('getPrice ' ));
54+ $ mock_accessory
55+ ->expects ($ this ->exactly (2 ))
56+ ->method ('getPrice ' )
57+ ->will ($ this ->onConsecutiveCalls (20.00 , 10.00 ))
58+ ;
59+ $ this ->phone ->addAccessory ($ mock_accessory );
60+ $ this ->phone ->addAccessory ($ mock_accessory );
61+ $ this ->assertEquals (30.0 , $ this ->phone ->getAccessoriesPrice ());
62+ }
63+
64+ /**
65+ * Test get total phone price.
66+ */
67+ public function testGetTotalPrice ()
68+ {
69+ $ mock_accessory = $ this ->getMock ('\\PhpDesignPatterns \\Structural \\Flyweight \\AccessoryFlyweightInterface ' , array ('getPrice ' ));
70+ $ mock_accessory
71+ ->expects ($ this ->exactly (2 ))
72+ ->method ('getPrice ' )
73+ ->will ($ this ->onConsecutiveCalls (20.00 , 10.00 ))
74+ ;
75+ $ this ->phone ->addAccessory ($ mock_accessory );
76+ $ this ->phone ->addAccessory ($ mock_accessory );
77+ $ this ->assertEquals (Smartphone::PHONE_PRICE +30.0 , $ this ->phone ->getTotalPrice ());
78+ }
79+ }
0 commit comments