orbituw/faketories 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

orbituw/faketories

Composer 安装命令:

composer require orbituw/faketories

包简介

A package to help create data sets when faking APIs for unit tests within laravel

README 文档

README

pipeline status

coverage report

This laravel packages providers a trait that can help generate data sets to be returned from fake API's during unit testing.

If you've watched Adam Wathan's Test Driven Laravel series you'll have seen that he recommends creating "fake" objects or gateway classes to be swapped into your Laravel applications container during testing in scenarios when a class needs to talk out to the internet.

If you've used Laravel's model factories before, using faketories should be convenient and instantly recognisable.

Getting started

You can install the package via composer:

composer require orbituw/faketories --dev

Register the package in the providers array of your config/app.php file: (Laravel 5.5+ users can skip this step)

\OrbitUW\Faketories\FaketoriesServiceProvider::class,

Generate a sample /tests/faketories/faketories.php definitions file:

php artisan faketories:install 

Usage Example

// give example of vrm lookup in real world

In the below example we needed to test that we could get a vehicles full details from an api, to present that information in our app, so we used faketories to create a fake gateway to generate the data needed

// Logical Steps

  1. Create an interface that lists out all of the methods that your real gateway will call so that your fake gateway can do the same.

  2. Create an interface so that your 'fake gateway' implements the same methods as your real gateway to ensure that the fake and real gateways can be bound to Laravels app container and swapped out with no issues.

  3. Ensure the fake gateway has the exact same methods and properties as the class that will be accessing the 'real' API.

  4. Modify the methods that access the 'real' API to point to the fake data generated by Faketories so the methods can be tested.

// Implementation

In app/Providers/AppServiceProvider.php bind the common interface to the Real Gateway within the laravel container.

    public function register()
    {
         $this->app->bind(VehicleGatewayInterface::class, RealVehicleApi::class);
    }

Then in your testing class, bind your interface to a specific instance of your fake gateway:

    public function test_you_can_get_vehicle_by_reg()
    {

        //create new instance of fake gateway
        $vehicleApi = new FakeVehicleApi();
 

        //generate a new faketory data set with specific values
        $vehicleApi->generate([
            'reg_no' => 've59hgp',
            'make' => 'audi',
        ]);
 

        //generate additional 5 totally random sets
        $vehicleApi->generate([], 5);
 

        //finally bind the interface to this gateway
        $this->app->instance(VehicleApiInterface::class, $vehicleApi);
    }

In your tests directory you will have a faketories directory which contains faketories.php

$faketory->define(\Tests\FakeVehicleApi::class, function( Generator $faker ){

    $transmissions = ['Manual', 'Auto'];

    return [
        'reg_no' => $faker->unique()->name,
        'make' => $faker->name(),
        'model' => $faker->numberBetween(100,400),
        'colour' => $faker->colorName,
        'year' => $faker->year,
        'engine' => $faker->numberBetween(500, 4000),
        'transmission' => $transmissions[ array_rand($transmissions) ],
    ];
});

This may look familiar to you if you've used laravel's model factories previously. You can define a class and a set of sample data you would like it to return. An instance of the faker library is passed into each definition so that you can create fake data sets quickly.

You must then create a new FakeApi class which will be used to create the testing end points for your fake API

class FakeVehicleApi implements VehicleApiInterface
{
    //import faketories trait
    use Faketories;
 

    public function getVehicleByReg($reg)
    {
        return $this->data->filter(function($vehicle) use($reg){
           return $vehicle->reg_no === $reg;
        })->first();
    }
}

note that we are using the Faketories trait which gives us access to a property called $data that will contain all of the data generated by the Faketories trait. $data is a Collection, you can then use any collection pipelines to manipulate the property.

Now when testing Faketory will produce the data acting as a fake api:

    public function test_you_can_get_vehicle_by_reg()
    {
        $vehicle = new FakeVehicleApi();
        $vehicle->generate([
            'reg_no' => 'TE57 ING'
        ]);
    
        $response = $vehicle->getVehicleByReg('TE57 ING');

        $this->assertEquals('TE57 ING', $response->reg_no);

    }

Enjoy :-)

统计信息

  • 总下载量: 533
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-07-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固