dongzhimin2231 2019-07-01 14:54
浏览 133

使用PHP 7.2在Laravel 5.8中运行PHPUnit时出错

I've been trying to configure my Laravel and Docker environment with PHPUnit code coverage. I had to install Xdebug which was a pain in the ass with so many different tutorials and most of them outdated or not working. Now that I finally installed Xdebug when I try to run my unit tests with vendor/bin/phpunit I'm getting the output:

Declaration of Illuminate\Foundation\Auth\VerifiesEmails::show(Illuminate\Http\Request $request) should be compatible with App\Http\Controllers\Controller::show($id)"

And my unit tests are not running anymore. Searching around this seems to be an issue updating PHP to 7.2 which is weird because my env was built already in php-7.2.

I've tried running composer update with no success and found nothing about this issue.

PHP 7.2.19 Laravel version 5.8.26 PHPUnit version 7.5.13

my Dockerfile:

FROM php:7.2-alpine as web

RUN apk add --no-cache \
 openrc \
 curl \
 nodejs \
 nodejs-npm \
 npm 

RUN apk add --no-cache $PHPIZE_DEPS \
    && pecl install xdebug-2.6.0beta1

RUN printf "xdebug.remote_enable=1
" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
         "xdebug.coverage_enable=1
" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
         "xdebug.idekey=\"PHPSTORM\"
" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
         "xdebug.remote_port=9000
" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
         "xdebug.remote_host=host.docker.internal" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

RUN docker-php-ext-enable xdebug 

RUN curl -sS https://getcomposer.org/installer | \
 php -- --install-dir=/usr/bin/ --filename=composer

# An IDE key has to be set, but anything works, at least for PhpStorm and VS Code...
ENV XDEBUG_CONFIG="xdebug.idekey=''"

EXPOSE 80

update: my App\Http\Controllers\Controller.php

use App\Services\Service;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    public function index(Request $request)
    {
        return response()->json([
            'message' => __('messages.list'),
            'data' => $this->service->all($request->all())
        ],200);
    }

    public function show($id)
    {
        return response()->json([
            'message' => __('messages.list'),
            'data' => $this->service->find($id)
        ], 200);
    }
}

Update: Problem and Fix

My Controller.php is inherited by my other Controllers(e.g. UserController, CaveController) but it is also inherited by Illuminate\Foundation\Auth\VerifiesEmailsController.php and it also have a method show(). To fix I removed my Resource methods from Controllers.php (e.g. show, index and destroy) and created ResourceController.php inheriting Controller.php with my resource methods. With this i can inherit show() from ResourceController.php in my UserController, CaveController.....

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于smbclient 库的使用
    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画