How to wrap whole class in transaction in PHPUnit? - php

My idea is to specify in each test class parameters, depends how we want to wrap test in transactions. I use Laravel 5.1.
use DB;
class TestCase
{
protected static $wrapMethodInTransaction = true;
protected static $wrapClassInTransaction = false;
/**
* #beforeClass
*/
public static function isClassWrappedInTransactionBefore()
{
if (!static::$wrapMethodInTransaction && static::$wrapClassInTransaction) {
DB::beginTransaction();
}
}
/**
* #afterClass
*/
public static function isClassWrappedInTransactionAfter()
{
if (!static::$wrapMethodInTransaction && static::$wrapClassInTransaction) {
DB::rollback();
}
}
}
The case is that #beforeClass & #afterClass fires when application is not booted yet/already dead, thus class DB is not accesible. I just want to run those methods once per class, but when application is booted/not killed yet. Are there any solutions for this?
DatabaseTransactions trait does not do the work, because it starts transaction after setUp() and rollbacks it after tearDown(). Laravel's 'magic'. I want the setUp() to be in transaction.

Related

How do I repeat the same test on a feature test in laravel

This is my feature test
class CreateImageTest extends TestCase
{
private static function headers(){
....
}
/**
* #test
*
*/
public function no_api_key_404()
{
......
}
/**
* #test
*
*/
public function not_logged_in_401()
{
......
}
/**
* #test
*
*/
public function empty_body_422()
{
....
}
}
I've always begun the test with middleware tests like what you see above (auth and API key middleware). I'm going to use the same test procedure for endpoints with similar middleware (there are a lot of them). How can I do that without being redundant? I was thinking to make a trait for the recurring testing pattern but I have no idea how to do that.
I think you should be able to achieve what you want by introducing an AbstractTestCase file. This file could contain all of your repeating middleware tests and be extended by the Test classes that need it.
Your abstract class could look something like this:
<?php
abstract class AbstractMiddlewareTestCase extends TestCase
{
protected array $headers = [];
public function no_api_key_404()
{
$this->get('your_api_url', $this->headers)
->seeStatusCode(404);
}
// Add any other shared tests here
}
And then you could extend from your regular test file like so:
<?php
class CreateImageTest extends AbstractMiddlewareTestCase
{
use ApiKeyTrait;
public function create_image() {
$this->get('your_api_url', $this->headers)
->seeStatusCode(200);
}
}
Additionally, you could leverage Traits to cater to multiple types of Middleware. I.e. if your create image endpoint is api key authorised you could have an ApiKeyTrait that could look something like this:
<?php
trait ApiKeyTrait
{
public function setUp(): void
{
parent::setUp();
$this->headers = [
'X-API-KEY' => 'yourKey',
];
}
}

Is it bad design to call a controller method from a job class

I recently wanted to implement queue functionality to my Laravel project, and as of now, it works. However, I'm not sure what the proper design pattern for a solution like this is since I'm new to Laravel.
I have a sync() method inside a ProductController class which is a void method that calls to an API, gets products, and inserts/updates records in a database. Since it takes around 2-5 minutes for the function to execute, I decided to try and implement a job to do it in the background.
I wasn't sure whether to copy the whole method and paste it into the handle() method inside the "SyncProducts" job class or call it from the controller class.
As of now, my job class looks like this.
class SyncProducts implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* #return void
*/
public $timeout = 1800;
public function __construct()
{
//
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
try {
(new \App\Http\Controllers\ProductController)->sync();
} catch (Exception $e) {
} catch (ResourceException $e) {
}
}
}
Inside the ProductController class, I added a new method that dispatches the job and redirects the user.
public function syncRun()
{
SyncProducts::dispatch();
return back();
}
Is this bad design? What is the proper way to implement it?
I consider it as bad design, and instead would use an action class which holds the logic in a re-usable way. The action class then can be called from a controller or from a job.
Here a (fairly basic) overview, for the sole purpose of giving an idea about the concept:
class MyWhateverAction
{
public function __construct($data) {
// whatever you need
}
public function execute()
{
// the logic which you now have in the controller
}
}
class MyWhateverController
{
public function synch($request, MyWhateverAction $action)
{
// do something to set $data
$action->execute($data)
return // whatever you need
}
}
class MyWhateverJob
{
public function handle($data, MyWhateverAction $action)
{
$action->execute($data)
}
}
More detailed infos about it:
a) https://stitcher.io/blog/laravel-queueable-actions
b) https://twitter.com/mmartin_joo/status/1509181862014509065?s=21

