PHPUnit Fatal error: require() when running test - php

I'm new at PHPUnit and I'm having a problem when running the test.
I noticed that the problem was that I was not including the class I supposed to test so I put the following line:
require '../../files/Cliente.php';
But then it gaves me the next error:
PHP Warning: require(../../files/Cliente.php): failed to open stream: No such file or directory in /home/esperanza/Escritorio/clasificadosj-clasificados-7da0b31e6060/pruebas/files/ClienteTest.php on line 6
PHP Fatal error: require(): Failed opening required '../../files/Cliente.php' (include_path='.:/usr/share/php:/usr/share/pear') in /home/esperanza/Escritorio/clasificadosj-clasificados-7da0b31e6060/pruebas/files/ClienteTest.php on line 6
My directory structure is the next:
Project
|_files
|_Cliente.php
|_Test
|_ClienteTest.php
I don't know if I have to configure a XML document or a bootstrap first.
Thanks a lot!

The problem is that you require a file that does not exists. As require will give a fatal error when a file is not found, you get that fatal error. Compare with: Difference between "include" and "require" in php.
Check the path to the file you want to require and correct it:
require __DIR__ . '/../files/Cliente.php';
Make use of the __DIR__ magic constant so that it's easy to specify in a relative fashion to the php-file of the testcase you write while the path is absolute. You then prevent to use the include_path configuration directive which makes it easier to trouble-shoot.

Related

PHP require 'guzzle.phar' throws Fatal Error but on page refresh works

