Laravel dependency injection does not find the class - php

I'm using PHPUnit to test my application, in this case I'm testing an API call (I'm doing GET, POST, PUT and DELETE through it). index method responds to GET(/api) route, in this method I have a custom Request:
public function index(\Api\User\Requests\IndexRequest $request)
{
// do some stuff...
}
Api\User\Requests\IndexRequest class looks like this:
class Request extends IndexApiRequest
{
// some methods in here
}
When I execute the test via PHPUnit it prompts:
Class Api\User\Requests\IndexRequest does not exist
Checking the trace route it dies in Illuminate\Routing\RouteDependencyResolverTrait. I couldn't figure out how I can interfere in the execution since it seems to happen between PHPUnit and Laravel.
Does anyone have an idea? I'm using Laravel 5.3, PHPUnit 5.6.5 running on Ubuntu 16.04, PHP 7.0 and nginx.
Thank you!

change class Request extends IndexApiRequest to class IndexRequest extends IndexApiRequest

Related

Laravel using package [Requests for PHP]

I am using Laravel 6.5.2 and trying to use the package "Requests for PHP". https://requests.ryanmccue.info/download/
I installed this package using composer. The reason I want to use this packages is that I have all CRUD scripts created (using above package) and would like to reuse the scripts.
I suspect there might be some name conflict since Laravel comes installed with a HTTP request solution called Request, meanwhile the [PHP Request] has the same name.
Question:
Has anyone succesfully installed [Requests for PHP] package and got it to work with Laravel 6.X ?
Other findings:
Whe comment-out the autoloader line, the corresponding page prints "hello" as it should. Leaving this line active indicates in the laravel stack trace that the error is the line that holds the autoloader.
[Error]
Class 'App\Http\Controllers\Requests' not found
[routes/web.php]
Route::get("/requests", "Requests#read");
[app/Http/Controllers]
<?php
namespace App\Http\Controllers;
// use Illuminate\Http\Request; // Standard line, creeating a controller.
require_once '../vendor/rmccue/requests/library/Requests.php';
Requests::register_autoloader();
class Requests extends Controller {
public function read() {
echo "hello";
}
}

Laravel default helper method and facade doesn't work on custom package

I am developing a custom package where I need an access to the config data. I was able to pull data from config through my blade files, however, when I tried to call it from any custom classes I made, it's throwing an error:
Error: Call to undefined function Acme\Package\config()
What's interesting though, is that, when I tried using the facade Illuminate\Support\Facades\Config, it cannot find the class.
Is there any way I could retrieve data from the config (from the package and/or the app)?
<?php
namespace Acme\Package;
class MyClass {
public function test() {
config('app.name');
}
}
UPDATE: It works when running in the browser (package installed in a Laravel project) but fails when running package's test
UPDATE: If this helps, my package can be found here
Call to the config() is from here
And the test case that fails can be found here
<?php
namespace Acme\Package;
class MyClass {
public function test() {
\Illuminate\Support\Facades\Config::get('app.name');
}
}
Try like this, to use the full namespace to the Config Facade. You could also make a use statement under your namespace to inject the facade and then use Config::get('app.name). The reason it is not working is that your package cannot resolve the namespace of that facade as it is outside of the IoC container
You should try this:
<?php
namespace Acme\Package;
use Config;
class MyClass {
public function test() {
Config::get('app.name');
}
}
Updated answer
Please add below line in config/app.php in aliases section
'Config' => Illuminate\Support\Facades\Config::class,
then run below command
php artisan config:cache
php artisan cache:clear

Call to a member function connection() on null during tests

I want to do performant testing, so I thought about using PHPUnit_Framework_TestCase instead of TestCase in my test files. I'm sacrificing http requests testing, but I want at least to use some of the components of Laravel (like its providers and such).
Here's what it looks like :
<?php
class ChatTest extends PHPUnit_Framework_TestCase
{
/** #test */
public function chat_can_be_accepted()
{
$this->chat->setStatus(Chat::CHAT_STATUS_WAIT_MASTER);
$this->chat->accept();
$this->assertEquals(Chat::CHAT_STATUS_WAIT_CONFIRM, $this->chat->getStatus());
}
}
I have an error saying Call to a member function connection() on null. setStatus actually uses Laravel internals in this example (something as simple as a relationship that needs the app).
I already tried switching every providers, but it seems code is not even going through providers. Also tried changing PHP versions, or extensions.
I'm answering my own question for future reminders, and also because I never found this answer anywhere else.
At this point I was using PHPStorm and it doesn't auto load the phpunit.xml file when launching tests by default.
That line in the file launches Laravel (works with Lumen too) along with the tests : bootstrap="bootstrap/app.php".
This is needed to use Laravel internals even without using their test frameworks.
To use this in PHPStorm :
To use this in CLI :
phpunit --configuration phpunit.xml

Testing a Laravel package

So, I'm writing a basic Laravel package and I seem to have stumbled upon yet another problem, this time with testing.
The package in development is currently in a packages folder in the root of the project. I have modified the composer.json file of the package to include the dependencies I need
"require-dev": {
"phpunit/phpunit": "~4.0",
"laravel/laravel": "dev-develop"
}
However , whenever I try running phpunit tests in the package folder (which contains a folder named tests along with a sample test), I get the following error:
PHP Fatal error: Class 'Illuminate\Foundation\Testing\TestCase' not found in /workspace/laravel/packages/sample/http-request/tests/HttpRequestTest.php on line 8
The test file is just the auto-generated stub:
<?php
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class HttpRequestTest extends Illuminate\Foundation\Testing\TestCase
{
/**
* A basic test example.
*
* #return void
*/
public function testExample()
{
$this->assertTrue(true);
}
}
Any idea why this isn't working? The app tests run without a hitch, but the app itself doesn't have dependencies other than what's in the box.
SOLUTION
Managed to make it work independently by extending the PHPUnit_Framework_TestCase:
class HttpRequestTest extends PHPUnit_Framework_TestCase
However , running it like:
vendor/bin/phpunit packages/yourname/package-name/
Works as well, so I picked it as an answer.
This works for me:
class HttpRequestTest extends TestCase
And running test with:
vendor/bin/phpunit packages/yourname/package-name/
(Posted on behalf of the OP as an answer).
Managed to make it work independently by extending the PHPUnit_Framework_TestCase:
class HttpRequestTest extends PHPUnit_Framework_TestCase
However , running it like:
vendor/bin/phpunit packages/yourname/package-name/
Works as well, so I picked it as an answer.
For Windows environments you need to use backslashes!
vendor\bin\phpunit packages\yourname\package-name
Create a directory named tests in your root of the package and write various test classes inside it. You can have all functioning of your package inside various methods in a single class or you can use multiple classes.
Move to your project's root and run following commands,
vendor/bin/phpunit packages/YOUR_DIRECTORY_NAME/PACKAGE_NAME
Wait a little and you will get the response for your test cases. No. of positive and negative tests will be shown.

PhantomJs cannot execute binary file

I am trying to use PhantomJs in my laravel 5 project. I have downloaded via composer. I have added into my providers section and aliases section within config/app.php
Okay so i have now created my controller as seen below:
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use PDF;
use View;
class PdfController extends Controller {
public function index()
{
$view = View::make('home');
return PDF::createFromView($view, 'home.pdf');
}
}
I have also created my route for this method. However when i try this in my browser it throws the following error:
PhantomJS: sh: /Users/matts/sites/ManagementApp/vendor/antking/phantom-pdf/src/../bin/phantomjs: cannot execute binary file
Has anyone come across this before?
Thanks
The "cannot execute binary file" indicates that it was not able to run phantomjs at all, and it is probably not related to your code. The most likely reason for this is an architecture incompatibility. Depending on whether or not you have a 32 or 64 bit system, you should use either phantomjs-1.9.8-linux-x86_64 (64bit) or phantomjs-1.9.8-linux-i686. It is possible that you used the wrong one for your system.

Categories