Laravel 5.6: Can't access class variable inside constructor - php-7

I have a custom class in the App\MyClasses folder. I want to set a variable within the class and only access it from various methods inside the class, so it can be private.
When I create a class instance from within a controller, I get an error "Undefined variable: configuration" in the constructor. Can you tell me what I'm missing here?
I'm using Laravel 5.6 and PHP 7.2. Thanks!
private $configuration;
/**
* Create a new instance of the connector.
* #param $userid The logged in user's ID
* #return void
*/
public function __construct($userid) {
//get the configuration object
$config = User::find($userid)->configuration;
//dd($config); //OK - has correct data
$this->$configuration = $config; //'undefined variable: configuration' error here
}

Related

In my service integration, I found a NullPointerException when instantiating class of another service

I cannot seem to figure out in my service integration why my instantiation of the class of another service returns null even when I can see the class in the jar in the library.
Please see my code here:
public class OAuthFacadeClientImpl implements OAuthFacadeClient {
/**
* oauth facade
*/
private OAuthFacade supergwOauthFacade;
/** LOGGER */
private static final Logger LOGGER = LoggerFactory.getLogger(OAuthFacadeClientImpl.class);
#Override
public AccessTokenRevokeResult revokeToken(String merchantId, String userId,
String scope) {
AccessTokenRevokeRequest revokeRequest = new AccessTokenRevokeRequest();
revokeRequest.setAccessToken(userId);
revokeRequest.setClientId(merchantId);
revokeRequest.setExtRequestId(scope);
LogUtil.info(LOGGER, "revokeRequest value = " + revokeRequest);
AccessTokenRevokeResult revokeResult = supergwOauthFacade
.revokeToken(revokeRequest.getClientId(), revokeRequest.getAccessToken(),
revokeRequest.getExtRequestId());
SALResultChecker.checkAndAssert(revokeResult);
return revokeResult;
}
/**
* Setter method for property <tt>oAuthFacade</tt>.
*
* #param oAuthFacade value to be assigned to property miniAppQueryFacade
*/
public void setOAuthFacade(OAuthFacade oAuthFacade) {
this.supergwOauthFacade = oAuthFacade;
}
}
Breakpoints of my code upon debugging
You basically need to instantiate an object of type OAuthFacade prior to using this method revokeToken(...) and feed that reference using your setter method (e.i., setOAuthFacade(...)) so that supergwOauthFacade will not point to nothing and avoid getting NullPointerException. Good LUCK

How to fix 'ArgumentCountError' while using page object model in behat

I am trying to use page object model in my behat framework . Here is my code snippet.
HomePage.php
use Behat\Behat\Context\Context;
use SensioLabs\Behat\PageObjectExtension\PageObject\Page;
class HomePage extends Page implements Context{
protected $path = '/';
}
FeatureContext.php
use Behat\Behat\Context\Context;
use SensioLabs\Behat\PageObjectExtension\PageObject\Page;
class FeatureContext extends Page implements Context, \Behat\Behat\Context\SnippetAcceptingContext
{
private $homepage;
public function __construct(HomePage $homepage)
{
$this->homepage = $homepage;
}
/**
* #Given /^(?:|I )visited (?:|the )(?P<pageName>.*?)$/
*/
public function iVisitedThe($pageName)
{
if (!isset($this->$pageName)) {
throw new \RuntimeException(sprintf('Unrecognised page: "%s".', $pageName));
}
$this->$pageName->open();
}
}
But while executing the behat tests I am getting the following error ->"ArgumentCountError: Too few arguments to function SensioLabs\Behat\PageObjectExtension\PageObject\Page::__construct(), 0 passed and at least 2 expected"
The setup should be like this:
feature > contexts > page objects
Feature file uses definitions that are defined in the context files and a Context file implements the actual step by calling actions from different page objects.
Your page object should extend just Page.
The Feature context should extend MinkContext and implement Context.
All the other contexts should just implement Context.
Ps: take a look again at the page object extension documentation on Working with page objects topic.

Create new category instance inside template - prestashop 1.7

I know i can use category class methods inside .tpl template files like this:
{assign var='all_categories' value=Category::getCategories()}
But how can i actually initialize Category object inside template? So that __construct function runs.
I ask this because when i try to use some Category class functions, i get this error:
Using $this when not in object context
There is no way to instance a category through a tpl file, some classes have a public static method to do this, eg, like the Db class, this have one called getInstance, unfortunately by default doesn't exist nothing similar in the Category class. You should instance it in a php file and send to Smarty, or modify the class adding an object:
public static $instance = array();
And the method:
public static function getInstance($id_category)
{
if (isset(self::$instance[$id_category])) {
return self::$instance[$id_category];
}
return self::$instance[$id_category] = new Category($id_category);
}
Now you can use in your tpl:
{assign var='category' value=Category::getInstance(3)}

Restler not requiring required properties

I have this for my object class and API class. I'm able to call post without sending a task_list_id or display_order. As long as I just pass title it's calling the method.
class BaseTaskObj
{
/// #var int $task_list_id The SQL ident of the Task List to use for the Task. {#min 1}{#required true}
public $task_list_id;
}
class PostTaskObj extends BaseTaskObj
{
/// #var int $assigned_id The SQL ident of the Person who this task is assigned to {#min 1}{#required false}
public $assigned_id;
}
class MyTaskAPI {
/**
* Creates a new Task associated with an existing task list.
*
* #param PostTaskObj $info The details of the Task object to create. {#required title, display_order}
*
* #status 201
*
* #return int The SQL ident of the newly created Task
*/
function post(PostTaskObj $info) {
}
}
task_list_id and assigned_id currently do not have valid phpdoc comments. They do not have any assigned value as well. This makes them the required parameters for the api call.
But then you have {#required title, display_order} which overwrites the required list with invalid parameters thus making them not required

Mockery not being able to count method calls on a Facade

There's a problem with a simple test which is not passing at all.
I have an action within the controller :
/**
* #Get("/parse")
* #param Dispatcher $dispatcher
* #return string
*/
public function parse(){
$xml_file = public_path()."/dummy.xml";
//File::get($xml_file); Tried this as well
$file = $this->file->get($xml_file);
return $file;
}
And on the test I have a method like :
/**
* A basic functional test example.
*
* #return void
*/
public function testBasicExample(){
File::shouldReceive("get")->once();
$this->call('GET', '/parse');
}
And on Laravel documentation, they say that each Facade can be Mocked directly, without instantiating it, but the test is never passing, I'm getting an exception :
Mockery\Exception\InvalidCountException: Method get() from Mockery_0 should be called exactly 1 times but called 0 times.
PS : I have Laravel 5 and on Test Class I have the tearDown method just in case you're wondering.
Finally found a solution.
Instead of using the File, facade, I did inject the FileSystem depency through the constructor, and Mocked that on unit test, and passed the mocked object into IoC Container, and only that way worked, otherwise on Laravel 5, Mocking Facades is not working, shouldReceive() is not keeping count as we've been told by Laravel Docs.
Kind Regards