Codeception skipps test method even if depend-upon test was passed - skip

I looked into manual here:
http://codeception.com/docs/07-AdvancedUsage
and there is ability to set #depens annotation for method.
class InvoiceStatusCest
{
public function testOne()
{
}
/**
* #depends testOne
*/
public function testTwo()
{
}
}
But for my surprise my testTwo() always skips, even if testOne() if empty or passed...
i see in console
Running InvoiceStatusCest.testOne - Ok
- Skipped

I had issues with making a test depend on another test in another Cest. Using the just name of the test from the other Cest in the #depends annotation worked for me:
class InvoiceCest
{
public function testCreate()
{
}
}
class InvoiceStatusCest
{
/**
* #depends testCreate
*/
public function testChangeInvoiceStatus()
{
}
}

In your version of codeception dependencies are not handles very well, but you can accomplish what you want using this annotation instead:
class InvoiceStatusCest
{
public function testOne()
{
}
/**
* #depends Codeception\TestCase\Cest::testOne
*/
public function testTwo()
{
}
}

Codeception has some seriously finicking annotation
For instance
this
/*
* #depends testOne
*/
will NOT work but this
/**
* #depends testOne
*/
will work
NOTE the single * versus the ** at the beginning.
Just spent 4 hours of my life discovering this...

Related

How to automatically create sqlite test db when executing parallel tests?

