OOP :
interface FileClientInterface {
function open($path);
function exist($path);
}
class FileClient implements FileClientInterface {
function open($path)
{
fopen($path, "w+");
}
function exist($path)
{
return file_exists($path);
}
}
class MyClass
{
private $fileClient;
public function __construct(FileClientInterface $fileClient)
{
$this->fileClient = $fileClient;
}
public function createFile($dirPath)
{
$name = time() . "-RT";
$file = $dirPath . '/' . $name . '.tmp';
$this->fileClient->open($file);
fopen($file, "w+");
if ($this->fileClient->exist($file)) {
return $name . '.tmp';
} else {
return '';
}
}
}
class MockFileClient implements FileClientInterface {
public $calledOpen = 0;
public $calledExists = 0;
public function open($path)
{
$this->calledOpen ++;
}
public function exist($path)
{
$this->calledExists++;
}
}
$mockFileClient = new MockFileClient();
$myClass = new MyClass($mockFileClient);
$myClass->createFile('/test/path');
print_r($mockFileClient->calledOpen . PHP_EOL);
print_r($mockFileClient->calledExists . PHP_EOL);
, (FileClientInterface) , ( ). . , - .