lyw0301 / yaf-starter-kit
Yaf 快速开发工具包,项目模板.
Requires
- php: >=7.0
- ext-yaf: *
- illuminate/database: ^5.8
- illuminate/events: ^5.8
- illuminate/pagination: ^5.8
- league/plates: ^3.3
- mongodb/mongodb: ^1.4
- monolog/monolog: ~1.0
- pda/pheanstalk: ^4.0
- php-amqplib/php-amqplib: ^2.8
- predis/predis: ^1.1
- psr/container: ^1.0
- psr/http-message: ~1.0
- qiniu/php-sdk: ^7.2
- symfony/console: 3.*
Requires (Dev)
- filp/whoops: ~2.1@stable
- friendsofphp/php-cs-fixer: ~1.8@stable
- mockery/mockery: ~1.0
- phpmd/phpmd: ~2.6@stable
- sebastian/phpcpd: ~2.0@stable
- squizlabs/php_codesniffer: 3.0.x-dev
- symfony/var-dumper: ~3.0
Suggests
- illuminate/database: The best PHP ORM Engine from Laravel.
- illuminate/validation: The form validation component from Laravel.
- league/plates: Plates is a native PHP template system that's fast, easy to use and easy to extend.
This package is auto-updated.
Last update: 2025-03-11 17:25:32 UTC
README
The Yaf testable starter kit and composer supported.
集成 illuminate/database, 极大的方便在 Yaf 中进行数据库操作。
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 lyw0301/yaf-starter myapp -vvv
-
Web server rewrite rules:
Apache
#.htaccess RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule .* index.php
Nginx
server { listen 80; server_name mongochina.com; root /path/to/mongochina; index index.php index.html index.htm; if (!-e $request_filename) { rewrite ^/(.*) /index.php/$1 last; } }
Lighttpd
$HTTP["host"] =~ "(www.)?mongochina.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 # yaf commands (namespace:\App\Commands)
│ ├── controllers # Yaf Controllers (namespace:\)
│ ├── models # Yaf Models (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
├── yaf # 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
$ ./yaf make:controller Foo_Bar # or:foo_bar/FooBar/FooBarController # # /www/mongochina.com/app/controllers/Foo/Bar.php Created! # /www/mongochina.com/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:
$ ./yaf make:test Foo_Bar # Also supports multiple type controller names # /www/mongochina.com/tests/controllers/Foo/BarTest.php Created!
The handle() method
The controller entry method handle()
:
<?php 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' => 'lyw0301', 'age' => 28, ]; return view('profile-page', $data); }
<!--app/views/profile-page.php:--> <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:
$ ./yaf 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 = Reuqest::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' => 'MongoChina', '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/
License
MIT