Last active
January 3, 2022 01:29
-
-
Save kmuenkel/b261942f9aaa2f3dcc84aea6bf13a3d7 to your computer and use it in GitHub Desktop.
Testbench bug workaround
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Tests; | |
| use Throwable; | |
| use PHPUnit\Util\Test; | |
| use BadMethodCallException; | |
| use Illuminate\Http\Request; | |
| use Faker\Generator as Faker; | |
| use Illuminate\Routing\Router; | |
| use Illuminate\Config\Repository; | |
| use Illuminate\Encryption\Encrypter; | |
| use App\Providers\AppServiceProvider; | |
| use Illuminate\Foundation\Application; | |
| use Orchestra\Testbench\Exceptions\Handler; | |
| use Laravel\Passport\PassportServiceProvider; | |
| use Illuminate\Validation\ValidationException; | |
| use Illuminate\Contracts\Debug\ExceptionHandler; | |
| use Orchestra\Testbench\TestCase as BaseTestCase; | |
| use Illuminate\Contracts\Routing\{BindingRegistrar, Registrar}; | |
| /** | |
| * Class TestCase | |
| * @package Tests | |
| */ | |
| class TestCase extends BaseTestCase | |
| { | |
| use ResponseHelpers, AnonymousResponse, TokenHelpers; | |
| /** | |
| * @var Faker | |
| */ | |
| protected $faker; | |
| /** | |
| * @inheritDoc | |
| */ | |
| protected function setUp(): void | |
| { | |
| parent::setUp(); | |
| // $this->artisan('config:clear'); | |
| // $this->artisan('vendor:publish', ['--all' => true, '--force' => true]); | |
| static::generateAppKey($this->app); | |
| $this->faker = app(Faker::class); | |
| } | |
| protected function setUpTraits() | |
| { | |
| $this->artisan('optimize:clear'); | |
| return parent::setUpTraits(); | |
| } | |
| /** | |
| * @param Application $app | |
| * @return string | |
| */ | |
| public static function generateAppKey(Application $app): string | |
| { | |
| /** @var Repository $config singleton */ | |
| $config = $app->make('config'); | |
| if ($key = !$config->get('app.key')) { | |
| $key = 'base64:' . base64_encode(Encrypter::generateKey($config->get('app.cipher'))); | |
| $config->set('app.key', $key); | |
| } | |
| return $key; | |
| } | |
| /** | |
| * @param Application $app | |
| */ | |
| protected function resolveApplicationExceptionHandler($app) | |
| { | |
| $app->singleton(ExceptionHandler::class, function (...$args) { | |
| return new class(...$args) extends Handler | |
| { | |
| /** | |
| * @param Request $request | |
| * @param Throwable $e | |
| * @return mixed | |
| */ | |
| public function render($request, Throwable $e) | |
| { | |
| $request->headers->set('Accept', 'application/json'); | |
| $debug = $e instanceof ValidationException ? [ | |
| 'errors' => $e->errors(), | |
| 'data' => $e->validator->getData() | |
| ] : []; | |
| return parent::render($request, $e)->setContent(json_encode([ | |
| 'type' => get_class($e), | |
| 'code' => $e->getCode(), | |
| 'message' => $e->getMessage(), | |
| 'debug' => $debug, | |
| 'trace' => $e->getTrace() | |
| ])); | |
| } | |
| }; | |
| }); | |
| } | |
| /** | |
| * This only seems to be an issue when run in GitLab CI Jobs | |
| * @inheritDoc | |
| * @link https://github.com/orchestral/testbench/issues/132#issuecomment-252438072 IMS Global Documentation | |
| */ | |
| protected function getPackageAliases($app) | |
| { | |
| return [ | |
| 'routes' => [Router::class, Registrar::class, BindingRegistrar::class] | |
| ]; | |
| } | |
| /** | |
| * @inheritDoc | |
| */ | |
| protected function getPackageProviders($app) | |
| { | |
| return [PassportServiceProvider::class, AppServiceProvider::class]; | |
| } | |
| /** | |
| * @return array | |
| */ | |
| public function patchGetAnnotations() : array | |
| { | |
| return Test::parseTestMethodAnnotations(static::class, $this->getName()); | |
| } | |
| /** | |
| * Address compatibility issues between Orchestra\Testbench and PHPUnit\Framework. | |
| * @param string $name | |
| * @param array $arguments | |
| * @return array | |
| */ | |
| public function __call(string $name, array $arguments = []) | |
| { | |
| if ($name == 'getAnnotations') { | |
| return $this->patchGetAnnotations(); | |
| } | |
| throw new BadMethodCallException("Undefined method ' $name'."); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment