php – Mocking Static Eloquent Models方法包括find()

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – Mocking Static Eloquent Models方法包括find()脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在关注一般的Mockery和 PHP-UnIT教程 – 包括Jeffrey Way介绍用PHP-Unit和Mockery测试Laravel.但是,对于这个应用程序 – 我们很好地依赖于Eloquent,而宁愿不创建存储库类.

我们能够很好地模拟我们的Widget模型的实例方法.但是,我们正在使用Route:模型绑定,并且我承认在测试控制器的show($widget)方法时,我不确定如何模拟模型的find()方法.

我已经阅读了https://github.com/padraic/mockery/wiki#mocking-public-static-methods个文档,并且看到“别名”前缀可以放在类的前面进行模拟.但我似乎无法让这个工作.

这是routes.PHP

Route::model('widgets','Widget');
Route::resource('widgets','WidgetController');

这是(缩短的)控制器……

<?PHP
/*
|--------------------------------------------------------------------------
| Widget Controller
|--------------------------------------------------------------------------
|
| An example controller that uses the PRe-baked RESTful resource controller
| actions for index,create,Store,show,edit,update,destroy,as well as a
| delete method to show the record before deletion.
|
| See routes.PHP  ->
| Route::resource('widget','WidgetController');
| Route::get('widget/{widget}/delete','WidgetController@delete');
|
*/

class WidgetController extends BaseController
{
    /**
     * Widget Model
     * @VAR Widget
     */
    protected $widget;

    /**
     * Inject the model.
     * @param Widget $widget
     */
    public function __construct(Widget $widget)
    {
        parent::__construct();
        $this->widget = $widget;
    }

    /**
     * Display a listing of the resource.
     *
     * See public function data() below for the data source for the list,* and the view/widget/index.blade.PHP for the jquery script that makes
     * the Ajax request.
     *
     * @return Response
     */
    public function index()
    {
        // Title
        $title = Lang::get('widget/title.widget_management');

        // Show the page
        return View::make('widget/index',compact('title'));
    }

    /**
     * Show a single widget details page.
     *
     * @return View
     */
    public function show($widget)
    {
        // Title
        $title = Lang::get('widget/title.widget_show');

        // Show the page
        return View::make('widget/show',compact('widget','title'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        // Title
        $title = Lang::get('widget/title.create_a_new_widget');

        // Show the page
        return View::make('widget/create',compact('title'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        // Validate the inputs
        $rules = array(
            'name'=> 'required|alpha_dash|unique:widgets,name','description'=> 'required'
            );

        // Validate the inputs
        $validator = Validator::make(Input::all(),$rules);

        // Check if the form validates with success
        if ($validator->passes()) {
            // Get the inputs,with some exceptions
            $inputs = Input::except('csrf_token');

            $this->widget->name = $inputs['name'];
            $this->widget->description = $inputs['description'];
            $this->widget->save($rules);

            if ($this->widget->id) {
                // redirect to the new widget page
                return Redirect::to('widgets')->with('success',Lang::get('widget/messages.create.success'));

            } else {
                // Redirect to the widget create page
                //var_dump($this->widget);
                return Redirect::to('widgets/create')->with('error',Lang::get('widget/messages.create.error'));
            }
        } else {
            // Form validation Failed
            return Redirect::to('widgets/create')->withinput()->withErrors($validator);
        }
    }

}

这是测试夹具.

# /app/tests/controllers/WidgetControllerTest.PHP

class WidgetControllerTest extends TestCase
{
    public function __Construct()
    {
        $this->;mock = Mockery::mock('Eloquent','Widget');
    }

    public function SETUP()
    {
        parent::setUp();

        $this->app->instance('Widget',$this->mock);
    }

    public function tearDown()
    {
        Mockery::close();
    }

    /**
     * Index
     */
    public function testIndex()
    {
        $this->call('GET','widgets');

        $this->assertTrue($this->client->getResponse()->isOk());
        $this->assertViewHas('title');
    }

    /**
     * Show
     */
    public function testShow()
    {
        //$this->mock->shouldReceive('find')->with(1)->once()->andReturn(array('id'=>1));

        $this->mock
        ->shouldReceive('find')
        ->once()
        ->andSet('id',1);

        //$this->call('GET','widgets/1');
        $this->action('GET','WidgetController@show',array(
            'widgets' => 1
            ));

        $this->assertTrue($this->client->getResponse()->isOk());
    }

    /**
     * Create
     */
    public function testCreate()
    {
        $crawler = $this->client->request('GET','widgets/create');

        $this->assertTrue($this->client->getResponse()->isOk());
        $this->assertViewHas('title');
        $this->assertCount(1,$crawler->filter('h3:contains("Create a New Widget")'));

    }

    /**
     * Store Success
     */
    public function testStoreSuccess()
    {
        $this->mock
        ->shouldReceive('save')
        ->once()
        ->andSet('id',1);

        $this->call('POST','widgets',array(
            'name' => 'Fu-Widget','description' => 'Widget description'
            ));

        $this->assertRedirectedToRoute('widgets.index');
    }

    /**
     * Store Fail
     */
    public function testStoreFail()
    {

        $this->call('POST',array(
            'name' => '','description' => ''
            ));

        $this->assertRedirectedToRoute('widgets.create');
        $this->assertSessionHasErrors(['name']);

    }
}

testShow方法错误调用未定义的方法Widget :: find()

思考?

解决方法

脚本宝典总结

以上是脚本宝典为你收集整理的php – Mocking Static Eloquent Models方法包括find()全部内容,希望文章能够帮你解决php – Mocking Static Eloquent Models方法包括find()所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。