diferences between behat documentation and real live. What it's wrong? - behat

When I run $ vendor/bin/behat --init
I get this:
<?php
use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
/**
* Defines application features from the specific context.
*/
class FeatureContext implements Context
{
/**
* Initializes context.
*
* Every scenario gets its own context instance.
* You can also pass arbitrary arguments to the
* context constructor through behat.yml.
*/
public function __construct()
{
}
}
But the documentation show this:
<?php
// features/bootstrap/FeatureContext.php
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
class FeatureContext implements SnippetAcceptingContext
{
/**
* Initializes context.
*/
public function __construct()
{
}
}
I'm not sure if it's because I don't a behat.yml file or it's other problem.
And it give me all the time this error:
`FeatureContext` context class not found and can not be used.
Thanks
Oskar

Related

Cannot extend generic interface in Groovy

I am trying to implement a spring interface called the ObjectPostProcessor interface in groovy to create a new class.
I have tried to do it using a new class implementing the interface and also by creating a new anonymous class. I keep seeing an error saying.
Groovyc: Can't have an abstract method in a non-abstract class.
Which makes me think that groovy doesn't consider the abstract method of the class as overriden correctly.
Below is the code example that is breaking.
What is the correct way to implement this kind of an interface in groovy ?
interface ObjectPostProcessor<T> {
/**
* Initialize the object possibly returning a modified instance that should be used
* instead.
*
* #param object the object to initialize
* #return the initialized version of the object
*/
public <O extends T> O postProcess(O object);
}
// groovy error Error:(15, 1) Groovyc: Can't have an abstract method in a non-abstract class. The class 'ObjectPostProcessorImpl' must be declared abstract or the method 'java.lang.Object postProcess(java.lang.Object)' must be implemented.
class ObjectPostProcessorImpl implements ObjectPostProcessor<Integer> {
#Override
public <O extends Integer> O postProcess(O object) {
return object
}
}
class Anon {
public static void main(String[] args) {
ObjectPostProcessor<Integer> objectPostProcessor = new ObjectPostProcessor<Integer>() {
/** Groovyc: Can't have an abstract method in a
* non-abstract class.
* The class 'ObjectPostProcessorImpl' must
* be declared abstract or the method
* 'java.lang.Object postProcess(java.lang.Object)'
* must be implemented. */
#Override
public <O extends Integer> O postProcess(O object) {
return object;
}
};
ObjectPostProcessorImpl objectPostProcessorImplObj = new ObjectPostProcessorImpl();
System.out.println(objectPostProcessor.postProcess(12));
System.out.println(objectPostProcessorImplObj.postProcess(12));
}
}

Register an asset in Yii2 for all views in a module?

I have a module in Yii2 containing a lot of controllers, models and views.
How can I register an asset for all views, without register it in all view one by one?
The module has init() method, you can use it for code that needs to be executed every time the module is accessed:
<?php
namespace frontend\modules\users;
use frontend\assets\UsersAsset;
use Yii;
use yii\base\Module as BaseModule;
class Module extends BaseModule
{
/**
* #inheritdoc
*/
public $controllerNamespace = 'frontend\modules\users\controllers';
/**
* #inheritdoc
*/
public function init()
{
UsersAsset::register(Yii::$app->view);
parent::init();
}
}
Don't forget to call parent implementation.

Laravel 5: How to add Auth::user()->id through the constructor ?

