phpunit cannot find Class, PHP Fatal error - laravel

I get phpunit and install it as this link using the simplest way for test purposes.
I just download the phpunit.phar file, chmod & rename & move to /usr/local/bin Then, I run phpunit --version, its ok.
PHPUnit 3.7.27 by Sebastian Bergmann
I write a simple test
public function testSomething(){
$this -> assertTrue(true)
}
Then I go into the source file folder, phpunit --colors Test
It works. So, I decide write a complex demo.
My project folder structure is like this.
Project Name
--> app
--> api
--> tests
Now I write a simple class in app/api/FlyApi.php
<?php
class FlyApi {
public function makeFly(){
//do something else
}
}
Then I write another test class for FlyApi.php
<?php
class FlyApiTest extends PHPUnit_Framework_TestCase {
public function testFly(){
//new a FlyApi
$flyApi = new FlyApi();
//do something
}
}
At this line $flyApi = new FlyApi() I got the error.
PHP Fatal error: Class 'FlyApi' not found in /home/kevin/Workspace/fly/app/api/FlyApi.php on line 23
Yes, this line $flyApi = new FlyApi()

You didn't load the definition of your FlyApi class.
This solution is Laravel specific:
You should be extending TestCase rather than PHPUnit_Framework_TestCase.

Try adding your /api/ folder into your ClassLoader at app\start\global.php.
You will find this section:
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
...
app_path().'/api/
));

Are you using Laravel's phpunit.xml file? It includes Laravel's (and Composer's) autoload.php file which lets you use all your autoloaded classes within it.
Finally, what's the whole error? It should (hopefully) tell you what class it's trying to load (which will give you clues if the namespace is wrong or something).

Related

How to set up PhpStorm correctly? Сan't run PHPUnit on Laravel 8 on Windows 10. Call to undefined function Tests\Unit\is_id_term()

I have installed PHPUnit globally and set up an environment variable "path" by, so it runs by command phpunit.phar
I created a file with a custom function in the "app" folder:
function is_id_term(string $q, &$id = null, string $name = 'id'): bool {
...
...
...
...
...
}
After that I added at the autoload section in the composer.json file in the following line:
"files": [
"./app/functions.php"
]
Then I ran the command:
composer dump-autoload
Then created unit test in a tests/unit/ folder:
namespace Tests\Unit;
use PHPUnit\Framework\TestCase;
class IsIdTermTest extends TestCase
{
public function testOne()
{
$res = is_id_term('id: 1');
$this->assertTrue($res);
}
public function testString()
{
$res = is_id_term('id:lucas');
$this->assertFalse($res);
}
}
If I run these tests using the green button in the upper right corner of PhpStorm, I get the following error:
C:\localserver\php8.0.2\php.exe C:\Users\webgr\AppData\Roaming\Composer\vendor\laravel\installer\bin\phpunit.phar --no-configuration C:\Users\webgr\projects\my-blog\tests --teamcity --cache-result-file=C:\Users\webgr\projects\my-blog\.phpunit.result.cache
Testing started at 19:00 ...
PHPUnit 9.5.4 by Sebastian Bergmann and contributors.
Error : Call to undefined function Tests\Unit\is_id_term()
C:\Users\webgr\projects\my-blog\tests\Unit\IsIdTermTest.php:11
After starting the test using the green button, file appears in the root folder of the project .phpunit.result.cache with this content
C:37:"PHPUnit\Runner\DefaultTestResultCache":230:{a:2:{s:7:"defects";a:2:{s:32:"Tests\Unit\IsIdTermTest::testOne";i:4;s:35:"Tests\Unit\IsIdTermTest::testString";i:4;}s:5:"times";a:2:{s:32:"Tests\Unit\IsIdTermTest::testOne";d:0.004;s:35:"Tests\Unit\IsIdTermTest::testString";d:0;}}}
But, if I execute the command in terminal:
phpunit.phar
then all tests pass without errors.
In the same way, PhpStorm finds this function without problems if I click on its name while in some other file.
Can you please tell me how to set up PhpStorm? And what is my mistake?
Try replacing
use PHPUnit\Framework\TestCase;
with
use Tests\TestCase;
The artisan make:test {name} --unit command seems to always generate the template with the TestCase referenced as PHPUnit\Framework\TestCase
After reinstalling the project, everything worked by itself, but I still don't know what was the cause of the error. I believe that the reason was that I created the project before I installed PHPUnit globally and configured XDebug. Although of course I could be wrong.

Change public directory laravel

I want to change the public folder to public_html, the main problem is that some commands and functions not work because it still point to public.
I am trying to use the laravel+vue to develop a spa. i normally rename the public folder to public_html and that's all, but know it seems that i have to use the public_path helper and the console and these use the public folder too.
im using npm run dev as in the laravel docs, but it generate everything in the public folder, not in the public_html that i have renamed
so how can i tell the console and the public_path helper to do they magic in the public_html folder?
I am using Laravel 5.6, it is a fresh project, with just a controller and a view with a mix helper calling the js that i want link this <script src="{{ mix('js/app.js') }}"></script>.
May this can help you. You can override Laravel default helper methods. This code helped me at a time when I got same problem like you.
Simple I created one helper file and override public_html function something like this:
/**
* Get the path to the public folder.
*
* #param string $path
* #return string
*/
function public_path($path = '')
{
return base_path().'/public_html';
}
Let's say file name is AppHelper.php and located into app/Http folder.
To override helper from base you need to include your helper file(AppHelper.php) before Laravel does it's own autoload file.
So for the web you have to include your file into index.php like this:
require __DIR__.'/../app/Http/AppHelper.php';
require __DIR__.'/../vendor/autoload.php';
Remember your helper must be included before autoload file. and this code snippet for Command line or Artisan CLI:
require __DIR__.'/app/Http/AppHelper.php';
require __DIR__.'/vendor/autoload.php';
You have to write your artisan cli changes into artisan file which is on root.
In Laravel if you check helper function then each and every function wrapped with if condition like this function is not exist then declare it using function_exists()
In our case we already declare public_path function and included before Laravel autoload file so every request first execute our function.
Hope this can help you. Good luck.
Add this in register() method of your AppServiceProvider
public function register()
{
// Add this part
$this->app->bind('path.public', function() {
return base_path().'/public_html';
});
}
For the css+js resources, as #Bagus Tesa said, you have a file named webpack.mix.js in the root of your application folder. It's not you who created it, it comes with the laravel project. In that file you'll find something like:
mix.js('resources/assets/js/app.js', 'public/js')
.sass('resources/assets/sass/app.scss', 'public/css');
Change the destination path from public to public_html

Can't use public_path() in a helper file Laravel 5.4

I have a helper.php file in app/Helpers directory. I included that file in composer.json:
...
"files": [
"app/Helpers/helpers.php"
]
...
Helper works fine but I can't use public_path() method there. I need to include another file (please don't ask me why because it's old code that I don't need to rewrite). So I have the following:
require_once public_path() . '/appadmin/bootstrap.php';
I know that by default Laravel looks in /public/ folder but I faced with a problem. If I need to perform composer update I have to use public/appadmin/bootstrap.php path in helper.php, but after performing I have to change that path to /appadmin/bootstrap.php for correct work. That's why I decide to use public_path() method to receive correct path for both cases. And if I use it I'm getting an error:
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postUpdate
Script Illuminate\Foundation\ComposerScripts::postUpdate handling
the post-update-cmd event terminated with an exception
[ReflectionException]
Class path.public does not exist
Thank's in advance!
Have you tried updating your app to the current revision?
There are some files in the framework itself who need to be updated.
Check out the config files, the bootstrap files, server.php and the start files here https://github.com/laravel/laravel/tree/develop.
You could open index.php (in your public directory) and change:
$app = require_once __DIR__.'/../bootstrap/app.php';
// set the public path to this directory
$app->bind('path.public', function() {
return __DIR__;
});
Now you dont need to change your public path when your public directory has changed.

