derptest / phpmachinist
PHP的对象工厂测试
Requires
- php: >5.3.0
Requires (Dev)
- ext-pdo_mysql: *
- ext-pdo_sqlite: *
- doctrine/orm: ~2.0
- phake/phake: ~2.0
- phpunit/phpunit: ~4.5
This package is auto-updated.
Last update: 2024-09-10 10:52:19 UTC
README
PHPMachinist:对象工厂测试
什么???!!!
创建数据库固定值的一种稍微不那么令人烦恼的方法。它从几个项目中借鉴了很多
- https://github.com/chriskite/phactory
- https://github.com/thoughtbot/factory_girl
- https://github.com/ccollins/milkman
- https://github.com/notahat/machinist
它们真的很棒。只是语言不对,或者没有完全达到我的需求。
Hip Hop VM (HHVM) 支持
Hip Hop VM 已得到支持和测试。如果您想使用 Doctrine 存储,则需要 Doctrine ORM 2.4.x 或更高版本。
数据存储支持
目前支持以下数据存储
- PDO
- MySQL
- SQLite
- Doctrine 2 ORM
安装
将包 derptest/phpmachinist 添加到您的 composer.json 中。有关 Composer 的更多信息,请访问 https://composer.php.ac.cn
配置
PHP Machinist 的配置分为两个步骤
-
注册数据存储
通过静态方法
Machinist::store()或 Machinist 实例上的addStore()方法注册数据存储。这两种方法接受相同的参数,一个StoreInterface实例和一个可选的存储名称。如果没有提供名称,则默认为default。以下是两种方法的示例<?php use DerpTest\Machinist\Machinist; use DerpTest\Machinist\Store\SqlStore; // This store will be referenced by the name "default" Machinist::store(SqlStore::fromPdo(new \PDO('sqlite::memory:'))); // This store will be referenced by the name "non-default" Machinist::instance()->addStore( SqlStore::fromPdo(new \PDO('sqlite::memory:')), 'non-default' );
注意:当使用
SqlStore::fromPdo时,pdo 错误模式将被设置。默认情况下,它将设置为异常,这样触发错误的测试就会抛出异常并失败。可以通过将错误模式作为工厂方法的第二个参数传递来覆盖默认行为。 -
定义蓝图
蓝图通过允许您配置以下内容来定义您的数据存储中的实体
- 用于保存或检索蓝图数据的
StoreInterface - 数据将存储的表/集合
- 表/集合中列/属性的默认值。默认值允许您只处理对测试逻辑真正重要的数据,而不是浪费时间在设置对您的数据存储无意义但必需的值上,从而使测试代码变得杂乱。
- 蓝图与其他先前定义的蓝图之间的关系
可以为同一表/集合定义多个蓝图。这允许您快速为数据设置多个默认值集。以下示例显示了为具有不同角色的用户表创建两个蓝图。定义多个蓝图可以使您的测试更易于阅读,而且编写速度更快。
<?php use DerpTest\Machinist\Machinist; use DerpTest\Machinist\Store\SqlStore; use DerpTest\Machinist\Blueprint; // Register the default data store Machinist::store(SqlStore::fromPdo(new \PDO('sqlite::memory:'))); // Create a company blueprint using the "hard way". This will be used in a relationship $company = new Blueprint( Machinist::instance(), 'company', array( 'streetAddress' => '123 Any Street', 'city' => 'Any Town', 'state' => 'NV', 'zip' => '89101' ) ); Machinist::instance()->addBlueprint('company', $company); // Create a standard user blueprint using the "easy way" Machinist::blueprint( 'user', // This is the blueprint name array( 'role' => 'USER', // The user will default to the STANDARD_USER role 'active' => true // The user will default to active 'company' => Machinist::relationship($company) // Create relationship ), 'user', // The "user" table/collection to used. Not required // as the name is the same as the table/collection 'default' // Data store. Not required if "default" ); // Create an administrator user blueprint using the "easy way" Machinist::blueprint( 'administratorUser', // This is the blueprint name array( 'role' => 'ADMINISTRATOR', // The user will default to the STANDARD_USER role 'active' => true // The user will default to active 'company' => Machinist::relationship($company) // Create relationship with company blueprint ), 'user' // The "user" table/collection to used. Required as // the blueprint name is not the same as the // table/collection );
- 用于保存或检索蓝图数据的
关系的深入探讨
关系可能是 PHP Machinist 最强大的功能之一。它们允许您快速关联相关数据,而无需担心实际测试中的主键、外键和其他无意义的东西。它们甚至会对您做一些 "find or create" 魔法。以下是如何使用上面的示例蓝图快速创建两个用户和一个公司的示例。
<?php // ... Machinist::blueprint('user')->make( array( 'username' => 'pedro@voteforpedro.org', 'company' => array('name' => 'Pedro for Class President') ) ); Machinist::blueprint('user')->make( array( 'username' => 'napoleon@voteforpedro.org', 'company' => array('name' => 'Pedro for Class President') ) );
这是创建两个用户和一个公司所需代码的填充总和。PHP Machinist 在第一个蓝图调用中施展了一些魔法。它查找具有 name 的公司为 Pedro for Class President。但没有找到,因此它创建了一个公司并用于关系。对于第二个 make() 调用,它找到了第一个调用中创建的公司,并使用该公司进行关系。
关系也会填充从数据查找中获得的数据。以下是一个使用相同蓝图和从前一个示例中创建的数据的示例。
<?php // ... $pedro = Machinist::blueprint('user')->findOne(array('username' => 'pedro@voteforpedro.org')); echo ($pedro->company->name);
这将导致 Pedro for Class President 显示在屏幕上。
许可证
请参阅项目中的 LICENSE 文件