Laravel 5 Model mocking - php

I have seen a few posts in various places about this and they all seem to have a similar answer. However for the life of me I cannot get the Mockery object working properly.
The Attribute model looks like this
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Attribute extends Model {
public function test()
{
return (new \App\Models\Value())->hello();
}
}
The Value model like this
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Value extends Model
{
public function hello()
{
return 'goodbye';
}
}
The PHPUnit test looks like this
use App\Models\Attribute;
class AttributeModelTest extends TestCase
{
public function testThing()
{
$mock = Mockery::mock('\App\Models\Value');
$mock->shouldReceive('hello')
->once()
->andReturn('hello');
$this->app->instance('\App\Models\Value', $mock);
$a = new \App\Models\Attribute();
$return = $a->test();
var_dump($return);
}
}
PHPUnit outputs 'goodbye', where I though that I am telling it to return 'hello' in the mock and it doesn't. Any ideas what I might be doing wrong?

As discussed in comments:
Change return (new \App\Models\Value())->hello(); with return (\App::make('App\Models\Value'))->hello();
And in the test: $a = new \App\Models\Attribute(); with $a = App::make('App\Models\Attribute'); so Laravel will resolve the dependencies through the container

Related

Why I am unable to assert that method has been called on my mocked service?

I have made a simple class named MyService
namespace App\Services;
class MyService
{
private $anotherService;
public function setService(AnotherService $anotherService)
{
$this->anotherService = $anotherService;
}
public function getService()
{
if(empty($this->anotherService)){
$this->setService(new AnotherService());
}
return $this->anotherService;
}
public function call()
{
$anotherService = $this->getService();
$anotherService->SetXY(5,10);
}
}
As you can se via a setter I set as Depedency the AnotherService:
namespace App\Services;
class AnotherService
{
public function SetXY($x,$y)
{
}
}
In order to test whether the MyService runs as expected I made the following test:
namespace Tests\Services;
namespace Tests\Services;
use App\Services\MyService;
use App\Services\AnotherService;
use PHPUnit\Framework\TestCase;
use Mockery;
class MyServiceTest extends TestCase
{
public function testService()
{
$mockedAnotherService = Mockery::spy(AnotherService::class);
$mockedAnotherService->shouldReceive('SetXY');
$service = new MyService();
$service->setService($mockedAnotherService);
$service->call();
$mockedAnotherService->shouldHaveReceived()->setXY(5,10);
}
}
But for some reason seems that I am unable to assert that setXY is called despite the opposite. The error is:
1) Tests\Services\MyServiceTest::testService
Mockery\Exception\InvalidCountException: Method setXY(<Any Arguments>) from Mockery_0_App_Services_AnotherService should be called
at least 1 times but called 0 times.
/var/www/html/api/vendor/mockery/mockery/library/Mockery/CountValidator/AtLeast.php:47
/var/www/html/api/vendor/mockery/mockery/library/Mockery/Expectation.php:310
/var/www/html/api/vendor/mockery/mockery/library/Mockery/ReceivedMethodCalls.php:46
/var/www/html/api/vendor/mockery/mockery/library/Mockery/VerificationDirector.php:36
/var/www/html/api/vendor/mockery/mockery/library/Mockery/HigherOrderMessage.php:46
/var/www/html/api/tests/Services/MyServiceTest.php:23
phpvfscomposer:///var/www/html/api/vendor/phpunit/phpunit/phpunit:60
Do you know why that does happen?
There is a typo in:
$mockedAnotherService->shouldHaveReceived()->setXY(5,10);
Should be:
$mockedAnotherService->shouldHaveReceived()->SetXY(5,10);

Add function to controller in laravel 7

This might not be best practice- however my functions will be rather comprehensive and used by additional controllers, thus I would like to separate them from the controller's public class. (what is best practice? I just want to load the functions to specific pages/controllers.)
Laravel 7
class HomeController extends Controller
{
function add($a, $b) {
print($a + $b);
}
public function show($id) {
add(1,2)
}
}
You shouldn't perform logic in the controller.
I recommend you to create a Calculator folder for example where you store the sum, something like this:
The controller:
declare(strict_types=1);
namespace App\Controller;
use App\Calculator;
final class HomeController extends Controller
{
// $id not needed, although, if you are going to use it,
// try to declare the type!
public function show() {
return (new Calculator())->add(1, 2);
}
}
And the calculation class:
declare(strict_types=1);
namespace App\Calculator;
final class Calculator
{
public function add(float $firstOp, float $secondOp): float
{
return $firstOp + $secondOp;
}
}
PS: Do not print in the controller, you have to follow the SRP (Single Responsibility Principle)! :)
You can replace the return (new Calculator())->add(1, 2); in the show() method by a dd(new Calculator())->add(1, 2)); for example.
Seems like a solution for using a trait a common practice in Laravel.
namespace App\Http\Controllers;
use App\Traits\MySpecialFunctions;
class HomeController extends Controller
{
use MySpecialFunctions;
}
namespace App\Traits;
trait MySpecialFunctions {
function add($a, $b) {
print($a + $b);
}
public function show($id) {
add(1,2)
}
}

