Laravel Virgin:在phpunit集成测试中建立和销毁数据库

时间:2019-06-25 09:44:07

标签: laravel phpunit integration-testing

使用nilportuguess雄辩的存储库library,创建了以下(有错误的)存储库:

namespace App\Repositories;

use NilPortugues\Foundation\Infrastructure\Model\Repository\Eloquent\EloquentRepository;
use App\Model\Rover;

class RoverRepository extends EloquentRepository
{

    /**
     * {@inheritdoc}
     */
    protected function modelClassName()
    {
        return Rover::class;
    }

    /**
     * {@inheritdoc}
     */
    public function find(Identity $id, Fields $fields = null)
    {
        $eloquentModel = parent::find($id, $fields);

        return $eloquentModel->toArray();
    }

    /**
     * {@inheritdoc}
     */
    public function findBy(Filter $filter = null, Sort $sort = null, Fields $fields = null)
    {
        $eloquentModelArray = parent::findBy($filter, $sort, $fields);

        return $this->fromEloquentArray($eloquentModelArray);
    }

    /**
     * {@inheritdoc}
     */
    public function findAll(Pageable $pageable = null)
    {
        $page = parent::findAll($pageable);

        return new Page(
            $this->fromEloquentArray($page->content()),
            $page->totalElements(),
            $page->pageNumber(),
            $page->totalPages(),
            $page->sortings(),
            $page->filters(),
            $page->fields()
        );
    }

    /**
    * @param array $eloquentModelArray
    * @return array
    */
   protected function fromEloquentArray(array $eloquentModelArray) :array
   {
        $results = [];
        foreach ($eloquentModelArray as $eloquentModel) {
            //This is required to handle findAll returning array, not objects.
            $eloquentModel = (object) $eloquentModel;

            $results[] = $eloquentModel->attributesToArray();
        }

        return $results;
   }
}

为了找到它们,我想对一个sqlite内存数据库进行集成测试:

namespace  Test\Database\Integration\Repositories;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Repositories\RoverRepository;
use App\Model\Rover;
use App\Model\Grid;

class RoverRepositoryTest extends TestCase
{
    use RefreshDatabase;

    private $repository=null;

    public function setUp(): void
    {
        parent::setUp();
        $grid=factory(Grid::class)->create([
            'width'=>5,
            'height'=>5
        ]);
        $rover=factory(Rover::class, 5)->create([
            'grid_id' => $grid->id,
            'grid_pos_x' => rand(0, $grid->width),
            'grid_pos_y' => rand(0, $grid->height),
        ]);

        //How do I run Migrations and generate the db?

        $this->repository = new RoverRepository();
    }

    public function tearDown(): void
    {
        parent::tearDown();
        //How I truncate and destroy Database?
    }

   /**
     * Testing Base Search
     *
     * @return void
    */
    public function testBasicSearch(): void
    {
        //Some Db test
    }
}

但是我有一些问题:

  • 如何保存通过工厂模型生成的?
  • 如何在tearDown()中核对我的数据库?

0 个答案:

没有答案