How do I run phpunit test despite tested code throwing Notices - php

I know ideally that I should fix all the places in my code where PHP Notices are being thrown but for now I am trying to get a simple test up and running for one part of the application.
When I try to run my test using
sudo phpunit -c tests/phpunit.xml tests/
I get the following notices:
PHP Notice: Undefined property: stdClass::$integer_value in /var/www/application/authenticate.php on line 111
PHP Notice: Undefined property: stdClass::$boolean_value in /var/www/application/authenticate.php on line 115
PHP Notice: Undefined property: stdClass::$boolean_value in /var/www/application/site/authenticate.php on line 115
PHP Notice: Undefined property: stdClass::$integer_value in /var/www/application/authenticate.php on line 111
I've tried following the advice on this answer to a similar question https://stackoverflow.com/a/8412996/3263538
and have created a phpunit.xml configuration file which includes the following xml to ignore notices and warnings
tests/phpunit.xml
<phpunit convertErrorsToExceptions="false"
convertNoticesToExceptions="false"
convertWarningEToExceptions="false">
</phpunit>
UtilitesTest.php (dummy test that returns true)
<?php
use PHPUnit\Framework\TestCase;
include_once "/var/www/application/utilities.php";
class UtilitiesTest extends TestCase
{
public function testEmpty()
{
return 1;
}
}
?>
but still no luck. Is there something I'm missing here? Ideally I'd like to suppress these Notice messages.

Related

PHP Cpanel Cron with Argv

I have a cron job that runs daily (for over a year) and suddenly does not work.
I have a cron job that looks like:
/usr/bin/php -q /home/tcanfarotta22/public_html/test_arg.php low_customer=2 high_customer=200
And then a PHP script that gets the variables like:
parse_str($argv[1], $params);
parse_str($argv[2], $params2);
$low_customer = $params['low_customer'];
$high_customer = $params2['high_customer'];
Now I am getting the following errors:
[18-Jun-2018 10:38:01 America/New_York] PHP Notice: Undefined
variable: argv in /home/tcanfarotta22/public_html/test_arg.php on line
5 [18-Jun-2018 10:38:01 America/New_York] PHP Notice: Undefined
variable: argv in /home/tcanfarotta22/public_html/test_arg.php on line
6 [18-Jun-2018 10:38:01 America/New_York] PHP Notice: Undefined
index: low_customer in /home/tcanfarotta22/public_html/test_arg.php on
line 8 [18-Jun-2018 10:38:01 America/New_York] PHP Notice: Undefined
index: high_customer in /home/tcanfarotta22/public_html/test_arg.php
on line 9
Nothing has changed so I ma not sure what to do. Does anyone know?
If the enabling of register_argc_argv does not fix the issue, then maybe you are trying to access $argv within a class, in this case, you need to declare it as global before you can access it:
global $argv;
BTW, I have tested your code (executed by the terminal) and it runs like a charm.
If this does not fix the issue, maybe then you need to put more details, also we need to know how you are testing?

PHP CRYPT_BLOWFISH Error?

I'm on Redhat and using PHP Crypt_Blowfish and having following error.
ERROR:
Notice: Constant CRYPT_BLOWFISH_AUTO already defined in /usr/share/pear/Crypt/Blowfish.php on line 33 Notice: Constant CRYPT_BLOWFISH_MCRYPT already defined in /usr/share/pear/Crypt/Blowfish.php on line 38 Notice: Constant CRYPT_BLOWFISH_PHP already defined in /usr/share/pear/Crypt/Blowfish.php on line 43 Fatal error: Cannot redeclare class Crypt_Blowfish in /usr/share/pear/Crypt/Blowfish.php on line 88
My code is just something like:
include_once('Blowfish.php');
$key = '12345678';
$cipher = new Crypt_Blowfish($key);
echo Eencrypt($cipher, "hello world!");
And when i check my php.ini, the mcrypt is also already installed and enabled.
What went seriously wrong please?
Strict standards warnings are no errors. You can remove them from your error_reporting setting easily:
error_reporting(error_reporting() & ~E_STRICT);

PHP Error log file format (php.ini error_log directive) on Windows

