如何使用phpunit测试此类? [英] How do I test this class using phpunit?

查看:58
本文介绍了如何使用phpunit测试此类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel 4.2,并试图进入使用phpunit测试我的代码,而不是手动测试所有内容.我读过杰弗里·韦(Jeffrey Way)的书《 Laravel Testing Decoded》,但我仍然发现我的第一个测试很棘手.我要测试的课程如下.我正在苦苦挣扎的是-我应该测试什么?

I am using Laravel 4.2 and am trying to get into using phpunit to test my code rather than manually test everything. I have read Jeffrey Way's book 'Laravel Testing Decoded' but I still finding my very first test tricky. The class I am trying to test is below. What I am struggling with is - what should I test?

我不认为我应该测试数据库或Model $ advert,因为它们应该有自己的测试.在那种情况下,我认为我需要模拟$ advert或为其创建一个工厂,但我不知道哪个.

I don't think I should test the database or the Model $advert as these should have their own tests. In which case I think I need to either mock $advert or create a factory for it but I don't know which.

任何指针将不胜感激.

EloquentListing.php

EloquentListing.php

<?php

namespace PlaneSaleing\Repo\Listing;

use Illuminate\Database\Eloquent\Model;

class EloquentListing implements ListingInterface {

    protected $advert;

    public function __construct(Model $advert)
    {
        $this->advert = $advert;
    }

    /**
     * Get paginated listings
     *
     * @param int  Current page
     * @param int Number of listings per page
     * @return StdClass object with $items and $totalItems for pagination
     */
    public function byPage($page=1, $limit=10)
    {

        $result = new \StdClass;
        $result->page = $page;
        $result->limit = $limit;
        $result->totalItems = 0;
        $result->items = array();

        $listings = $this->advert
                         ->orderBy('created_at')
                         ->skip( $limit * ($page-1) )
                         ->take($limit)
                         ->get();

        // Create object to return data useful for pagination
        $result->items = $listings->all();
        $result->totalItems = $this->totalArticles;

        return data;

    }
    /**
     * Get total listing count
     *
     * 
     */
    protected function totalArticles()
    {

        return $this->advert->count();

    }

}

推荐答案

您必须测试类中具有的每个方法.您有构造函数,该构造函数也应经过测试,以查看是否将模型设置为属性以及受保护的方法.

You have to test every method you have in your class. You have constructor, which should be also tested, to see, if it sets model to your attributes, as well your protected method.

您应该用嘲弄来嘲弄您的模型.可以使用

You should mock your model with mockery. It can be installed with

$ composer require mockery/mockery

然后在您的测试文件中:

Then in your test file:

<?php

use Mockery;
use ReflectionClass;
use PlaneSaleing\Repo\Listing\EloquentListing;

class EloquentListingTest extends \TestCase
{

    /**
     * Testing if __constructor is setting up property
     */
    public function testModelSetsUp()
    {
        $mock = Mockery::mock(Illuminate\Database\Eloquent\Model::class);

        $listing = new EloquentListing($mock);

        $reflection = new ReflectionClass($listing);

        // Making your attribute accessible
        $property = $reflection->getProperty('advert');
        $property->setAccessible(true);

        $this->assertInstanceOf(Illuminate\Database\Eloquent\Model::class, $property);
    }

    /**
     * Here you will check if your model is recieving calls
     */
    public function testByPage()
    {
       $mock = Mockery::mock(Illuminate\Database\Eloquent\Model::class);

       $mock->shouldReceive('orderBy')
            ->with('created_at')
            ->once()
            ->andReturn(Mockery::self())
            ->shouldReceive('skip')
            ->with(10)
            ->once()
            ->andReturn(Mockery::self())
            ->shouldReceive('take')
            ->with(10)
            ->andReturn(Mockery::self())
            ->shouldReceive('get')
            ->once()
            ->andReturn(Mockery::self())
            ->shouldReceive('all')
            ->once()
            ->andReturn(Mockery::self());

        $listing = new EloquentListing($mock);
    }

    /**
     * Here you will see, if your model is receiving call '->count()'
     */
    public function testTotalArticles()
    {
        $mock = Mockery::mock(Illuminate\Database\Eloquent\Model::class);

        $mock->shouldReceive('count')
            ->once()
            ->andReturn(Mockery::self());

        $listing = new EloquentListing($mock);

        // We will have to set method accesible
        $reflection = new ReflectionClass($listing);

        $method = $reflection->getMethod('totalArticles');
        $method->setAccessible(true);

        $listing->totalArticles();
    }

}

这篇关于如何使用phpunit测试此类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