Test Dependencies between PHP unit tests in Laravel 5.7

I am working on Laravel 5.7 and i'd like to ask you a question regarding the PHPUnit testing.
I have a test class, let's say ProductControllerTest.php, with two methods testProductSoftDelete() and testProductPermanentlyDelete(). I want to use the annotation #depends in the testProductPermanentlyDelete() in order to soft-delete first a product and then get the product id and proceed to permanently deletion test. The problem here is that the DatabaseTransaction trait runs the transactions in every test (method) execution. I need to start the transaction before all the tests of my ProductControllerTest class and then rollback the transaction at the end of all tests. Do you have any ideas? From what i have searched from the web nothing worked properly.
public function testProductSoftDelete()
{
some code
return $product_id;
}
/**
* #depends testProductSoftDelete
*/
public function testProductPermanentlyDelete($product_id)
{
code to test permanently deletion of the product with id $product_id.
There is a business logic behind that needs to soft delete first a
product before you permanently delete it.
}
Does the following make sense?
namespace Tests\App\Controllers\Product;
use Tests\DatabaseTestCase;
use Tests\TestRequestsTrait;
/**
* #group Coverage
* #group App.Controllers
* #group App.Controllers.Product
*
* Class ProductControllerTest
*
* #package Tests\App\Controllers\Product
*/
class ProductControllerTest extends DatabaseTestCase
{
use TestRequestsTrait;
public function testSoftDelete()
{
$response = $this->doProductSoftDelete('9171448');
$response
->assertStatus(200)
->assertSeeText('Product sof-deleted successfully');
}
public function testUnlink()
{
$this->doProductSoftDelete('9171448');
$response = $this->actingAsSuperAdmin()->delete('/pages/admin/management/product/unlink/9171448');
$response
->assertStatus(200)
->assertSeeText('Product unlinked successfully');
}
}
namespace Tests;
trait TestRequestsTrait
{
/**
* Returns the response
*
* #param $product_id
* #return \Illuminate\Foundation\Testing\TestResponse
*/
protected function doProductSoftDelete($product_id)
{
$response = $this->actingAsSuperAdmin()->delete('/pages/admin/management/product/soft-delete/'.$product_id);
return $response;
}
}
namespace Tests;
use Illuminate\Foundation\Testing\DatabaseTransactions;
abstract class DatabaseTestCase extends TestCase
{
use CreatesApplication;
use DatabaseTransactions;
}
Create a separate function to execute the same behavior twice:
public function testProductSoftDelete()
{
doSoftDelete();
}
public function testProductPermanentlyDelete()
{
doSoftDelete();
doPermanentDelete();
}
Your case isn't a case of test dependency, but what you really want to do is to check if a soft deleted can be permanent deleted (or something like that).
Creating a dependency, in this case, will increase complexity of the test.
It's usually better to mount the test scenario from scratch (data/objects), then execute the logic, and then test if the actual scenario is the expected.

Factory helper not working within PHPUnit setup method

