测试带有注入依赖项的实现时出现phpunit错误

时间:2015-08-07 11:42:23

标签: php unit-testing laravel interface phpunit

我正在尝试在我构建的名为EloquentListing的类上设置phpunit测试,该类实现了一个名为ListingInterface的接口。 EloquentListing模型的构造函数需要注入Eloquent模型。因此,我使用服务提供程序将实现绑定到接口并注入名为RepoServiceProvider的模型。但是,当我运行phpunit时出现以下错误:

  

.PHP致命错误:无法在第11行的/home/cabox/workspace/app/tests/PlaneSaleing/Repo/Listing/EloquentListingTest.php中实例化接口PlaneSaleing \ Repo \ Listing \ ListingInterface

我的代码如下:

ListingInterface.php

<?php

namespace PlaneSaleing\Repo\Listing;

interface ListingInterface {

    public function byPage($page=1, $limit=10);
}

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;

    }

}

RepoServiceProvider.php

<?php

namespace PlaneSaleing\Repo;

use Illuminate\Support\ServiceProvider;
use PlaneSaleing\Repo\Listing\EloquentListing as Listing;
use Advert;

class RepoServiceProvider extends ServiceProvider {

    public function register()
    {

      $this->app->bind('PlaneSaleing\Repo\Listing\ListingInterface', function($app) {
        return new Listing(new Advert);
      } );

    }

}

EloquentListingTest.php

<?php

use PlaneSaleing\Repo\Listing\EloquentListing as Listing;

class EloquentListingTest extends TestCase {

    public function testListingByPage()
    {

        // Given
        $listing = new Listing(Mockery::mock('Advert'));
        $result = $listing->byPage();

        // When


        // Then
        $this->assertTrue($result == 10);
    }

}

0 个答案:

没有答案