For an example:
php.ini file
...
; Log errors to specified file.
error_log = c:/php/php.log
...
Error log file (c:/php/php.log) contains every entry in this format:
[12-Jun-2011 12:58:55] PHP Notice: Undefined variable: test in C:\www\phpinfo.php on line 2\r\r\n
[12-Jun-2011 12:59:01] PHP Notice: Undefined variable: test in C:\www\phpinfo.php on line 2\r\r\n
[12-Jun-2011 13:01:12] PHP Notice: Undefined variable: test in C:\www\phpinfo.php on line 2\r\r\n
[12-Jun-2011 13:02:11] PHP Notice: Undefined variable: test in C:\www\phpinfo.php on line 2\r\r\n
[12-Jun-2011 13:11:23] PHP Notice: Undefined variable: test in C:\www\phpinfo.php on line 2\r\r\n
[12-Jun-2011 13:12:10] PHP Notice: Undefined variable: test in C:\www\phpinfo.php on line 2\r\r\n
Two carriage return character and one new line per one error line.
Why it happens?
How to change error log file to default format:
[12-Jun-2011 12:58:55] PHP Notice: Undefined variable: test in C:\www\phpinfo.php on line 2\r\n
[12-Jun-2011 12:59:01] PHP Notice: Undefined variable: test in C:\www\phpinfo.php on line 2\r\n
[12-Jun-2011 13:01:12] PHP Notice: Undefined variable: test in C:\www\phpinfo.php on line 2\r\n
[12-Jun-2011 13:02:11] PHP Notice: Undefined variable: test in C:\www\phpinfo.php on line 2\r\n
[12-Jun-2011 13:11:23] PHP Notice: Undefined variable: test in C:\www\phpinfo.php on line 2\r\n
[12-Jun-2011 13:12:10] PHP Notice: Undefined variable: test in C:\www\phpinfo.php on line 2\r\n
PHP Version 5.3.6
Apache/2.2.15 (Win32)
Tested on Windows 7 Home Basic and Windows XP SP3, same results.
php.ini file contains only two strings
log_errors = On
error_log = c:/server/php.log
apache phpinfo() script -> http://pastehtml.com/view/awvx1vgpp.html
PS.
sever: nginx 1.0.4
FastCGI + PHP Version 5.3.6
Everything works as expected.
nginx phpinfo() script -> http://pastehtml.com/view/awvwvk9p9.html
There are more ini directives to control the output into the error log. These are explained on this PHP manual page.
By default, PHP running with not loading the ini and only having the error_log set and logging enabled, it does output a single line only. You can test that on your system as well, it demonstrates it well for the Command Line Interface (CLI) "Server Application Programming Interface (API) [SAPI]":
php -n -d error_log=./error.log -d log_errors=1 -r 'error;'
Use this command to make an isolated run and compare it with your output. If it has two line endings as well, then this is probably a bug of your php version.
If not a problem with the PHP version [not the case in the original posting (OP)] then within your application some settings looks to be altered. You need to find out which setting. Probably you can find it by registering an own error handler and then dumping the ini settings.
Last but not least, if it isn't a misunderstanding of the line ending (see my comment), then one may want to also look in the ini settings named error_prepend_string and error_append_string regarding the error display (but not the error logging).

phpunit.xml Does not seem to be working

I have bee trying to get this test to run
require_once 'PHPUnit/Framework.php';
require_once('../config/config.php');
require_once('../classes/division.class.php');
class DivisionTest extends PHPUnit_Framework_TestCase
{
public function Divisiontest()
{
$division = new division();
try{
$division->createDivisionDetails();
}catch (CustomException $e) {
return;
}
$this->fail('An expected exception has not been raised.');
}
}
on this Command
phpunit --configuration=phpunit.xml division.test.php
but keep geting the error message
PHP Notice: Undefined index: HTTP_HOST in /home/workspace/people_scope/config/config.php on line 13
Notice: Undefined index: HTTP_HOST in /home/workspace/people_scope/config/config.php on line 13
PHP Notice: Undefined index: HTTP_HOST in /home/workspace/people_scope/config/config.php on line 26
Notice: Undefined index: HTTP_HOST in /home/workspace/people_scope/config/config.php on line 26
PHP Warning: require_once(/people_scope/config/standard.inc.php): failed to open stream: No such file or directory in /home/workspace/people_scope/config/config.php on line 46
Warning: require_once(/people_scope/config/standard.inc.php): failed to open stream: No such file or directory in /home/workspace/people_scope/config/config.php on line 46
PHP Fatal error: require_once(): Failed opening required '/people_scope/config/standard.inc.php' (include_path='/usr/bin:.:/usr/share/php:/usr/share/pear:/people_scope/assets/PEAR/:/people_scope/classes/base/') in /home/workspace/people_scope/config/config.php on line 46
Fatal error: require_once(): Failed opening required '/people_scope/config/standard.inc.php' (include_path='/usr/bin:.:/usr/share/php:/usr/share/pear:/people_scope/assets/PEAR/:/people_scope/classes/base/') in /home/workspace/people_scope/config/config.php on line 46
I am using a phpunit.xml to try to set the $_SERVER['HTTP_HOST'] but seem like it is not working at all
<?xml version="1.0" encoding="utf-8" ?>
<phpunit>
<php>
<server name="HTTP_HOST" value="DEV"/>
</php>
</phpunit>
Everything I have read seem to say this should work
The xml looks fine as far as i can see it. Two suggestions:
class DivisionTest extends PHPUnit_Framework_TestCase
{
public function Divisiontest()
i'd change the function name to testDivision() so phpunit picks it up as a real test (the code gets execution because its named like the class (a.k.a. php 4 style constructor) but thats not really optimal)
If that doesn't fix your problem: Does it work when you put
$_SERVER['HTTP_HOST'] = "DEV";
before the
$division = new division();
statement ? Just to make sure the error is that the xml file isn't read and doesn't hide somewhere else

How to get the Ansinerator library to work?

I'm trying to get the Ansinerator library to work my on my localhost, but something's amiss.
I'm receiving the following errors:
Notice: Undefined index: hash in ansi.php on line 23 Notice: Undefined index: aml in ansi.php on line 37 Notice: Undefined variable: PNG_DESTRUCT in ansi.php on line 119 Notice: Undefined offset: -1 in ansi.php on line 362 Notice: Trying to get property of non-object in ansi.php on line 362 Notice: Undefined variable: links in ansi.php on line 459 Notice: Trying to get property of non-object in ansi.php on line 677 Notice: Undefined variable: links in ansi.php on line 459 Notice: Undefined variable: map_link in ansi.php on line 687
Any idea where am I going wrong with this one? I've installed the php-gd library, restarted the server, and while runs perfectly on production server, localhost will refuse. Ideas?
Thank you.
Check
- That you've got 'proper' versions of everything installed on localhost. (Compare localhost with production.) There's obviously something different between the 2 servers here, just use phpinfo() to check.
Restart localhost again? (Just kidding...)
Is it possible that you have set different error reporting levels on both servers?
Before anything, put error_reporting(E_ALL); at the top of your script on both servers and see if it runs perfectly. (My guess is you will get errors on both servers)

Categories