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
Related
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.
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([]);
});
I struggle to understand how i'm supposed to do a custom filter in easyadmin 3.3 . The documentation isnt helping. I alway got an error
Error: Class App\Entity\entity1 has no field or association named "field1"
So i tried to put the mapping as false to prevent this, following the doc and i'm getting this
Attempted to call an undefined method named "mapped" of class "App\Controller\Admin\Filter\customfilter".
here my code :
Crud controller :
public function configureFilters(Filters $filters): Filters
{
return $filters
->add(getAutoclaveFilter::new('fiedWithNoAssociation')->mapped(false))
;
}
custom filter :
class GetAutoclaveFilter implements FilterInterface
{
use FilterTrait;
public static function new(string $propertyName, $label = null): self
{
return (new self())
->setFilterFqcn(__CLASS__)
->setProperty($propertyName)
->setLabel($label);
}
public function apply(QueryBuilder $queryBuilder, FilterDataDto $filterDataDto, ?FieldDto $fieldDto, EntityDto $entityDto): void
{
$queryBuilder->andWhere(sprintf('%s.%s = :value', $filterDataDto->getEntityAlias(), $filterDataDto->getProperty()))
->setParameter('value',$filterDataDto );
}
And the entity :
public function fiedWithNoAssociation()
{
return $this->getEntityAssociated()->getEntity2();
}
What i'm doing wrong ? Is the mapped function not implemented yet ?
Use instead of mapped:
->setFormTypeOption('mapped', false)
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.
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.