parsa
4 سال پیش توسط parsa مطرح شد
1 پاسخ

تست نویسی در lumen

عرض ادب

دوستان یه مشکل دارم

در تست نویسی لاراول از withoutExceptionHandling استفاده میکنیم حالا جایگزینش در lumen چی هست ؟

@hesammousavi
@ali.bayat
@Alimotreb


ثبت پرسش جدید
علی بیات
تخصص : توسعه دهنده ارشد وب
@ali.bayat 4 سال پیش مطرح شد
1

درود

در لاراول متدهای withExceptionHandling و withoutExceptionHandling توسط تریت "InteractsWithExceptionHandling" مدیریت میشند.

به این صورت که میاد اِکسپشن هندلر فعلی رو کش میکنه و با گزینه ای که بهش پاس داده میشه عوضش میکنه و به این ترتیب خطاهای واقعی رو در یونیت تست به شما نمایش میده.

در لومن همچین تریتی موجود نیست... ساده ترین راه برای اضافه کردن این قابلیت به لومن اینه که یه فایل "InteractsWithExceptionHandling.php" در پوشه tests بسازید و کدهای زیر رو درش قرار بدید:

<?php

namespace Illuminate\Foundation\Testing\Concerns;

use Exception;
use Illuminate\Validation\ValidationException;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Symfony\Component\Console\Application as ConsoleApplication;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

trait InteractsWithExceptionHandling
{
    /**
     * The original exception handler.
     *
     * @var \Illuminate\Contracts\Debug\ExceptionHandler|null
     */
    protected $originalExceptionHandler;

    /**
     * Restore exception handling.
     *
     * @return $this
     */
    protected function withExceptionHandling()
    {
        if ($this->originalExceptionHandler) {
            $this->app->instance(ExceptionHandler::class, $this->originalExceptionHandler);
        }

        return $this;
    }

    /**
     * Only handle the given exceptions via the exception handler.
     *
     * @param  array  $exceptions
     * @return $this
     */
    protected function handleExceptions(array $exceptions)
    {
        return $this->withoutExceptionHandling($exceptions);
    }

    /**
     * Only handle validation exceptions via the exception handler.
     *
     * @return $this
     */
    protected function handleValidationExceptions()
    {
        return $this->handleExceptions([ValidationException::class]);
    }

    /**
     * Disable exception handling for the test.
     *
     * @param  array  $except
     * @return $this
     */
    protected function withoutExceptionHandling(array $except = [])
    {
        if ($this->originalExceptionHandler == null) {
            $this->originalExceptionHandler = app(ExceptionHandler::class);
        }

        $this->app->instance(ExceptionHandler::class, new class($this->originalExceptionHandler, $except) implements ExceptionHandler {
            protected $except;
            protected $originalHandler;

            /**
             * Create a new class instance.
             *
             * @param  \Illuminate\Contracts\Debug\ExceptionHandler  $originalHandler
             * @param  array  $except
             * @return void
             */
            public function __construct($originalHandler, $except = [])
            {
                $this->except = $except;
                $this->originalHandler = $originalHandler;
            }

            /**
             * Report the given exception.
             *
             * @param  \Exception  $e
             * @return void
             */
            public function report(Exception $e)
            {
                //
            }

            /**
             * Determine if the exception should be reported.
             *
             * @param  \Exception  $e
             * @return bool
             */
            public function shouldReport(Exception $e)
            {
                return false;
            }

            /**
             * Render the given exception.
             *
             * @param  \Illuminate\Http\Request  $request
             * @param  \Exception  $e
             * @return mixed
             *
             * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException|\Exception
             */
            public function render($request, Exception $e)
            {
                if ($e instanceof NotFoundHttpException) {
                    throw new NotFoundHttpException(
                        "{$request->method()} {$request->url()}", null, $e->getCode()
                    );
                }

                foreach ($this->except as $class) {
                    if ($e instanceof $class) {
                        return $this->originalHandler->render($request, $e);
                    }
                }

                throw $e;
            }

            /**
             * Render the exception for the console.
             *
             * @param  \Symfony\Component\Console\Output\OutputInterface  $output
             * @param  \Exception  $e
             * @return void
             */
            public function renderForConsole($output, Exception $e)
            {
                (new ConsoleApplication)->renderException($e, $output);
            }
        });

        return $this;
    }
}

سپس داخل تست ها.. هر جا خواستید اِکسپشن هندلینگ رو غیر فعال کنید:

use InteractsWithExceptionHandling;

برای ارسال پاسخ لازم است وارد شده یا ثبت‌نام کنید

ورود یا ثبت‌نام