When executing tests separately I have added this functions to make sure sqlite db would be created if it's not there:
abstract class TestCase extends BaseTestCase
{
use CreatesApplication, DatabaseMigrations;
public function setUp(): void
{
$this->initializeTestDB();
parent::setUp();
Notification::fake();
if ($this->tenancy) {
$this->initializeTenancy();
}
}
public function initializeTestDB(): void
{
$dbname = env('DB_DATABASE');
$filePath= "./" . $dbname;
if (!file_exists($filePath)) {
touch($filePath);
}
}
But this does not help when using php artisan test --parallel. I have tried to move this method into AppServiceProvider as in the documentation. Also didn't helped:
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
ParallelTesting::setUpProcess(function ($token) {
$this->handleTestDB();
});
Any ideas to handle this properly for both parallel and separate tests ?

Inject services recursively in Laravel

I am using Laravel 5.2.45.
I am using dependency injection for my services.
Now, there is service A that needs service B, so I inject service B into A.
Also, there are methods in service B, that need service A, so I also injected service A into B.
However, this configuration seems to create a problem. This is my implementation:
class AService
{
/**
* #var BService
*/
protected $bService;
public function __construct(BService $bService) {
$this->bService = $bService;
}
}
class BService
{
/**
* #var AService
*/
protected $aService;
public function __construct(AService $aService) {
$this->aService = $aService;
}
}
First I noticed that my routes are not working, so I tried checking them with:
php artisan route:list
And I got:
PHP Fatal error: Allowed memory size of 134217728 bytes exhausted
(tried to allocate 4096 bytes) in
/var/www/tacnet-plus/bootstrap/cache/compiled.php on line 1426
Segmentation fault (core dumped)
It seems that this configuration creates a memory overflow. I suspect that the first service creates the second and the second creates the first indefinitely.
I have had the same issue in the past with Java Spring, and I know you can inject your services there with a setter instead of the constructor, to avoid creating the dependency in the constructor and inject the service on demand through the setter. Is there something similar in Laravel?
It seems that there is nothing out of the box that will inject a service without using the constructor. However, I can change one of both services to inject the other service manually in a getter, instead of the constructor:
class AService
{
/**
* #var BService
*/
protected $bService;
public function __construct(BService $bService) {
$this->bService = $bService;
}
}
use App;
class BService
{
public function __construct() {
}
/**
* #return AService
*/
private function getAService() {
return App::make('Some\Namespace\AService');
}
}

Laravel Unit Test Controller

How can i test my function which has __construct ?
For ex my controller code looks like :
namespace App\Http\Controllers;
use App\Repositories\UserAccountRepository;
class UserController extends ProtectedController
{
protected $userAccountRepository;
public function __construct(userAccountRepository
$userAccountRepository){
parent::__construct();
$this->userAccountRepository = $userAccountRepository;
}
public function FunctionWantToTest(){
return 'string';
}
The unit test required to fulfill the construct first before test my FunctionWantToTest.
So, my test code looks like,
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use App\Http\Controllers\UserController;
class UserTest extends TestCase
{
/**
* A basic test example.
*
* #return void
*/
public function testUserList()
{
$UserController = new UserController(???what value??);
$this->assertTrue(true);
}
}
Need more help (best practice) how to test the code which has construct.
I think you just need to use app->make:
class UserTest extends TestCase
{
protected $userController;
public function setUp() {
parent::setUp();
$this->userController = $this->app->make(UserController::class);
}
/**
* A basic test example.
*
* #return void
*/
public function testUserList()
{
$this->userController // do what you need here...
$this->assertTrue(true);
}
}
Use feature test or integration test when test controller,
You can see this brief explanation about unit test and integration test.
What's the difference between unit tests and integration tests?

Javadoc for enum field method

I have an enum which implements an interface. The method is implemented in its constants with a comment for each method:
public interface MyIF{
int foo1();
}
public enum SomeTypes implements MyIF{
TYPE_ONE {
/**
* this method ...
*
* #return ...
*/
#Override
public int foo1() {...}
}, ...
}
Javadoc ignores the method comment. Everything else is generated fine. Is there a way I can make these comments appear in the generated javadoc ?
Thanks

override return type in PHPDoc

I have a class Abc with method (body is not important):
/**
* #return SomeBaseClass
*/
function getAll() { ... }
In child class of Abc called AbcChild I'd like to redefine only type of returning class to see it properly in Netbeans. Can I do it without redefining method:
/**
* #return SomeClass
*/
function getAll() { return parent::getAll(); }
Try something like this:
/**
* #method SomeClass getAll()
*/
class AbcChild
{
// ....
}
More info about #method
No, because you need the child method code itself in order to have a child docblock to associate with it. If you have the docblock but not the method code, the docblock won't be tied to anything, and thus will have no effect. Most people dislike altering their code to accommodate docblock behavior, though it's never really bothered me to do so.
However, another option for you is to adjust the #return tag on the parent method, so that it lists all possible return types that you want to indicate the children could return. That makes me wonder, though... if you are not actually overriding the method itself, then how is the child class actually returning a different class than the parent? I can see ways to do this in my mind, involving class properties that contain the differing class objects, but they'd feel like code smells to me ;-)
If there is no method override code itself in the child, then I would choose to put all possible return types in the parent's #return.
Actually I think there is other way than full method override. You can change #return phpdoc block in the child interface which extends base interface. Let me explain with code what I mean:
interface EntityRepository
{
/**
* #return object
*/
public function get($id);
public function add($entity, $sync = false);
public function remove($entity, $sync = false);
// other methods common for custom repositories
}
interface ProjectRepository extends EntityRepository
{
/**
* #return Project
*/
public function get($id);
}
This is part of your domain. And now the concrete implementation taken from Symfony & Doctrine:
use Doctrine\ORM\EntityRepository;
use Model\Repository\EntityRepository as BaseEntityRepository;
abstract class DoctrineEntityRepository extends EntityRepository implements BaseEntityRepository
{
public function get($id)
{
$entity = $this->find($id);
if (!$entity) {
throw new EntityNotFoundException();
}
return $entity;
}
public function add($entity, $sync = false)
{
// method implementation
}
public function remove($entity, $sync = false)
{
// method implementation
}
}
use Model\Repository\ProjectRepository as BaseProjectRepository;
class ProjectRepository extends DoctrineEntityRepository implements BaseProjectRepository
{
public function specificQueryForProjects()
{
// method implementation
}
}
This way you dont have to override methods in child classes only because of code autocomplete. You just have to extend interfaces to let users of your API know that the return value changed.

Resources