Yaf skeleton
The Yaf testable skeleton and composer supported.
Requirement
- PHP >= 7.0
- Yaf >= 3.0
Installation
- Update
yaf.ini
:
[yaf]
yaf.use_namespace=1
yaf.use_spl_autoload=1
...
- Create project.
$ composer create-project overtrue/yaf-skeleton myapp -vvv
-
Web server rewrite rules:
Apache
#.htaccess RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule .* index.php
Nginx
server { listen 80; server_name myapp.com; root /path/to/myapp; index index.php index.html index.htm; if (!-e $request_filename) { rewrite ^/(.*) /index.php/$1 last; } }
Lighttpd
"/index.php/$1", ) }">$HTTP["host"] =~ "(www.)?myapp.com$" { url.rewrite = ( "^/(.+)/?$" => "/index.php/$1", ) }
Application structor
├── .scripts
│ ├── .gitkeep
│ ├── common.sh
│ ├── job_phpcs.sh
│ ├── job_phpmd.sh
│ ├── job_phpunit.sh
│ ├── sami.phar
│ └── sami.php
├── app
│ ├── commands # sora commands (namespace:\App\Commands)
│ ├── controllers # Yaf Controllers (namespace:\)
│ ├── exceptions # Exceptions (namespace:\App\Exceptions)
│ ├── facades # Service Facades (namespace:\)
│ ├── plugins # Yaf plugins (namespace:\)
│ ├── presenters # Object presenters (namespace:\App\Presenters)
│ ├── services # Services, the 3rd service bridges (namespace:\App\Services)
│ ├── traits # Traits (namespace:\App\Traits)
│ ├── views # Template files
│ ├── helpers.php # Herlpers
│ ├── Bootstrap.php # Yaf Bootstrap file
├── config
│ ├── application.ini # Yaf config file
├── public # web extrence
│ └── index.php
├── sora # The command line tool
├── tests # Unit tests
└── vendor #
├── phpunit.xml.dist # PHPUnit config file
├── .gitignore
├── .php_cs # PHP-CS-Fixer config file
├── composer.json
├── composer.lock
├── README.md
Controllers
$ ./sora make:controller Foo_Bar # or:foo_bar/FooBar/FooBarController
#
# /www/myapp/app/controllers/Foo/Bar.php Created!
# /www/myapp/tests/controllers/Foo/BarTest.php Created!
All controllers are created in the app/controllers
directory,and test files are also created in the tests/controllers
directorty.
Of course, you can also create tests independently:
$ ./sora make:test Foo_Bar # Also supports multiple type controller names
# /www/myapp/tests/controllers/Foo/BarTest.php Created!
The handle() method
The controller entry method handle()
:
class ExampleController extends BaseController
{
public function handle()
{
return 'Hello world!';
// return json(['foo' => 'bar']);
// return redirect('https://easywechat.com');
// return view('welcome', ['name' => 'MyApp']); // template engine require.
}
}
Views
There is no built-in template engine. If you need to use a PHP template, we recommend that you use Plates, Plates is a native PHP template system that's fast, easy to use and easy to extend.
$ composer require league/plates -vvv
You can use view(string $template, array $data)
helper in controller handle
method as a result.
for example:
public function handle()
{
$data = [
'name' => 'overtrue',
'age' => 28,
];
return view('profile-page', $data);
}
<h1><?= $name ?>h1>
<p><?= $age ?>p>
More usage please read the Plates Docs.
Unit tests
The difficulty of writing unit tests is inversely proportional to the quality of your code. The higher the code quality, the lower the difficulty of unit testing, so design your code well.
The unit test for creating the controller can be done with the following command:
$ ./sora make:test Foo_BarController
Write test cases
To create a controller test object, use,you can use the mock_controller
function:
$controller = mock_controller(Foo_BarController::class);
// Indicates the method to mock, and the protected method is also mockable
$controller = mock_controller(Foo_BarController::class, ['getUsers', 'getApp']);
Assertion
We have such a controller:
...
public function handle()
{
$params = Request::only('uids', 'screen_name', 'trim_status', 'has_extend', 'simplify', 'is_encoded');
$users = $this->get('main.users.show_batch', $params);
return $users;
}
...
So the test should cover the above three behaviors:
public function testHandle()
{
$input = [
'uids' => '2193182644',
'screen_name' => '安正超',
'trim_status' => 0,
'has_extend' => 1,
'simplify' => 0,
'is_encoded' => 0,
'foo' => 'bar',
];
Request::shouldReceive('only')
->with('uids', 'screen_name', 'trim_status', 'has_extend', 'simplify', 'is_encoded')
->andReturn($input);
$controller = mock_controller(Users_Show_BatchController::class, ['get']); // mock the `get` method
$controller->shouldReceive('get')->with('main.users.show_batch', array_except($_GET, ['foo']))
->andReturn('foo')
->once();
$response = $controller->handle();
$this->assertSame('foo', $response);
}
Facade Assertion
Request::shouldReceive('get')->with('mid')->andReturn('mock-mid')->once();
Log::shouldReceive('action')->with(48, 'oid', 'ext')->once();
..
Built-in auxiliary assertion methods
$this->shouldAbort($message, $code);
They are used to correspond to exceptions thrown by abort($message, $code);
in the controller
Some helper methods in test case classes
Mock request method:
$this->method('post');
Mock request uri
:
$this->uri('/foo/bar?uid=12345');
Mock config:
$this->config('foo', 'bar');
Mock request IP:
$this->ip('127.0.0.1');
Mock $_SERVER vars:
$this->server('REQUEST_URI', '/foo/bar');
Docs
- Yaf Docs: http://www.php.net/manual/en/book.yaf.php
- Plates Docs: http://platesphp.com/v3/
- PHPUnit Docs: https://phpunit.de/manual/current/zh_cn/phpunit-book.html
- Mockery Docs: http://docs.mockery.io/en/latest/
❤️
Sponsor me
如果你喜欢我的项目并想支持它,点击这里
Project supported by JetBrains
Many thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.
PHP 扩展包开发
想知道如何从零开始构建 PHP 扩展包?
请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— 《PHP 扩展包实战教程 - 从入门到发布》
License
MIT