Writing some unit tests, and I want to have an object created before the tests in the class are done. So I set up the setUpBeforeClass() method:
<?php
namespace Tests\Unit;
use Tests\TestCase;
use App\Location;
class UserTests extends TestCase {
const FAKEID = 9999999;
public static function setUpBeforeClass() : void {
parent::setUpBeforeClass();
factory(Location::class)->make(["id" => self::FAKEID])->save();
}
}
But when I try running this, I get this error:
InvalidArgumentException: Unable to locate factory with name [default] [App\Location].
But the factory class is set up properly. In fact, if I move this same line down to one of my test functions it works perfectly.
public function testCreateUser() {
factory(Location::class)->make(["id" => self::FAKEID])->save();
// do other stuff...
}
The only thing that sticks out to me as different about setUpBeforeClass() is that it's a static method, but I don't know why that would prevent the factory class from working.
Laravel does a lot of setting up in the setUp() method in the TestCase class. The setUpBeforeClass() method is called before that, that's why your factory is not loaded yet.
The Laravel's TestCase class setup method (see class):
/**
* Setup the test environment.
*
* #return void
*/
protected function setUp()
{
if (! $this->app) {
$this->refreshApplication();
}
$this->setUpTraits();
foreach ($this->afterApplicationCreatedCallbacks as $callback) {
call_user_func($callback);
}
Facade::clearResolvedInstances();
Model::setEventDispatcher($this->app['events']);
$this->setUpHasRun = true;
}
Change your code to use setUp instead:
protected static function setUp() : void
{
parent::setUp();
factory( Location::class )->make( ["id" => self::FAKEID] )->save();
}

Laravel integration testing jobs

I am trying to run an integration tests for my app. I have those jobs:
StartJob
PrepareJob
PeformJob
StartJob dispatches one or more PrepareJob, every PrepareJob dispatches one PerformJob.
Adding this
$this->expectsJobs(
[
StartJobs::class,
PrepareJob::class,
PerformJob::class
]
);
makes my test fail with error saying
1) JobsTest::testJobs
BadMethodCallException: Method Mockery_0_Illuminate_Contracts_Bus_Dispatcher::dispatchNow() does not exist on this mock object
Removing $this->expectsJobs makes all my tests pass, but I can't assert a given job was run, only whether it modified the DB to a given state.
StartJobs.php
class StartJobs extends Job implements ShouldQueue
{
use InteractsWithQueue;
use DispatchesJobs;
public function handle(Writer $writer)
{
$writer->info("[StartJob] Started");
for($i=0; $i < 5; $i++)
{
$this->dispatch(new PrepareJob());
}
$this->delete();
}
}
PrepareJob.php
class PrepareJob extends Job implements ShouldQueue
{
use InteractsWithQueue;
use DispatchesJobs;
public function handle(Writer $writer)
{
$writer->info("[PrepareJob] Started");
$this->dispatch(new PerformJob());
$this->delete();
}
}
PerformJob.php
class PerformJob extends Job implements ShouldQueue
{
use InteractsWithQueue;
public function handle(Writer $writer)
{
$writer->info("[PerformJob] Started");
$this->delete();
}
}
JobsTest.php
class JobsTest extends TestCase
{
/**
* #var Dispatcher
*/
protected $dispatcher;
protected function setUp()
{
parent::setUp();
$this->dispatcher = $this->app->make(Dispatcher::class);
}
public function testJobs()
{
$this->expectsJobs(
[
StartJobs::class,
PrepareJob::class,
PerformJob::class
]
);
$this->dispatcher->dispatch(new StartJobs());
}
}
I think it has to do something with how I am using a concrete dispatcher, while $this->expectsJob mocks the dispatcher. Might be related to this - https://github.com/laravel/lumen-framework/issues/207. What's the way to solve this?
To me it sounds like there is no dispatchNow()-method. In the Jobs your run dispatch() but the error says dispatchNow() does not exist.
Laravel didn't have the dispatchNow()-method before a certain version (i think Laravel 5.2 ... not sure) but just the dispatch(). Could be that the expectsJobs didn't think about that and fails.
You could try not passing it in one array but use 3 commands:
$this->expectsJobs(StartJobs::class);
$this->expectsJobs(PrepareJob::class);
$this->expectsJobs(PerformJob::class);
Maybe that helps.

Categories