Category : testing

I have a problem with writing test for pdf generation. I have controller, service and repository, so service is injected in my controller and it’s calling method which is part of repository class. My test always returns error 500, probably because missing dependencies. Does anybody knows how to make test passing? This is the code: ..

Read more

I recently set up a new laravel 8 project and I want to run the tests in memory so I set the testing environment to the following: in phpunit.xml I set db <server name="DB_CONNECTION" value="sqlite"/> <server name="DB_DATABASE" value=":memory:"/> Database connection config/database.php ‘sqlite’ => [ ‘driver’ => ‘sqlite’, ‘url’ => env(‘DATABASE_URL’), ‘database’ => env(‘DB_DATABASE’, database_path(‘database.sqlite’)), ‘prefix’ ..

Read more

I’m using Laravel 8 starting with cloned pingcrm and I’m tryng to write a test to update a contact, but seems it doesn’t work. Note that I made a change to the preset structure, now a contact is directly associated to the user that created it (not to the account) public function test_an_authorized_user_can_update_a_contact() { $this->user->contacts()->saveMany( ..

Read more

i’m new with the tests in Laravel (7.x), i create two feature tests, but the seconds don’t run with php artisan test my UserTest.php <?php namespace TestsFeature; use IlluminateFoundationTestingRefreshDatabase; use IlluminateFoundationTestingWithFaker; use TestsTestCase; class UserTest extends TestCase { /** * A basic feature test example. * @test * @return void */ public function an_authtenticated_user_can_get_own_details() { ..

Read more

I have running project on localhost port 8000, and at first seek it opens home page (‘/’) perfecly but for some reasons <?php namespace TestsFeature; use IlluminateFoundationTestingRefreshDatabase; use TestsTestCase; class ExampleTest extends TestCase { /** * A basic test example. * * @return void */ public function testBasicTest() { $response = $this->get(‘/’); $response->assertStatus(200); } } ..

Read more

api.php Route::middleware([‘trim.strings’, ‘other’])->group(function () { Route::put(‘a’, ‘[email protected]’); }) Kernel.php use AppHttpMiddlewareTrimStrings; protected $routeMiddleware = [ ‘trim.strings’ => TrimStrings::class, ‘other’ => Other::class ] Tests.php $this->withMiddleware() ->putJson(‘/a’, ‘{"a": [{"name" => "hi "}]’) ->assertStatus(200) ->assertJson([ ‘data’ => [ ‘a’ => [ [‘name’ => "hi"], ] ] ]); More Info: Laravel 7 Behavior: In the tests: Only middlewares other ..

Read more

I want to create with factory in the test case but i got a validation error it say The name has already been taken. But how? It always refresh database every time the test start run. The factory $factory->define(Companies::class, function (Faker $faker) { return [ ‘name’ => $faker->unique()->company, ’email’ => $faker->email, ‘website’ => $faker->url, ]; ..

Read more

Hello i would like to test a store method in laravel. In the store method has simple data and image. The faker generate all data, image as well but the validator not accepte the image. Why is this happend? The faker generate correct image name and extension. Here is the test case public function test_an_authenticated_user_can_add_new_companie ..

Read more

does not pass me the last test that compares the field just entered with the one sent because json I have the following fields: public function up() { Schema::create(‘polls’, function (Blueprint $table) { $table->id(); $table->text(‘now’); $table->json(‘paramJson’); $table->enum(‘status’, [‘a’, ‘b’, ‘c’,’d’])->default(‘a’); }); } Poll model: protected $fillable=[ ‘now’, ‘paramJson’, ]; //to cast that column from JSON ..

Read more

I want to test that the Auth facade, when createUserProivder() method is called, returns my user provider. The problem is that with the following code, with the commented out part, the AuthManager is still the original, not the mock. With the uncommented part, I get an error: MockeryExceptionBadMethodCallException : Method Mockery_2_Illuminate_Auth_AuthManager::validate() does not exist on ..

Read more

I am trying to unit test the update function in a resource controller and I keep getting an error Call to undefined method assertStatus(). Here is my code: $this->withoutExceptionHandling(); $this->withoutExceptionHandling(); $timesheet = factory(TimeSheets::class)->create(); $user = factory(UsersLoginModel::class)->create(); $data = [ ‘subject’ => $this->faker->text, ‘subjectmatter’ => $this->faker->text, ‘description’ => $this->faker->text, ]; ; $update = $this->actingAs($user)->json(‘PATCH’,’timesheets/update’.$timesheet->id, $data); $this->assertStatus(200); ..

Read more

I want mock laravel using PESTPHP Testing Framework on cache helper but when i try i got error like this Method Mockery_2_Illuminate_Cache_CacheManager::remember() does not exist on this mock object I want mock rememberForever method but why the remember method get mocked. I also try to change shouldReceive to another method but always remember method to ..

Read more

I’m trying to add the code-coverage report feature for phpunit in Laravel 6 by following the tutorial https://coderwall.com/p/mxtqiw/add-code-coverage-report-to-laravel-project after adding the logging node on the phpunit.php file <?xml version=”1.0″ encoding=”UTF-8″?> <phpunit xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=”./vendor/phpunit/phpunit/phpunit.xsd” backupGlobals=”false” backupStaticAttributes=”false” bootstrap=”vendor/autoload.php” colors=”true” convertErrorsToExceptions=”true” convertNoticesToExceptions=”true” convertWarningsToExceptions=”true” processIsolation=”false” stopOnFailure=”false”> … <logging> <log type=”coverage-html” target=”./report” lowUpperBound=”50″ highLowerBound=”80″ /> </logging> </phpunit> and running the ..

Read more

I user http tests to check the availability status of my pages. But I noticed that in tests one of my policy works differently. The controller method: public function show(Competition $competition) { $competition->load(‘registeredTeams.user’, ‘teams.user’); return view(‘competitions.show’, compact(‘competition’)); } The policy method applied to it: public function view(User $user, Competition $competition) { return $user->id === $competition->user_id; ..

Read more

Lets say I have this job: class ExampleJob implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; public $retryAfter = 300; public function handle() { // code } } I want to make sure that a job will be re-tried after x minutes when it fails. So, if I have the above job, how can I ..

Read more

I have a problem with routing and I’m near of freaking. In my API route file: Route::post(‘/role-user/attach/{user}’, ‘[email protected]’)->name(‘role.user.attach’); This is my Controller: public function attach(User $user) { $user->assignRole($this->roles); return response(”,200); } I making post request to the route by name in my test: (also tried path) public function roles_can_be_attached_to_a_user() { $user = factory(AppUser::class)->create(); $roles = ..

Read more

I have created a feature test to test the admin registration. When i run the testcases it gives me this error 1) TestsFeatureAdminRegisterTest::testValidRegistration The expected [AppEventsUserRegistered] event was not dispatched. Failed asserting that false is true. /var/www/html/gas-backend/tests/Feature/Admin/RegisterTest.php:85 Below i have stated the admin registration test file and user registered event file Admin/RegisterTest.php $response->assertStatus(201); $response->assertJson($expectedResponse); $this->assertDatabaseHas(‘users’, ..

Read more