Fatal error: Class 'PayPal\Api\Payer' not found codeigniter

I uploaded the paypal php sdk to my codeigniter app inside the libraries/paypal folder.
In the same folder I create a paypal.php file and inside it I insert this code:
<?php
require __DIR__ . '/common.php';
use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction;
class Paypal{
function createPayment(){
$payer = new Payer();
$payer->setPaymentMethod("paypal");
//....
}
}
but when I call this function in my controller I get this error:
Fatal error: Class 'PayPal\Api\Payer' not found in '....'
I don't understand why instead common.php is imported correctly.
In my app I also have the Facebook sdk and I created the same structure to use it and it works
Best way is to follow these steps.
Download the SDK using composer.
Upload the vendor directory into codeIgniter Application root.
Then in the index.php file put this line
include "./vendor/autoload.php";
Now you can access the Paypal SDK and its dependent libraries easily.

Unable to get laravel/database and restler working with versioning

I have an existing website up and running, and now I want to add a REST interface to it in an api subdirectory. I'm not able to get this to work with versioning. I installed like so (no errors):
$ php ~/bin/composer.phar create-project laravel/database --prefer-dist api
$ cd api
$ php ~/bin/composer.phar require restler/framework 3.0.0-RC6
Then I uncommented the lines in public/index.php related to Restler and add a new API class that just echos a string. If I run this via php artisan serve and look at it through the localhost URL, then the method works.
Now I want to enable versioning, so I added these lines to public/index.php
use Luracast\Restler\Defaults;
Defaults::$useUrlBasedVersioning = true;
And in app/controllers I created a v1 directory and moved Test.php into that. I also added a namespace directive to the file of the format namespace A\B\v1
When I restart the artisan server and query the API, I get a 404 error. I've tried as both http://localhost:8000/Test and http://localhost:8000/v1/Test
What have I forgotten to do?
Here is how I made it to work. Note the folder where I placed the api class file.
in index.php
use Luracast\Restler\Restler;
use Luracast\Restler\Defaults;
Defaults::$useUrlBasedVersioning = true;
$r = new Restler();
$r->addAPIClass('A\B\Test');
Test.php kept in app/controllers/A/B/v1/Test.php
<?php namespace A\B\v1;
class Test
{
public function get()
{
return 'working';
}
}
Both http://localhost:8000/v1/test and http://localhost:8000/test return "working"

Resources