Skip to content

Commit b556436

Browse files
author
Dominik Liebler
committed
PHP7 Facade
1 parent e6c67c5 commit b556436

File tree

7 files changed

+698
-413
lines changed

7 files changed

+698
-413
lines changed

Structural/Facade/BiosInterface.php

-17
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,13 @@
22

33
namespace DesignPatterns\Structural\Facade;
44

5-
/**
6-
* Interface BiosInterface.
7-
*/
85
interface BiosInterface
96
{
10-
/**
11-
* Execute the BIOS.
12-
*/
137
public function execute();
148

15-
/**
16-
* Wait for halt.
17-
*/
189
public function waitForKeyPress();
1910

20-
/**
21-
* Launches the OS.
22-
*
23-
* @param OsInterface $os
24-
*/
2511
public function launch(OsInterface $os);
2612

27-
/**
28-
* Power down BIOS.
29-
*/
3013
public function powerDown();
3114
}

Structural/Facade/Facade.php

+2-11
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,14 @@ class Facade
77
/**
88
* @var OsInterface
99
*/
10-
protected $os;
10+
private $os;
1111

1212
/**
1313
* @var BiosInterface
1414
*/
15-
protected $bios;
15+
private $bios;
1616

1717
/**
18-
* This is the perfect time to use a dependency injection container
19-
* to create an instance of this class.
20-
*
2118
* @param BiosInterface $bios
2219
* @param OsInterface $os
2320
*/
@@ -27,19 +24,13 @@ public function __construct(BiosInterface $bios, OsInterface $os)
2724
$this->os = $os;
2825
}
2926

30-
/**
31-
* Turn on the system.
32-
*/
3327
public function turnOn()
3428
{
3529
$this->bios->execute();
3630
$this->bios->waitForKeyPress();
3731
$this->bios->launch($this->os);
3832
}
3933

40-
/**
41-
* Turn off the system.
42-
*/
4334
public function turnOff()
4435
{
4536
$this->os->halt();

Structural/Facade/OsInterface.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@
22

33
namespace DesignPatterns\Structural\Facade;
44

5-
/**
6-
* Interface OsInterface.
7-
*/
85
interface OsInterface
96
{
10-
/**
11-
* Halt the OS.
12-
*/
137
public function halt();
8+
9+
public function getName(): string;
1410
}

Structural/Facade/Tests/FacadeTest.php

+18-31
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,34 @@
22

33
namespace DesignPatterns\Structural\Facade\Tests;
44

5-
use DesignPatterns\Structural\Facade\Facade as Computer;
5+
use DesignPatterns\Structural\Facade\Facade;
66
use DesignPatterns\Structural\Facade\OsInterface;
77

8-
/**
9-
* FacadeTest shows example of facades.
10-
*/
118
class FacadeTest extends \PHPUnit_Framework_TestCase
129
{
13-
public function getComputer()
10+
public function testComputerOn()
1411
{
12+
/** @var OsInterface|\PHPUnit_Framework_MockObject_MockObject $os */
13+
$os = $this->createMock('DesignPatterns\Structural\Facade\OsInterface');
14+
15+
$os->method('getName')
16+
->will($this->returnValue('Linux'));
17+
1518
$bios = $this->getMockBuilder('DesignPatterns\Structural\Facade\BiosInterface')
16-
->setMethods(array('launch', 'execute', 'waitForKeyPress'))
17-
->disableAutoload()
18-
->getMock();
19-
$operatingSys = $this->getMockBuilder('DesignPatterns\Structural\Facade\OsInterface')
20-
->setMethods(array('getName'))
21-
->disableAutoload()
22-
->getMock();
19+
->setMethods(['launch', 'execute', 'waitForKeyPress'])
20+
->disableAutoload()
21+
->getMock();
22+
2323
$bios->expects($this->once())
24-
->method('launch')
25-
->with($operatingSys);
26-
$operatingSys
27-
->expects($this->once())
28-
->method('getName')
29-
->will($this->returnValue('Linux'));
24+
->method('launch')
25+
->with($os);
3026

31-
$facade = new Computer($bios, $operatingSys);
27+
$facade = new Facade($bios, $os);
3228

33-
return array(array($facade, $operatingSys));
34-
}
35-
36-
/**
37-
* @param Computer $facade
38-
* @param OsInterface $os
39-
* @dataProvider getComputer
40-
*/
41-
public function testComputerOn(Computer $facade, OsInterface $os)
42-
{
43-
// interface is simpler :
29+
// the facade interface is simple
4430
$facade->turnOn();
45-
// but I can access to lower component
31+
32+
// but you can also access the underlying components
4633
$this->assertEquals('Linux', $os->getName());
4734
}
4835
}

Structural/Facade/uml/Facade.uml

+23-24
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<Diagram>
3-
<ID>PHP</ID>
4-
<OriginalElement>\DesignPatterns\Structural\Facade\BiosInterface</OriginalElement>
5-
<nodes>
6-
<node x="0.0" y="174.0">\DesignPatterns\Structural\Facade\BiosInterface</node>
7-
<node x="208.0" y="174.0">\DesignPatterns\Structural\Facade\OsInterface</node>
8-
<node x="0.0" y="0.0">\DesignPatterns\Structural\Facade\Facade</node>
9-
</nodes>
10-
<notes />
11-
<edges />
12-
<settings layout="Hierarchic Group" zoom="1.0" x="149.0" y="140.5" />
13-
<SelectedNodes>
14-
<node>\DesignPatterns\Structural\Facade\BiosInterface</node>
15-
</SelectedNodes>
16-
<Categories>
17-
<Category>Fields</Category>
18-
<Category>Constants</Category>
19-
<Category>Constructors</Category>
20-
<Category>Methods</Category>
21-
</Categories>
22-
<VISIBILITY>private</VISIBILITY>
23-
</Diagram>
24-
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Diagram>
3+
<ID>PHP</ID>
4+
<OriginalElement>\DesignPatterns\Structural\Facade\BiosInterface</OriginalElement>
5+
<nodes>
6+
<node x="0.0" y="163.0">\DesignPatterns\Structural\Facade\BiosInterface</node>
7+
<node x="0.0" y="0.0">\DesignPatterns\Structural\Facade\Facade</node>
8+
<node x="234.0" y="163.0">\DesignPatterns\Structural\Facade\OsInterface</node>
9+
</nodes>
10+
<notes />
11+
<edges />
12+
<settings layout="Hierarchic Group" zoom="1.0" x="179.0" y="140.0" />
13+
<SelectedNodes>
14+
<node>\DesignPatterns\Structural\Facade\BiosInterface</node>
15+
</SelectedNodes>
16+
<Categories>
17+
<Category>Methods</Category>
18+
<Category>Constants</Category>
19+
<Category>Fields</Category>
20+
</Categories>
21+
<VISIBILITY>private</VISIBILITY>
22+
</Diagram>
23+

Structural/Facade/uml/uml.png

29 KB
Loading

0 commit comments

Comments
 (0)