How to call a model function inside the controller in Laravel 5

I have been facing a problem of not able to use the model inside the controller in the new laravel framework version 5. i created the model using the artisan command
"php artisan make:model Authentication" and it created the model successfully inside the app folder, after that i have created a small function test in it, and my model code looks like this.
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function test(){
echo "This is a test function";
}
}
Now i have no idea, that how shall i call the function test() of model to my controller , Any help would be appreciated, Thanks in advance.
A quick and dirty way to run that function and see the output would be to edit app\Http\routes.php and add:
use App\Authentication;
Route::get('authentication/test', function(){
$auth = new Authentication();
return $auth->test();
});
Then visit your site and go to this path: /authentication/test
The first argument to Route::get() sets the path and the second argument says what to do when that path is called.
If you wanted to take this further, I would recommend creating a controller and replacing that anonymous function with a reference to a method on the controller. In this case, you would change app\Http\Routes.php by instead adding:
Route::get('authentication/test', 'AuthenticationController#test');
And then use artisan to make a controller called AuthenticationController or create app\Http\Controllers\AuthenticationController.php and edit it like so:
<?php namespace App\Http\Controllers;
use App\Authentication;
class AuthenticationController extends Controller {
public function test()
{
$auth = new Authentication();
return $auth->test();
}
}
Again, you can see the results by going to /authentication/test on your Laravel site.
Use scope before method name
<?php
namespace App\Models;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
class Mainmenu extends Model
{
public function scopeLeftmenu() {
return DB::table('mainmenus')->where(['menu_type'=>'leftmenu', menu_publish'=>1])->orderBy('menu_sort', 'ASC')->get();
}
}
above code i tried to access certain purpose to call databse of left menu
than we can easy call it in Controller
<?php
Mainmenu::Leftmenu();
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function scopeTest(){
echo "This is a test function";
}
}
Just prefix test() with scope. This will become scopeTest().
Now you can call it from anywhere like Authentication::Test().
For me the fix was to set the function as static:
public static function test() {..}
And then call it in the controller directly:
Authentication::test()
You can call your model function in controller like
$value = Authentication::test();
var_dump($value);
simply you can make it static
public static function test(){
....
}
then you can call it like that
Authentication::test();
1) First, make sure your Model is inside a Models Folder
2) Then supposing you have a model called Property inside which you have a method called returnCountries.
public function returnCountries(){
$countries = Property::returnCountries();
}
of course, in your case, replace Property by the name of your Model, and returnCountries by the name if your function, which is Test
and in the Model you write that function requesting the countries
so in your Model, place a:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function test(){
return $test = "This is a test function";
}
}
and this is what your Controller will be getting
You should create an object of the model in your controller function then you can model functions inside your controller as:
In Model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Authentication extends Model {
protected $table="canteens";
public function test(){
return "This is a test function"; // you should return response of model function not echo on function calling.
}
}
In Controller:
namespace App\Http\Controllers;
class TestController extends Controller
{
// this variable is used to store authenticationModel object
protected $authenticationModel;
public function __construct(Request $request)
{
parent::__construct($request);
$this->authenticationModel= new \App\Authentication();
}
public function demo(){
echo $this->authenticationModel->test();
}
}
Output:
This is a test function

Laravel4 init another Model from a Model

How do initiate model from model in Laravel 4. I try the following but without any success
namespace App\Models;
use \Httpful\Request;
use \RecursiveIteratorIterator;
use \RecursiveArrayIterator;
use Illuminate\Support\Facades\DB as DB;
class Parser extends \Eloquent {
public function __construct(AffiApi $affiapi) {
$this->affiapi = $affiapi;
$url = "HTTP://some.com/xml/feed_config.xml";
$xml = Request::get($url)->expectsXml()->send();
$this->xml = $xml->body;
}
public function Test(){
$api = new Api();
//I get error Class 'App\Models\Api' not found
}
}
class Api extends Parser {
}
How di i do it properly or what is the cleanest way to achive this
The neatest and best practiced way to do this is by using a principle called dependency injection (DI). This way it is loosely coupled and it will also be possible to inject stub classes for unit testing.
class Parser extends \Eloquent {
public function __construct(Api $api) {
$this->api = $api;
}
public function Test() {
}
}
Be sure to run php artisan dump-autoload from the root of your project after adding new classes.