I can get the ID of the authenticated user like this:
Auth::user()->id = $id;
Great it works, ... but I have a load of methods which need it and I want a cleaner way of adding it to the class as a whole,so I can just reference the $id in each method. I was thinking of putting it into the constructor, but as Auth::user is a static, I am making a mess of things and don't know how to do it.
Many thanks for your help !
Laravel >= 5.3
you can't access the session or authenticated user in your controller's constructor because the middleware has not run yet.
As an alternative, you may define a Closure based middleware directly in your controller's constructor. Before using this feature, make sure that your application is running Laravel 5.3.4 or above:
class UserController extends Controller {
protected $userId;
public function __construct() {
$this->middleware(function (Request $request, $next) {
if (!\Auth::check()) {
return redirect('/login');
}
$this->userId = \Auth::id(); // you can access user id here
return $next($request);
});
}
}
Instead of using the Facade you can inject the contract for the authentication class and then set the user ID on your controller. Like #rotvulpix showed you could put this on your base controller so that all child controllers have access to the user ID too.
<?php
namespace App\Http\Controllers;
use Illuminate\Contracts\Auth\Guard;
class FooController extends Controller
{
/**
* The authenticated user ID.
*
* #var int
*/
protected $userId;
/**
* Construct the controller.
*
* #param \Illuminate\Contracts\Auth\Guard $auth
* #return void
*/
public function __construct(Guard $auth)
{
$this->userId = $auth->id();
}
}
The guard has an id() method which returns void if no user is logged in, which is a little easier than having to go through user()->id.
You can use Auth::user() in the whole application. It doesn't matter where you are. But, in response to your question, you can use the 'Controller' class present in your controllers folder. Add a constructor there and make the reference to the user ID.
<?php namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesCommands;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
/* Don't forget to add the reference to Auth */
use Auth;
abstract class Controller extends BaseController {
use DispatchesCommands, ValidatesRequests;
function __construct() {
$this->userID = Auth::user()?Auth::user()->id:null;
}
}
Then, in any method of any controller you can use the $this->userID variable.

When I use a function that I append to the class Controller, it does not work in Yii

I appended a xxx function to the class Controller, then I touched a file named 'VideoController'. It's extends Controller.
When I execute the VideoController, the xxx function can't be called, why?
the function ajaxReturn :
class Controller extends CController
{
/**
* #var string the default layout for the controller view. Defaults to '//layouts/column1',
* meaning using a single column layout. See 'protected/views/layouts/column1.php'.
*/
public $layout='//layouts/column1';
/**
* #var array context menu items. This property will be assigned to {#link CMenu::items}.
*/
public $menu=array();
/**
* #var array the breadcrumbs of the current page. The value of this property will
* be assigned to {#link CBreadcrumbs::links}. Please refer to {#link CBreadcrumbs::links}
* for more details on how to specify this property.
*/
public $breadcrumbs=array();
/**
* zhoumengkang
* 从Thinkphp里拖过来的
*/
protected function ajaxReturn($data,$info='',$status=1,$type='JSON') {
$result = array();
$result['status'] = $status;
$result['info'] = $info;
$result['data'] = $data;
if(strtoupper($type)=='JSON') {
header("Content-Type:text/html; charset=utf-8");
exit(json_encode($result));
}elseif(strtoupper($type)=='XML'){
header("Content-Type:text/xml; charset=utf-8");
exit(xml_encode($result));
}elseif(strtoupper($type)=='EVAL'){
header("Content-Type:text/html; charset=utf-8");
exit($data);
}else{
// TODO
}
}
}
but it can't by called in
class VideoController extends Controller {
public function actionTest() {
$this->ajaxReturn(true,'test',1);
}
}
You have to import your controller before extend.
Yii::import('application.controllers.Controller');
class VideoController extends Controller {
public function actionTest() {
$this->ajaxReturn(true,'test',1);
}
}
Better change your controller name from Controller to someothername.

which is the correct PHPdoc for methods of objects which are properties in phpstorm?

I'm new to PHPStorm and I imported an existing project in this IDE. Now I receive many warnings like
Method 'query' not found in class
I read about using PHPDoc-blocks in order to declare the origin of variables which are not defined in the current class, but I cannot get out how I should do it for this situation:
class loginModel extends Model{
public function checkLogin(){
[...]
if($this->db->query($sql)){[...]} //Warning as stated above
[...]
}
}
$this->db itself is inheritated from class Model:
class Model{
protected $db;
private function connect(){
$this->db = new PGSQL();
}
}
and therefore can access the public PGSQL method named query.Maybe not that well designed, but how could I solve these messages without downgrading their severity?
class Model{
/**
* #var PGSQL
*/
protected $db;
private function connect(){
$this->db = new PGSQL();
}
}
Docblocks work on properties too