hotel-quickly/nette-phpunit
Composer 安装命令:
composer require hotel-quickly/nette-phpunit
包简介
Nette + PHPunit + Multiple dbs + fixtures
README 文档
README
- Support multiple database connections
- Support multiple fixtures formats loading (for each connection)
Getting started
Installation
Run the following commands to install this package
composer require --dev hotel-quickly/nette-phpunit
Usage
In order to use Database test case, you must extend your base class from HQ\Test\AbstractDbTestCase and implement these 3 methods
getContainer- This method will returnNette\Di\Containerinstance.getConnections- Return an array ofHQ\Test\Connection\AbstractConnection(connectionName, baseSchema, ...)connections object.
- connectionName - A database connection name, which will be used as a prefix of fixture file.
- baseSchema - Database schema which includes
CREATE TABLE ...sql statements.
getBaseFixtureDir- Return a base directory for searching base fixture file (see
use HQ\Test\AbstractDbTestCase; use HQ\Test\Connection\NetteConnection; class YourBaseDbTestCase extends AbstractDbTestCase { protected function getContainer() { return require dirname(__DIR__) . '/app/bootstrap.php'; } protected function getConnections() { return [ new NetteConnection('default', __DIR__ . '/../tests/skeleton.sql', $this->container->getService('database.default.context')) ]; } public function getBaseFixtureDir() { return __DIR__; } }
Working with fixtures
Loading fixtures
By default, fixtures will be automatically loaded from 3 places, in the following orders
base fixturesclass fixturesinstance fixtures
1. base fixtures
This is a primary fixtures that will be loaded for all test cases (think of it as initial data for the whole apps)
test
+- YourBaseDbTestCase.php
+- <connection-name>-fixtures.{yaml, json, php} e.g. default-fixtures.json
2. class fixtures
This will be loaded each time test case was invoked.
test
+- User
+- UserTest.php
+- <connection-name>-fixtures.{yaml, json, php} e.g. default-fixtures.json
3. instance fixtures
This is pretty much the same as class fixtures, except that we define the fixture inside TestCase itself (so only php array is supported.
class MyTest extends MyBaseDbTestCase { public function getFixtures() { return [ '<connection-name>' => [ ... ] ] } }
####Fixture formats + structure
This library currently supports 3 fixture formats e.g. json, yaml and php.
All formats has the same fixture structure i.e.
[
"<table-name>": [
[
"<field-name1>": "<value1>",
"<field-name2>": "<value2>"
],
...
]
]
json example
{ "user": [ {"username": "john"}, {"username": "jane"} ] }
yaml example
user: - username: john - username: jane
php example - NOTE - Don't forget to put return statement
return [ 'user' => [ ['username' => 'john'], ['username' => 'jane'], ] ]
Improving testing speed
Due to the overhead of creating database schema at the beginning of the tests (only once). This might slow down your test suites speed. You can avoid this problem by exporting UNITTEST_CREATE_SCHEMA=false env and then run unit test as usual. This will make it skips database schema create overhead.
Common problems
- InvalidArgumentException: There is already a table named account with different table definition.
This error indicates that your fixtures for same dataset (table e.g. user) has different column structure.
For example, you have default-fixtures.yaml that contains user table and it has the following fields
user: - username: john
But somewhere in other fixtures, you have same user table with different fields e.g.
user: - email: john
This will cause the above error. To fix this, just make sure you define the same fields on each fixtures.
- ErrorException: ini_set(): Cannot change zlib.output_compression - headers already sent
This happens when Nette has php ini zlib.output_compression enabled in the container. Removing this config during testing will fix this error
hotel-quickly/nette-phpunit 适用场景与选型建议
hotel-quickly/nette-phpunit 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.24k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 01 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 hotel-quickly/nette-phpunit 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hotel-quickly/nette-phpunit 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.24k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-01-14