Issue with Facade and injected dependency in Laravel 4

I am having an issue getting a Facade to work properly with a dependency injected into the underlying class.
I have a class called 'Listing'. It has one dependency called 'AdvertRepository' which is an interface and a class called EloquentAdvert which implements the interface. The code for these three classes is here:
// PlaneSaleing\Providers\Listing.php
<?php namespace PlaneSaleing\Providers;
use PlaneSaleing\Repositories\Advert\AdvertRepository;
class Listing {
protected $advert;
public function __construct (AdvertRepository $advert_repository) {
$this->advert = $advert_repository;
}
public function test() {
$this->advert->test();
}
public function test2() {
echo "this has worked";
}
}
// PlaneSaleing\Repositories\Advert\AdvertRepository.php
<?php namespace PlaneSaleing\Repositories\Advert;
interface AdvertRepository {
public function test();
}
// PlaneSaleing\Repositories\Advert\EloquentAdvert.php;
<?php namespace PlaneSaleing\Repositories\Advert;
class EloquentAdvert implements AdvertRepository {
public function test() {
echo 'this has worked';
}
}
I have then created a service provider called ListingServiceProvider.php, which has the following code:
// PlaneSaleing/Providers/ListingServiceProvider.php
<?php namespace PlaneSaleing\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\App;
class ListingServiceProvider extends ServiceProvider {
public function register() {
App::bind('PlaneSaleing\Repositories\Advert\AdvertRepository', 'PlaneSaleing\Repositories\Advert\EloquentAdvert');
}
}
I also added this to the ServiceProviders array in app.php
Now, if I inject Listing as a dependency into a controller and call the test method (as shown below) Laravel correctly detects the dependency, instantiates EloquentAdvert via its binding and displays 'this has worked'.
// Controllers/TestController.php
use PlaneSaleing\Providers\Listing;
class TestController extends BaseController {
protected $listing;
public function __construct(Listing $listing) {
$this->listing = $listing;
}
public function test1() {
$this->listing->test();
}
}
Now, I then created a facade for Listing. I added a new facade as follows and added an alias in app.php:
// PlaneSaleing\Providers\ListingFacade.php
<?php namespace PlaneSaleing\Providers;
use Illuminate\Support\Facades\Facade;
class ListingFacade extends Facade {
protected static function getFacadeAccessor() {
return 'Listing';
}
}
I also added the following new lines to ListingServiceProvider.php:
<?php namespace PlaneSaleing\Providers;
use PlaneSaleing\Repositories\Advert\AdvertRepository;
use PlaneSaleing\Repositories\Advert\EloquentAdvert;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\App;
class ListingServiceProvider extends ServiceProvider {
public function register() {
App::bind('PlaneSaleing\Repositories\Advert\AdvertRepository', 'PlaneSaleing\Repositories\Advert\EloquentAdvert');
// New lines...
$this->app['Listing'] = $this->app->share(function() {
return new Listing(new AdvertRepository);
});
}
}
NOW...if I call Listing::test(), I get the following error: Cannot instantiate interface PlaneSaleing\Repositories\Advert\AdvertRepository.
If I call Listing::test2() , I get 'this has worked' so it seems the Facade is working correctly.
It seems that when accessing Listing via its Facade the binding between AdvertRepository and EloquentAdvert doesnt work. I have looked at my code in the ServiceProvider thinking it was the issue, but I cant figure it out.
Both the Facade and binding work when tested individually but not when both are used at the same time.
Any ideas???
OK, So I have figured it out...For those who run into a similar problem...
The offending statement was in ListingServiceProvider.php which read:
$this->app['Listing'] = $this->app->share(function() {
return new Listing(new AdvertRepository);
});
The error is the new AdvertRepository statement. The reason being is that, we are telling php to directly instantiate the interface 'AdvertRepository'. Instead, we need to tell Laravel to instantiate the appropriate implementation of the 'AdvertRepository' interface. To do that, we use App::make('AdvertRepository'). That way, Laravel uses the binding previously declared to instantiate the correct implementation.
If your constructor is not being inject with a class, you must tell Laravel what class will be used when it needs to instantiate a particular interface:
Put this in your filters or bindings file:
App::bind('PlaneSaleing\Repositories\Advert\AdvertRepository', function()
{
return new PlaneSaleing\Repositories\Advert\EloquentAdvert;
});

Categories