I want to use Guzzle in my project without using Composer(limitation I can't avoid). I have downloaded their phar file in my project directory PROJECT_ROOT/includes/guzzle.phar
Now in my other files if I use require_once or include or include_once it throws Fatal Error below:
PHP Fatal error:
require(): Failed opening required 'phar://guzzle.phar/autoloader.php (include_path='.:/usr/share/php:PROJECT_ROOT/includes') in PROJECT_ROOT/includes/guzzle.phar on line 3
But when I use
require 'includes/guzzle.phar'
it throws the above error first time the page loads but when I refresh it doesn't throw any error and works ok after that.
Over the past two days, I have looked at various SO questions, tried using include_once, include, require_once and adding PROJECT_ROOT to include path but nothing seems to work.
Any help is appreciated and do let me know if any more info is needed. Thanks
The mentioned PHP Fatal error will throw only if the file path is in correct, you can also find Warnings mentioning the file path is incorrect just before the Fatal error.
PHP Fatal error:
require(): Failed opening required 'phar://guzzle.phar/autoloader.php (include_path='.:/usr/share/php:PROJECT_ROOT/includes') in PROJECT_ROOT/includes/guzzle.phar on line
On each Error/Warning/Notice PHP include information about the error cause to help the developer to track the issue.
Failed opening required 'phar://guzzle.phar/autoloader.php
From the above line/code you can find that the guzzle.phar file path is incorrect, try to use an absolute path insted of relative one.
require_once $_SERVER['DOCUMENT_ROOT'].""."includes/guzzle.phar"

PHP require_once '...........';

I am calling the function require_once '../autoload.php'; but I get the error
Warning: require_once(../autoload.php): failed to open stream: No such file or directory in C:\xampp\htdocs\Api\Autotask\vendor\test.php on line 2
Fatal error: require_once(): Failed opening required '../autoload.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\Api\Autotask\vendor\test.php on line 2
What am I doing wrong?
To avoid this type of problems, and for best practice, i advice to use the root path:
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
The code above will assign to $root your realpath on the server.
After that just use the require/include etc according to your localhost/webserver root :
require_once($root/Api/Autotask/vendor/autoload.php)
For example, if your file is located in websiterootfolder/php/nice_folder
Your code will be :
require_once($root/php/nice_folder/autoload.php)
By using this practice, you can include any file from whatever position and any way you want, dynamic or not

trouble with require_once failed to open stream: No such file or directory

I'm getting the following error when trying to open the index file of a project that I am taking over on:
Warning: require_once(core_services/logging.php): failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/app/docroot/www.website.com/index.php on line 7
Fatal error: require_once(): Failed opening required 'core_services/logging.php' (include_path='.:/Applications/XAMPP/xamppfiles/lib/php') in /Applications/XAMPP/xamppfiles/htdocs/app/docroot/www.website.com/index.php on line 7
The code the error refers to looks like this:
7 require_once "core_services/logging.php";
8 $logger=new syslog_class(__FILE__,LOG_DEBUG);
I checked to make sure the filepath exists by defining it and echoing it back as well as the contents of logging.phpin terminal and that returns just fine and when I get the current working directory on line 6, it returns /Applications/XAMPP/xamppfiles/htdocs/app/docroot/www.website.com, which I am assuming is defining my root folder.
Not sure exactly how to go about resolving this problem and if anyone has any ideas on approach, I would love to hear them as my PHP is a bit rusty and lacking outside of the context of wordpress.
edit: approaches I've tried so far
require 'core_services/logging.php';
require '/core_services/logging.php';
require './core_services/logging.php';
require '../core_services/logging.php';
require '/Applications/XAMPP/xamppfiles/htdocs/app/docroot/www.website.com/core_services/logging.php';
require realpath(dirname(__FILE__)) .'/core_services/logging.php';
require(__DIR__"/../core_services/logging.php");
require_once 'core_services/logging.php';
require_once '/core_services/logging.php';
require_once './core_services/logging.php';
require_once '../core_services/logging.php';
require_once '/Applications/XAMPP/xamppfiles/htdocs/app/docroot/www.website.com/core_services/logging.php';
require_once realpath(dirname(__FILE__)) .'/core_services/logging.php';
require_once __DIR__"/../core_services/logging.php";
I have also verified that it is not a permissions issue.
Try specifying full path:
require './core_services/logging.php';
//OR
require '/Applications/XAMPP/xamppfiles/htdocs/app/docroot/www.website.com/core_services/logging.php';

include path error in the cron job in php

i have the functionality to generate XML file on each day using php.
This works fine in the browser.but it doesn't work in the cron job.
The cron give me error for the required once and Fatal error.And it stops the execution of my cron job.
the included file is also including other files.
the error which i am getting is like,
Warning: require_once(/common/configs/config_local.inc.php) [function.require-once]: failed to open stream: No such file or directory in /home/wwwsite/public_html/sitename/common/configs/config.inc.php on line 281
Fatal error: require_once() [function.require]: Failed opening required '/common/configs/config_local.inc.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/wwwsite/public_html/sitename/common/configs/config.inc.php on line 281
any one having solution to this issue?
It's telling you it can't find '/common/configs/...' note the absolute path there with the first character being a '/'.
You can either make it a relative path from where you are defining it, or put the prefix in there as well ('/home/wwwsite/public_html/sitename/'), maybe with a variable ($sitebase ='/home/wwwsite/public_html/sitename/';) and prefix the include filename with it.
The reason it is working on a website, but not on the command line could be one of a few ways. Most obvious is that the php.ini include_path for the CLI (command line interface) doesn't include the '.' for checking from the current directory. There's also a good reason it doesn't have it by default. The other way is maybe your code puts the ..../sitename/ directory into the include_path - or otherwise sets up the files to be included automatically.
i have got the solution,
curl yoursitename/yourfilename.php > /dev/null 2>&1
This has run my cron job as i have set without any error

Two Errors trying to include .php file: Warning: require(...) AND Fatal error: require() ... (include_path='.;C:\php\pear')

Here is the first error:
Warning: require(/hb/includes/constants.php) [function.require]: failed to open stream: No such file or directory in C:\wamp2\www\hb\includes\connection.php on line 2
And the second error that immediately follows:
Fatal error: require() [function.require]: Failed opening required '/hb/includes/constants.php' (include_path='.;C:\php\pear') in C:\wamp2\www\hb\includes\connection.php on line 2
Here is the line that it doesn't like:
require("/hb/includes/constants.php");
I did install a new version of WAMP, but I know the directories/files are fine and the code worked previously and on another server. It seems like none of my require()'s are working anymore. I tried looking around and some suggested to others that they edit their php.ini, but I don't know what directory I would point to - if you can't tell I'm pretty noobish regarding all things server related.
Any help is appreciated.
First of all, are your really -- really ! -- sure that the file /hb/includes/constants.php, that your are trying to include, exists ?
Considering that the path you requested begins with a /, it looks like you're using an absolute path... that looks like an UNIX path, but you are on a Windows server...
If it exists, and PHP still cannot include it, check :
that it is readable by your webserver (including the directories) -- I'm thinking about UNIX permissions
that there is no open_basedir restriction that would prevent PHP from accessing those files.
BTW, having a warning and a fatal error is the behavior that's to be expected when using require : the warning is normal (it comes from include) ; and the fatal error is the difference between include and require.

Categories