如何断言嘲弄应该接受

时间:2019-12-22 14:42:03

标签: laravel phpunit mockery

我正在尝试使用Mockery实施中间件单元测试

public function handle() {
            // create user
            $user = factory(User::class)->create();

            // log in as admin user
            $this->be($user, 'api');

            $response = \Mockery::mock('Illuminate\Http\JsonResponse')->shouldReceive('getContent', 'setData')->once()->andReturn(json_encode(['data' => [], 'meta' => []]))->getMock();
            $request = Request::create('/admin/customers', 'GET');

            $middlewareResponse = $this->middleware->handle($request, function () use ($response) {
                return $response;
            });

            print_r($middlewareResponse); exit;
    }

如何获取setData并执行断言?

这是middlewareResponse

[_mockery_receivedMethodCalls:protected] => Mockery\ReceivedMethodCalls Object
        (
            [methodCalls:Mockery\ReceivedMethodCalls:private] => Array
                (
                    [0] => Mockery\MethodCall Object
                        (
                            [method:Mockery\MethodCall:private] => getContent
                            [args:Mockery\MethodCall:private] => Array
                                (
                                )

                        )

                    [1] => Mockery\MethodCall Object
                        (
                            [method:Mockery\MethodCall:private] => setData
                            [args:Mockery\MethodCall:private] => Array
                                (
                                    [0] => Array
                                        (
                                            [data] => Array
                                                (
                                                )

                                            [meta] => Array
                                                (
                                                    [home] => /customer/test/dashboard/consectetur-architecto-nostrum
                                                )

                                        )

                                )

                        )

                )

        )

这是我的主页中间件

class HomePage
{
    /**
     * Handle an incoming request.
     *
     * @param Request $request
     * @param Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next) {
        // get original response
        $response = $next($request);
        $responseData = json_decode($response->getContent(), true);

        $data['meta'] = [
            'home' => null,
        ];

        //print_r($response);exit;
        if($response instanceof JsonResponse) {
            // get banner meta data
            $data['meta'] = $this->getHomePageUrl();
        }

        // merge meta data into original
        $payloadWithMeta = array_merge_recursive($responseData, $data);

        $response->setData($payloadWithMeta);

        return $response;
    }

    /**
     * generate dashboard url for user's first customer of first profile
     *
     * @return array
     */
    private function getHomePageUrl() {
        // first initialise an empty profile
        $profile = null;

        // fetch user's first customer
        $user = Auth::user();
        $customer = $user->customers()->orderBy('title')->first();

        // set profile if customer found
        if($customer) {
            $customer->activateDatabase();

            $profile = Profile::orderBy('title')->first();
        }

        return [
            'home' => $profile ? 'customer/settings' : null
        ];
    }
}

``

0 个答案:

没有答案