0

I'm started to use PHPUnit, and I want to connect to my database and get some values from it, but as I have to connect to my database through the file 'database.php'.

'database.php' always had this problem with the undefined variable: $connected. So I read something about with the error-reporting message: error_reporting(E_ALL ^ E_NOTICE);

I did fix it earlier to my other files in my Project, but when I use PHPUnit I get this error message: Undefined variable: connected

My testcase file and it's code:

<?php include("database.php"); ?>
<?php
require_once 'person.php';

class PersonTest extends PHPUnit_Framework_TestCase {

    public function lookupPerson(){

    $result = database::query("select * from Person where forename='Anna'");
    $rows = mysql_numrows($result);
    for($i=0; $i < $rows; $i++){
    $arr[$i] = mysql_result($result, $i, 'forename');
    }
    return $arr;
    }


    public function testLooking(){
    $arr = PersonTest::lookupPerson();
    foreach($arr as $a){
    $this->assertEquals('Anna', $a);
    }
    }
}


?>

So I wonder what can I do?

Regards User

also Alexein

  • Is this your real test? You don't actually test anything in the Person class, just a method in the test class? – Fenton May 11 '12 at 11:52

3 Answers3

1

At the top of your code add below, this hides all notices... Ofcourse its best to just fix the error :)

error_reporting(0);
ini_set("display_errors", 0);
Hans Wassink
  • 2,529
  • 3
  • 30
  • 45
0

take a look at this question: test the return value of a method that triggers an error with PHPUnit

answers there show how to prevent PHPUnit from treating notices and warnings as exceptions.

if you can, fix the notice.

Community
  • 1
  • 1
Mariusz Sakowski
  • 3,232
  • 14
  • 21
0

I did fix it earlier to my other files in my Project, but when I use PHPUnit I get this error message: Undefined variable: connected

From what line in database.php is this notice being thrown? Perhaps you do a if (!$connected) somewhere, while $connected never has been written to. So begin your database.php with $connected = false and you'll lose the notice.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272