How to run a single test method in simpletest unittest class? - simpletest

This is my Unit Test class
<?
require_once '../simpletest/unit_tester.php';
require_once '../simpletest/reporter.php';
class Academic extends UnitTestCase
{
function setUp()
{
}
function tearDown()
{
}
function testAc1()
{
}
function testAc4()
{
}
function testAc7()
{
}
}
$test = new Academic();
$test->run(new HtmlReporter());
?>
When I run this script all methods viz., testAc1, testAc4, testAc7 etc are run.
Is there a way to execute just a single method ?
Thanks,
Shikhar

After digging through the SimpleTest source a bit, I have found the easiest way is to override the test's getTests() method as follows,
require_once('simpletest/autorun.php');
class Academic extends UnitTestCase
{
# ..
function testAc7()
{
}
function getTests()
{
return array("testAc7");
}
}
Here, simply including autorun.php as per usual, only the tests named in getTests() will be run.

Related

Laravel OBSERVER update process

In SESSObServer
public function updated(SESS $sESS)
{
log::info("data updated");
if ($sESS->wasChanged('is_active')) {
log::info("data updated");
}
}
IN Observer, created(SESS $sESS) function run but update is not run. How can i solve problem ?
I changed my query. Now, this is run.
I didn't use get. I used first()
$sess = SESS::where('sess_id', request('id'))->first();
$sess->save();
Make sure your Observer is registered.
# app/Providers/EventServiceProvider.php
use App\Models\SESS;
use App\Observers\SESSObserver;
class EventServiceProvider extends ServiceProvider
{
public function boot()
{
SESS::observe(SESSObServer::class);
}
}
Make sure you're using the correct classes. The logger's class is Log (capital L)
public function updated(SESS $sESS)
{
Log::info("data updated");
if ($sESS->wasChanged('is_active')) {
Log::info("data updated");
}
}
Alternatively, I think you can still use info().
public function updated(SESS $sESS)
{
info("data updated");
if ($sESS->wasChanged('is_active')) {
info("data updated");
}
}
If you're using apache, make sure the apache user can write to the log file.

Laravel mock multiple dependency

I have a Controller that has a dependency with BillingService, and BillingService has another dependency on UserService.
I need to call the Controller method getPlans and in this call I need to mock two functions:
loadPlans that is inside BillingService
getUsage that is in UserService
This is the full example:
class BillingPlanController
{
public function __construct(private BillingPlanService $billingPlanService)
{
}
public function getPlans()
{
$plans = $this->billingPlanService->getPlans();
//
}
}
class BillingPlanService
{
public function __construct(private UserService $userService)
{
}
public function getPlans()
{
$plans = $this->loadPlans();
$user = auth()->user();
$usage = $this->userService->getUsage(user); // DO SOMETHING, NEED TO MOCK .. HOW ?
}
public function loadPlans()
{
// DO SOMETHING, NEED TO MOCK .. HOW ?
}
}
At the end, in my test i simply call:
getJson(action([BillingPlanController::class, "getPlans"]));
In other tests, I'm able to mock a single Service, but in this scenario, I don't know how to write the mocks.
Sorry if I don't provide any "tries", but I really don't know how I can do that.
UPDATE
I tried to use partialMock and mock, but I get this error (when getUsage is called) - partialMock is used because i just need to mock a single function:
Typed property App\Modules\Billing\Services\BillingPlanService::$userService must not be accessed before initialization
$this->mock(UserService::class, function ($mock) {
$mock->shouldReceive("getUsage")->andReturn([]);
});
$this->partialMock(BillingPlanService::class, function ($mock) {
$mock->shouldReceive("loadPlans")->andReturn([]);
});
getJson(action([BillingPlanController::class, "getPlans"]));
Your exception in your partial mock, is because when you mock the BillingPlanService you do not intilize the userService due to it being a mock. You can simply set it on the mock and i think it should work in your context.
$userServiceMock = $this->mock(UserService::class, function ($mock) {
$mock->shouldReceive("getUsage")->andReturn([]);
});
$this->partialMock(BillingPlanService::class, function ($userServiceMock) use ($userServiceMock) {
$mock->set('userService', $userServiceMock);
$mock->shouldReceive("loadPlans")->andReturn([]);
});

the difference when using the initialization function in php

I'm not understanding how to use the initialization function in the code below. Can you explain it to me?
Class A
{
protected $classB;
public function __construct()
{
$this->classB = new ClassB();
}
}
Class A
{
protected $classB;
public function __construct(ClassB $classB)
{
$this->classB = $classB;
}
}
in the second way you always have to make your object like this.
$clasa = new ClassA($classb);
in the first way you are creating the object inside of the constructor.
the second way is an injection. and could be usefull when for example you need some attributes already seted in the object.

Test with Lumen package

I develop lumen package and I don't know test this.
In my package, I use global method config() and abort() but this methods exist with bootstrap/app.php and I have'nt this file in my package.
I'm thinking redefine this methods with dummies class but I have to write only one test method in test class when I test a method with a changement in the config to can re-call an antoher config dummy class .
It's not practical and I guess there's better.
I can share code if you want.
--- Edit
This is example :
Class CheckAuthorizationTest
public function testCanSeeOtherUserRoles()
{
$this->assertTrue(CheckAuthorization::canSeeOtherUserRoles($user, $user));
}
Class CheckAuthorization
static public function canSeeOtherUserRoles(Model $user_parent, Model $user_child)
{
return self::roleIsParentOfDirectChild($user_parent, $user_child);
}
static public function canShowGroup(array $parent_group, string $child_group)
{
$groupsHelper = new GroupsHelper();
foreach ($parent_group as $group) {
if (in_array($child_group, config('roles.roles'))) {
return true;
}
}
abort(403);
}
Result :
There was 1 error:
1) ::testCanSeeOtherUserRoles
ReflectionException: Class config does not exist

How to call dispatchShell on same shell for another method?

I have a cake shell, say DemoShell. And, from a method 'method1', I want to create another process which executes 'method2' of DemoShell. How can that be done?
class DemoShell extends AppShell {
// some code
private function method1() {
// some code
$success = $this->dispatchShell('Demo', 'method2', $params);
// some more code
}
private function method2() {
// some code to do something and return boolean value
}
}
I tried this, but method2 isn't getting called.

Resources