Category : phpunit

We are sending a slack message to our team channel hence not using notifiable instance. This is how I did it- Notification::route(‘slack’, env(‘SLACK_URL’)) ->notify(new StaffNotification()); And in StaffNotification public function toSlack() { return (new SlackMessage)->content(‘New Staff Message.’); } How should I test StaffNotification as all the assert available are accepting first parameter as notifiable? Source: ..

Read more

I am getting below error: Error: Call to a member function get() on null Here is my code $params = [ ‘auth’ => [ ‘id’ => ‘123’, ‘user_id’ => ‘123’ ], ]; $mockObj = Mockery::mock(TestClass::class)->makePartial(); $mockObj->expects($this->once())->method(‘TestMethod’)->andReturn(true); $requestMock = Mockery::mock(Request::class)->makePartial(); $requestMock->shouldReceive(‘path’)->withAnyArgs()->once()->andReturn(‘/api/test/’ . $id); $requestMock->shouldReceive(‘get’)->withAnyArgs()->once()->andReturn($params); app()->instance(‘request’, $requestMock->getMock()); $request = request(); $request->setRouteResolver(function () use ($request) { return (new ..

Read more

I am getting below error: Error: Call to a member function get() on null Here is my code: $params = [ ‘auth’ => [ ‘id’ => ‘123’, ‘user_id’ => ‘123’ ], ]; $mockObj = Mockery::mock(TestClass::class)->makePartial(); $mockObj->expects($this->once()) ->method(‘TestMethod’) ->andReturn(true); $requestMock = Mockery::mock(Request::class)->makePartial(); $requestMock->shouldReceive(‘path’) ->withAnyArgs() ->once() ->andReturn(‘/api/test/’ . $id); $requestMock->shouldReceive(‘get’) ->withAnyArgs() ->once() ->andReturn($params); app()->instance(‘request’, $requestMock->getMock()); $request = ..

Read more

I use Laravel 8 with PHPUnit 9.3.3 Now I has wroten in CreatesApp.php this code: <?php namespace Tests; use AppModelsUser; use IlluminateContractsConsoleKernel; use IlluminateSupportFacadesArtisan; trait CreatesApplication { /** * Creates the application. * * @return IlluminateFoundationApplication */ public function createApplication() { $app = require __DIR__.’/../bootstrap/app.php’; $app->make(Kernel::class)->bootstrap(); Artisan::call(‘migrate:reset’); Artisan::call(‘migrate’); AppModelsUser::factory(10)->create(); AppModelsPost::factory(10)->create(); return $app; } } And ..

Read more

I try to run the tests on newly installed Lumen Framework and get this error "Schema for PHPUnit 9.5 is not available". My composer.json for the project: { "name": "api/service", "description": "API Service.", "keywords": ["framework", "laravel", "lumen"], "license": "MIT", "type": "project", "require": { "php": "^7.4|^8.0", "flipbox/lumen-generator": "^8.2", "intervention/image": "^2.6", "laravel/lumen-framework": "^8.0" }, "require-dev": { "fakerphp/faker": ..

Read more

I’m upgrading from Laravel 6 to 7 and when I run phpunit I get this error in some tests InvalidArgumentException: json_decode error: Syntax error But if in the ReindexJob.php file I change the function public function handle(): void { $this->channel->items()->searchable(); } to public function handle(): void { $this->channel->items(); } I no longer have the error, ..

Read more

This is the unit test which I want to run. This is always failing. I have tried with wrong data. Still it fails and with correct data still it fails. Please someone help me out from this. If anyone wants to explore more: http://github.com/PawanRoy1997/forum.git Php Unit Test: <?php namespace TestsFeature; use AppModelsUser; use IlluminateFoundationTestingRefreshDatabase; use ..

Read more

when I use PHPUnit in laravel and add a factory to my own test class after running PHPUnit appear this error in the console: SQLSTATE[HY000]: General error: 1 no such table: users my test class method: public function testExistSomeTextsInIndexPage() { $users= factory(User::class)->create(); $this->get(‘/’)->assertSee($users->name); } this factory code work correctly in other parts of my project ..

Read more

Laravel has several built-in assertions that allow you to query the Response from a Request. public function testWebpage() { $response = $this->get(‘/important-page’) ->assertStatus(200) ->assertViewIs(‘page’) ->assertSee(‘This is a page’) ->assertSee(‘login’); } This is great, except when assertSee fails, it dumps a standard PHPUnit error–something like… 1) TestsPageTest::testWebpage Failed asserting that ‘<!DOCTYPE html>n <!–[if IE 8]><html class="ie8" ..

Read more

(crosspost from https://laracasts.com/discuss/channels/testing/phpunitcypress-testing-2-databases ) Hi all, so I’m trying to set up testing for my project part-way through (cause I was silly and didn’t write tests from the start) I’ll give a quick summary of the project so there is some context… A community runs a game server, which uses a database for in-game persistence, ..

Read more

I’m trying to mock a method in the Model so that I am able to test the controller api endpoint with different scenarios. I am currently using Laravel 8 with PhpUnit and Mockery package. In the route I am using Route model binding. api.php Route::get(‘/api/{project}’, [ProjectController::class, ‘show’]); ProjectController.php class ProjectController extends Controller { public function ..

Read more