File tree Expand file tree Collapse file tree 5 files changed +44
-21
lines changed Expand file tree Collapse file tree 5 files changed +44
-21
lines changed Original file line number Diff line number Diff line change @@ -10,15 +10,13 @@ final class OpenFileCommand implements Command
1010{
1111 private FileSystem $ fileSystem ;
1212
13- public function __construct (
14- FileSystem $ fileSystem ,
15- private string $ fileName
16- ) {
13+ public function __construct (FileSystem $ fileSystem )
14+ {
1715 $ this ->fileSystem = $ fileSystem ;
1816 }
1917
2018 public function execute (): void
2119 {
22- $ this ->fileSystem ->openFile ($ this -> fileName );
20+ $ this ->fileSystem ->openFile ();
2321 }
2422}
Original file line number Diff line number Diff line change 66
77interface FileSystem
88{
9- public function openFile (string $ fileName ): void ;
9+ public function openFile (): void ;
1010 public function writeFile (string $ content ): void ;
1111 public function closeFile (): void ;
1212}
Original file line number Diff line number Diff line change 66
77final class UnixFileSystem implements FileSystem
88{
9- private string $ fileName ;
9+ private string $ fileName = ' unix_file ' ;
1010
11- public function openFile (string $ fileName ): void
11+ public function openFile (): void
1212 {
13- $ this ->fileName = $ fileName ;
14-
15- printf ('Unix file %s is open ' . PHP_EOL , $ fileName );
13+ printf ('Unix file %s is open\n ' , $ this ->fileName );
1614 }
1715
1816 public function writeFile (string $ content ): void
1917 {
20- printf ('Unix file %s is writing ' . PHP_EOL , $ this ->fileName );
21- printf ('Content: %s ' . PHP_EOL , $ content );
18+ printf ('Unix file %s is writing\n ' , $ this ->fileName );
19+ printf ('Content: %s\n ' , $ content );
2220 }
2321
2422 public function closeFile (): void
2523 {
26- printf ('Unix file %s is closed ' . PHP_EOL , $ this ->fileName );
24+ printf ('Unix file %s is closed\n ' , $ this ->fileName );
2725 }
2826}
Original file line number Diff line number Diff line change 66
77final class WindowsFileSystem implements FileSystem
88{
9- private string $ fileName ;
9+ private string $ fileName = ' windows_file ' ;
1010
11- public function openFile (string $ fileName ): void
11+ public function openFile (): void
1212 {
13- $ this ->fileName = $ fileName ;
14-
15- printf ('Windows file %s is open ' . PHP_EOL , $ fileName );
13+ printf ('Windows file %s is open\n ' , $ this ->fileName );
1614 }
1715
1816 public function writeFile (string $ content ): void
1917 {
20- printf ('Windows file %s is writing ' . PHP_EOL , $ this ->fileName );
18+ printf ('Windows file %s is writing\n ' , $ this ->fileName );
2119 printf ('Content: %s ' . PHP_EOL , $ content );
2220 }
2321
2422 public function closeFile (): void
2523 {
26- printf ('Windows file %s is closed ' . PHP_EOL , $ this ->fileName );
24+ printf ('Windows file %s is closed\n ' , $ this ->fileName );
2725 }
2826}
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace Growthdev \DesignPatterns \Tests \Behavioral \Command ;
6+
7+ use Growthdev \DesignPatterns \Behavioral \Command \FileInvoker ;
8+ use Growthdev \DesignPatterns \Behavioral \Command \OpenFileCommand ;
9+ use Growthdev \DesignPatterns \Behavioral \Command \Receiver \FileSystem ;
10+ use Growthdev \DesignPatterns \Behavioral \Command \Receiver \UnixFileSystem ;
11+ use Growthdev \DesignPatterns \Behavioral \Command \Receiver \WindowsFileSystem ;
12+ use PHPUnit \Framework \TestCase ;
13+
14+ final class CommandTest extends TestCase
15+ {
16+ private static function getFileSystem (): FileSystem
17+ {
18+ $ os = strtoupper (substr (PHP_OS , 0 , 3 )) === 'LIN ' ;
19+
20+ return $ os ? new UnixFileSystem () : new WindowsFileSystem ();
21+ }
22+
23+ public function testCanInvokeFile (): void
24+ {
25+ $ this ->getFileSystem ();
26+
27+
28+ }
29+ }
You can’t perform that action at this time.
0 commit comments