Testing a controller that extend Controller class - php

I'm tries instance a class that extends Controller class
This is my Controller:
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class SalaryController extends Controller
{
public function calculateTotalSalary($id)
{
return $this->render('resources/view.html.twig');
}
}
and this is where I want to call the controller
<?php
namespace AppBundle\Tests\Units\Controller\Controller;
use AppBundle\Controller\SalaryController;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class SalaryControllerTest extends KernelTestCase
{
public function testExample()
{
$controller = new SalaryController;
$controller->calculateTotalSalary(1);
}
}
When executed the code give me this:
Starting test 'AppBundle\Tests\Units\Controller\Controller\SalaryControllerTest::testExample'.
PHP Fatal error: Call to a member function get() on null \vendor
symfony\src\Symfony\Bundle\FrameworkBundle\Controller\Controller.php on line 176
Line 176 in Controller class:
return $this->container->get('templating')->renderResponse($view, $parameters, $response);
the $container variable is null, Why ?

Related

Class 'App\TestService' not found - Laravel

Problem
I created service TestService, that I use in colntroller file TestController in function test().
When I called test(), I got an error:
local.ERROR: Class 'App\TestService' not found {"userId":1,"exception":"[object] (Error(code: 0): Class 'App\TestService' not found at /Backend/app/Http/Controllers/TestController.php:8)
Code
TestController.php:
<?php
namespace App\Http\Controllers;
use App\TestService;
class TestController extends Controller
{
public function test()
{
return response()->json(TestService::getTest());
}
}
TestService.php:
<?php
namespace App;
use App\TestService;
class TestService
{
public static function getTest()
{
return "test";
}
}
What I tried
I checked all the names and they are correct.
When I wrote in the colntroller file use App\TestService;
I had autocomplete, so the service and name are visible.
I used these commands to refresh the files: php artisan serve and php artisan clear-compiled.
But it still doesn't work.
You have not correctly defined Namespace.
The namespace must be a directory path where you have created a file.
TestService.php:
<?php
namespace App\Services\Tests;
class TestService
{
public static function getTest()
{
return "test";
}
}
TestController.php:
<?php
namespace App\Http\Controllers;
use App\Services\Tests\TestService;
class TestController extends Controller
{
public function test()
{
//Call service class like.
return response()->json(TestService::getTest());
}
}

ReflectionException Container.php on line 826

I have two classes in the services layer
CashTransactionServices.php
namespace App\Services\Cash;
use App\Models\Cash\CashTransaction;
use App\Models\Cash\CashBoxTransaction;
use DB;
class CashTransactionServices {
protected $AccountingAPI;
public function __construct(AccountingAPI $acc) {
$this->AccountingAPI = $acc;
}
CashBoxTransactionServices.php
namespace App\Services\Cash;
use App\Models\Cash\CashBoxTransaction;
class CashBoxTransactionServices extends CashTransactionServices {}
and in the controller
namespace App\Http\Controllers\Cash;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\Cash\CashBoxTransactionServices;
class CashBoxTransactionController extends Controller {
//
private $services;
public function __construct(CashBoxTransactionServices $cash_box_transaction_services) {
$this->services = $cash_box_transaction_services;
}
I have this error
ReflectionException: Class App\Services\Cash\CashBoxTransactionServices does not exist in file C:\xampp\htdocs\easyerp\vendor\laravel\framework\src\Illuminate\Container\Container.php on line 826
I made sure of using the right namespaces and the right classes names!
PS: when I remove the inheretance from CashBoxTransactionServices class It works!

PHP/Laravel parent class variable is null when calling from different controllers

Parent Class:
<?php
namespace App\Services;
class RequestVariables {
protected static $keys_tour;
public static function init() {
self::$keys_tour = array_flip(['tour_type', 'city_from']);
}
}
Child Class:
<?php
namespace App\Services;
class PreviousVersions extends RequestVariables {
public static function createVersion ($tour) {
dd(parent::$keys_tour);
}
}
When I call PreviousVersions::createVersion() from 1st controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Services\PreviousVersions;
use App\Tour;
class Tours2Controller extends Controller
{
public static function PreProcess($tour)
{
PreviousVersions::createVersion($tour);
}
}
it outputs what's expected:
array:2 [
"tour_type" => 0
"city_from" => 1 ]
but when I execute the same function in another controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Tour;
use App\Services\PreviousVersions;
class BookingController extends Controller {
public function booking($tour)
{
PreviousVersions::createVersion($tour);
}
}
it outputs 'null'
I can't see what's different between my controllers causing different results when calling the same method. Can somebody tell me why it outputs 'null' in the 2nd case?
If you need more information, please ask.
The $keys_tour property is being set inside the init() method of the RequestVariables class.
You can solve it by calling RequestVariables::init() inside the createVersions() method:
public static function createVersion ($tour)
{
RequestVariables::init();
}
Or using the parent keyword:
public static function createVersion ($tour)
{
parent::init();
}

Laravel interface dependency inject

I have a interface dependency inject Question,
the error message look like can`t catch the class,
have any ideal?
Error Message:
FatalThrowableError in UserController.php line 27:
Class 'App\Http\Controllers\App' not found
My folder path :
UserController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Domain\Services\Social\SocialService;
use Domain\Services\Social\SocialInterface;
use Domain\Services\Social\Facebook;
class UserController extends Controller
{
public function fb(Request $request)
{
App::bind(SocialInterface::class, Facebook::class);
$target = App::make(SocialService::class);
return $target ->getCallbackData($request);
}
}
SocialService.php
namespace Domain\Services\Social;
class SocialService
{
public function getCallbackData(SocialInterface $social,$request)
{
return $social->getCallbackData($request);
}
}
Facebook.php
namespace Domain\Services\Social;
class Facebook implements SocialInterface
{
public function getCallbackData($request)
{
//Somethig inside...
}
}
public function fb(Request $request)
{
\App::bind(SocialInterface::class, Facebook::class);
$target = \App::make(SocialService::class);
return $target ->getCallbackData($request);
}
Try it with backslash.

symfony2 routing issues

I am getting the following error:
The autoloader expected class "Acme\HelloBundle\Controller\HelloController" to be defined in file "/var/www/Symfony/app/../src/Acme/HelloBundle/Controller/HelloController.php". The file was found but the class was not in it, the class name or namespace probably has a typo.
The controller code I have is actually:
namespace Acme\HelloBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
class HelloController
{
public function indexAction($name)
{
return new Response('<html><body>Hello '.$name.'!</body></html>');
}
}
any idea why this is?
<?php namespace Acme\HelloBundle\Controller;
....
Just add the "*LESS_THAN*"?php tag at the beginning. Try if works.
Your controller should extend Symfony\Bundle\FrameworkBundle\Controller\Controller
namespace Acme\HelloBundle\Controller;
use Symfony\Component\HttpFoundation\Response;
use namespace Acme\HelloBundle\Controller;
class HelloController extends Controller
{
public function indexAction($name)
{
return new Response('<html><body>Hello '.$name.'!</body></html>');
}
}

Categories