auth::attempt method not defined - laravel-5.1

Auth seems to have no method called attempt, here is my code, im using codeception unit test with cest:
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Auth;
use Illuminate\Routing\Controller;
class ReportCest
{
protected $valid_email = 'email#gmail.com';
protected $valid_password = 'pass';
public function _before(UnitTester $I)
{
Auth::attempt([
'email' => $this->valid_email,
'password' => $this->valid_password
]);
}
}
In the Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers.php used:
$this->auth->attempt($credentials, $request->has('remember')
i wonder how could i implement it like this

after these lines
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Auth;
use Illuminate\Routing\Controller;
type this
use Auth;
or try this way
\Auth::attempt(.....

Try this, define Auth before the class definition
use Auth;

Related

How to pass a class to a method and create it there

I have such an example of a model:
class BirthdayModel {
List birthdays;
BirthdayModel({
#required this.birthdays,
});
factory BirthdayModel.fromJson(json){
return BirthdayModel(birthdays: json['data']);
}
Map<String, dynamic> toJson() {
return {
'birthdays': birthdays,
};
}
}
I want to transfer multiple models into one method:
exampleMethod(model: BirthdayModel);
and then in this method call the constructors or methods of the passed class
exampleMethod(#required model){
return model.fromJson(data);
}
Is it possible to do this?
Not the way you write it.
You cannot pass a class as argument. Even type arguments only pass types, so you cannot use static members that way.
What you can do is:
T exampleMethod<T>(T createModelFromJson(dynamic json)){
return createModelFromJson(data);
}
and call it as :
var birthday = exampleMethod(BirthdayModel.fromJson);
There is no way to access the fromJson programmatically - it's not an instance method so there is no interface for it. Static methods must be accessed explicitly.
(I'm ignoring dart:mirrors because you probably won't have access to those).

AutoFac Register confusion

Hi I am just been looking at AutoFac and following their getting-started tutorial
http://autofac.readthedocs.org/en/latest/getting-started/index.html
having followed it and understanding how their services work I wanted to try to create a new implementation on the same interface type
builder.RegisterType<TodayWriter>().As<IDateWriter>();
builder.RegisterType<TomorrowWriter>().As<IDateWriter>();
Both implentations contain the same code
public class TomorrowWriter : IDateWriter
{
private IOutput _output;
public TomorrowWriter(IOutput output)
{
this._output = output;
}
public void WriteDate()
{
this._output.Write(DateTime.Today.AddDays(1).ToShortDateString());
}
}
So TodaysWriter is the same apart from the WriteDate method displaying
this._output.Write(DateTime.Today.ToShortDateString());
instead.
So now using the application, how do I determine what implementation to use as both methods are called WriteDate()
using(var scope = Container.BeginLifetimeScope())
{
var writer = scope.Resolve<IDateWriter>();
// Is this using todaysWriter or TomorrowWriter?
writer.WriteDate();
}
Am I using this wrong?
Thanks
To differentiate between different implementations of the same interface look at named and keyed services in the docs.
Alternatively you can roll your own by registering a DateWriterFactory and having a method on that to get a specific IDateWriter implementation. something like:
public class DateWriterFactory
{
IDateWriter GetWriter(string writerName)
{
if (writername=="TodayWriter")
return new TodayWriter();
if (writername=="TomorrowWriter")
return new TomorrowWriter();
}
}
obviously the implementation of the factory could be as complex or as simple as you need. Or you could just have methods to get the fixed writers rather than pass in a string.

Testing HTTP parameter without sending or receiving HTTP requests

I am testing a function using PHPUnit, the function checks if there are any GET POST parameters set.
If Set it returns it otherwise returns null. I have no idea how can i test it as i am not setting any http request. The function is as follows,
public static function getURIParameter($param, $defaultValue=null) {
if(isset($_REQUEST[$param]) && is_array($_REQUEST[$param]))
return $_REQUEST[$param];
else
return isset($_REQUEST[$param]) ? trim($_REQUEST[$param]) : $defaultValue;
}
And this is my test function, with some psuedo kind of code.
public function testGetURIParameter()
{
$_POST['parameter'] = true; // is something like that possible?
$this->assertSame('POST', Core::getURIParameter('parameter'));
}
Can anyone tell me how can i test this function?
First, you should devide you code into logic and transport layer. Then use Dependency Injection pattern to inject transport into logic:
<?php
interface TransportInterface {
public function request($url, $params, $method);
}
class Logic {
public function __construct(TransportInterface $transport) {
$this->transport = $transport;
}
}
You should create 2 implementations of TransportInterface - real one (cUrl) and stub one (with empty method) to use it in test env.
In tests you create mock object for StubTransport, provide expectations and inject it into Logic class.

Injecting authentication information

I have an app which connects to multiple sites with a different username/password pair for each. What I want to do is wire up dependencies so that I can say "whenever you want a FTPConnection, use this connection" and "this connection" depends on whatever the user wants.
So say I have a class like this (pseudo-Google Guice syntax):
public class FTPConnection
{
FTPConnection(#Username String username, #Password String password)...
}
And a class that uses it
public class SomeFTPSiteProcessor
{
SomeFTPSiteProcessor(#Inject FTPConnection)...
}
What I would like to do is have the "currently active" connection be created whenever I want an instance of SomeFTPSiteProcessor.
How would I do this? Would I use a scope? Would I use a provider? Help! Pseudo-code would be most appreciated.
I hope this makes some sense...
Edit: The user makes the choice of which FTP connection to use at runtime and so I need the authentication information to be provided dynamically. The language makes me think of a provider of sorts, but I can't quite wrap my head around how it would be done.
Thanks
This is the Robot Legs problem.
public class SomeFTPSiteProcessor
{
SomeFTPSiteProcessor(#JeffsFtpServer FTPConnection)...
}
public class SomeOtherFTPSiteProcessor
{
SomeFTPSiteProcessor(#FredsFtpServer FTPConnection)...
}
class FtpModule extends PrivateModule {
String username;
String password;
Class<? extends Annotation> annotation;
void configure() {
bind(String.class).annotatedWith(Username.class).with(username);
bind(String.class).annotatedWith(Password.class).with(password);
expose(FTPConnection.class).annotatedWith(annotation);
}
}
Injector injector = Injector.createInjector(
new FtpModule("fred", "password", FredsFtpServer.class),
new FtpModule("jeff", "password", JeffsFtpServer.class));
I think you would need a factory then. Likely with that factory having an instance of the injector.
class ThingieFactory() {
#Inject Injector injector;
SomeFTPSiteProcessor create(params... ) {
return injector.createChild(new Module() { set params; } ).get(SomeFTPSiteProcessor.class);
}
}

How to accept any method call in JMockit?

I have something like the following code:
public void f() {
logger.info("some string");
}
How would I specify in JMockit that any call to logger is allowed? For example, if someone changed the logger call to:
logger.finest("some string");
the test should still pass.
You could create an stubbed-out mock class, as per http://jmockit.googlecode.com/svn/trunk/www/tutorial/StateBasedTesting.html:
#MockClass(realClass = Logger.class, stubs = "", inverse = true)
static class MockLoggerStub { }
Then just call this before running your test:
Mockit.setUpMocks(MockLoggerStub.class);
This will completely stub out the Logger class for your test.

Resources