How can I test this class using phpunit?

I am using Laravel 4.2 and am trying to use phpunit to test my code, not manual verification. I read Jeffrey Putin's book "Laravel Testing Decoded", but I still find my first test difficult. The class I'm trying to test is below. What I am struggling with - what should I check?

I don't think I should check the database or the $ model declaration as they should have their own tests. In this case, I think I need to either mock $ advert or create a factory for it, but I don't know which one.

Any pointers would be greatly appreciated.

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();

    }

}

      

+3


source to share


1 answer


You should check every method you have in your class. You have a constructor that also needs to be checked to see if it sets the model to your attributes, as well as your protected method.

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

$ composer require mockery/mockery



Then in a 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();
    }

}

      

+2


source







All Articles