mezon / infrastructure-layer
基础设施层
1.2.6
2022-09-13 09:10 UTC
Requires
- php: >=7.2.0
- mezon/conf: 1.2.*
Requires (Dev)
- infection/infection: ^0.21.5
- phpunit/phpunit: ^8.5
- vimeo/psalm: ^4.2
README
简介
此类提供了从基本基础设施事物中抽象出来的功能,这可能会使您的单元测试变得更加困难。例如:
- 控制台输出
- 文件系统操作
- GD 库
- HTTP 头部
- 重定向
- 会话
- 系统方法
基本概念
每个抽象都可以配置为使用原生函数调用(如file_get_contents)或模拟。
模拟调用可以在单元测试中使用。而原生调用将在生产环境中执行。
让我们看看一些真实的代码示例
// here we setup mock calls \Mezon\Conf\Conf::setConfigStringValue('fs/layer', 'mock'); // here we setup that mock will return true if the we check that this directory exists \Mezon\Fs\Layer::$createdDirectories[] = [ 'path' => 'path-to-existing-directory' ]; // here we will get true var_dump(\Mezon\Fs\Layer::directoryExists('path-to-existing-directory')); // and here we will get false var_dump(\Mezon\Fs\Layer::directoryExists('path-to-unexisting-directory'));
在这两个调用中,Mezon\Fs\Layer::directoryExists
没有执行方法 file_exists
的真实调用。
现在让我们看看我们有哪些抽象
控制台层
类 Mezon\Console\Layer
为控制台操作提供抽象
// here we setup mock calls Conf::setConfigStringValue('console/layer', 'mock'); // setup values wich will be returned by the method Mezon\Console\Layer::readline() \Mezon\Console\Layer::$readlines[] = 'line 1'; \Mezon\Console\Layer::$readlines[] = 'line 2'; // read lines // note that native php method `readline` will noe be called // just returning value of the Mezon\Console\Layer::$readlines array echo \Mezon\Console\Layer::readline(); // ouputs 'line 1' echo \Mezon\Console\Layer::readline(); // ouputs 'line 2' // here we setup native php methods calls Conf::setConfigStringValue('console/layer', 'real'); echo \Mezon\Console\Layer::readline(); // outputs string wich you will input in the console // because in this line real method `readline` will be called
文件系统层
此包还提供了文件系统常规操作的抽象
// here we setup mock calls Conf::setConfigStringValue('fs/layer', 'mock'); // writing file \Mezon\Fs\Layer::filePutContents('file-path', 'data', FILE_APPEND); // ^^^ but in this call all file content will be stored into memory, not on disk // on production this call works like file_put_contents // creating directory \Mezon\Fs\Layer::createDirectory('new-dir'); // ^^^ real directory also is not created // checking if the directory exists echo \Mezon\Fs\Layer::directoryExists('new-dir'); // we shall see 'true', because we have called \Mezon\Fs\Layer::createDirectory('new-dir'); echo \Mezon\Fs\Layer::directoryExists('unexisting-dir'); // we shall see 'false' // checking if the file exists echo \Mezon\Fs\Layer::fileExists('file-path'); // we shall see 'true', because we have called \Mezon\Fs\Layer::filePutContents('file-path', 'data', FILE_APPEND) echo \Mezon\Fs\Layer::fileExists('unexisting-file-path'); // we shall see 'false' // getting file contents echo \Mezon\Fs\Layer::fileGutContents('file-path'); // we shall see 'data', because we have already called \Mezon\Fs\Layer::filePutContents('file-path', 'data', FILE_APPEND) echo \Mezon\Fs\Layer::fileGutContents('unexisting-file-path'); // we shall see 'false'
内存文件系统
\Mezon\Fs\Layer
中的几乎所有方法在模拟模式下都使用 \Mezon\Fs\InMemory
。
// writing data in memory file system \Mezon\Fs\InMemory::filePutContents('file-path', 'data'); // reading data from inmemory file system echo \Mezon\Fs\InMemory::fileGetContents('file-path'); // checking that file esists echo \Mezon\Fs\InMemory::fileExists('file-path'); // outputs 'true' echo \Mezon\Fs\InMemory::fileExists('unexisting-file'); // outputs 'false' // preload file in FS // this method reads real file from your FS and uploads it into inmemory FS \Mezon\Fs\InMemory::preloadFile('real-file-path');
GD 模拟
我们还提供了一些 GD 函数的模拟。基本上是那些与文件系统交互的。
Conf::setConfigStringValue('gd/layer', 'mock'); // here we are trying to get file with this path from \Mezon\FS\InMemory var_dump(\Mezon\GD\Layer::getImageSize('path-to-image')); // reading image from file $resource = \Mezon\GD\Layer::imageCreateFromBmp('path-to-image-file'); $resource = \Mezon\GD\Layer::imageCreateFromGif('path-to-image-file'); $resource = \Mezon\GD\Layer::imageCreateFromJpeg('path-to-image-file'); $resource = \Mezon\GD\Layer::imageCreateFromPng('path-to-image-file'); $resource = \Mezon\GD\Layer::imageCreateFromWebp('path-to-image-file'); // store images into file. In mock mode all data is stored into in-memory FS - \Mezon\FS\InMemory \Mezon\GD\Layer::imageBmp($resource, 'path-to-file'); \Mezon\GD\Layer::imageGif($resource, 'path-to-file'); \Mezon\GD\Layer::imageJpeg($resource, 'path-to-file'); \Mezon\GD\Layer::imagePng($resource, 'path-to-file'); \Mezon\GD\Layer::imageWebp($resource, 'path-to-file');
HTTP 头部模拟
您可以使用头部。为此提供了两种方法
Conf::setConfigStringValue('headers/layer', 'mock'); // setting headers wich will be returned by the mock \Mezon\Headers\Layer::setAllHeaders([ 'Header Name' => 'Header Value' ]); // in mock mode this method will return data wich was set by \Mezon\Headers\Layer::setAllHeaders // in real mode - th resul of the method \getallheaders will be returned var_dump(\Mezon\Headers\Layer::getAllHeaders());
重定向模拟
您可以模拟重定向
Conf::setConfigStringValue('redirect/layer', 'mock'); // in real mode this call method header('Location: ...') will be called // in mock mode no redirection will be performed and redirect URL will be stored // in the \Mezon\Redirect\Layer::$lastRedirectionUrl // and \Mezon\Redirect\Layer::$redirectWasPerformed will be set to 'true' \Mezon\Redirect\Layer::redirectTo('./url');
会话模拟
您也可以模拟会话方法
Conf::setConfigStringValue('session/layer', 'mock'); // method returns 'session-name' if it is used as mock mode // and return result of session_name() if it is used in real model \Mezon\Session\Layer::sessionName(); // setting cookie \Mezon\Session\Layer::setCookie( string $name, string $value = "", int $expires = 0, string $path = "", string $domain = "", bool $secure = false, bool $httponly = false); // getting session id or setting it \Mezon\Session\Layer::sessionId(string $id = ''): string; // saving session data and closing session \Mezon\Session\Layer::sessionWriteClose();
系统方法模拟
您还可以模拟一些系统方法调用。例如,例如 die
Conf::setConfigStringValue('system/layer', 'mock'); // in mock mode field $dieWasCalled will be set to 'true' // in real mode built-in PHP method 'die' will be called \Mezon\System\Layer::die();