如何使用phpunit测试这个类?
我正在使用Laravel4.2,并尝试使用phpunit来测试我的代码,而不是手动测试所有内容。我读过Jeffrey Way的书《Laravel测试解码》,但我仍然觉得我的第一次测试很棘手。下面是我要测试的课程。我正在挣扎的是——我应该测试什么 我不认为我应该测试数据库或$advert模型,因为它们应该有自己的测试。在这种情况下,我想我需要模拟$advert或者为它创建一个工厂,但我不知道是哪个 任何指点都将不胜感激 elountellisting.php如何使用phpunit测试这个类?,php,unit-testing,laravel,phpunit,Php,Unit Testing,Laravel,Phpunit,我正在使用Laravel4.2,并尝试使用phpunit来测试我的代码,而不是手动测试所有内容。我读过Jeffrey Way的书《Laravel测试解码》,但我仍然觉得我的第一次测试很棘手。下面是我要测试的课程。我正在挣扎的是——我应该测试什么 我不认为我应该测试数据库或$advert模型,因为它们应该有自己的测试。在这种情况下,我想我需要模拟$advert或者为它创建一个工厂,但我不知道是哪个 任何指点都将不胜感激 elountellisting.php <?php namespace
<?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();
}
}
您必须测试类中的每个方法。您有一个构造函数,也应该对它进行测试,以查看它是否将模型设置为您的属性以及受保护的方法
你应该用嘲弄来嘲弄你的模型。它可以安装在
$composer需要模仿/模仿
然后在测试文件中:
<?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();
}
}
什么是ReflectionClass?它是一个PHP类,提供有关任何其他类的信息。您可以在此处看到更多信息: