-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e24706b
commit d70dd65
Showing
8 changed files
with
98 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
require_once __DIR__ . '/../vendor/autoload.php'; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class AdapterTest extends TestCase | ||
{ | ||
public function test_filesystem_adapter() : void | ||
{ | ||
$client = new AdapterPattern\App\Client(); | ||
|
||
$local_filesystem = new AdapterPattern\App\LocalFilesystem(); | ||
$this->assertEquals($client->readFile($local_filesystem), 'opening local file'); | ||
|
||
// this wont work since dropbox doesnt adhere to the same interface | ||
/* | ||
$dropbox_filesystem = new AdapterPattern\App\Dropbox(); | ||
$this->assertEquals($client->readFile($dropbox_filesystem), 'opening dropbox file'); | ||
*/ | ||
|
||
// so we use an adapter which implements the interface we want to adhere to | ||
$dropbox_filesystem = new AdapterPattern\App\Dropbox(); | ||
$this->assertEquals($client->readFile(new AdapterPattern\App\FilesystemAdapter($dropbox_filesystem)), 'opening dropbox file'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace AdapterPattern\App; | ||
|
||
class Client | ||
{ | ||
public function readFile(FilesystemInterface $filesystem) | ||
{ | ||
return $filesystem->open(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace AdapterPattern\App; | ||
|
||
interface CloudFilesystemInterface | ||
{ | ||
public function openFile(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace AdapterPattern\App; | ||
|
||
class Dropbox implements CloudFilesystemInterface | ||
{ | ||
public function openFile() | ||
{ | ||
return 'opening dropbox file'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace AdapterPattern\App; | ||
|
||
class FilesystemAdapter implements FilesystemInterface | ||
{ | ||
|
||
public function __construct(private CloudFilesystemInterface $filesystem) | ||
{ | ||
|
||
} | ||
|
||
public function open() | ||
{ | ||
return $this->filesystem->openFile(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace AdapterPattern\App; | ||
|
||
interface FilesystemInterface | ||
{ | ||
public function open(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace AdapterPattern\App; | ||
|
||
class LocalFilesystem implements FilesystemInterface | ||
{ | ||
|
||
public function open() | ||
{ | ||
return 'opening local file'; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters