0

I have an array with names I use to generate random names in a form and I want to use it in more then one function.

class TestMyTest extends PHPUnit_Extensions_Selenium2TestCase {
public function setUp()
{
$this->setHost('localhost');
$this->setPort(4444);
$this->setBrowser("firefox");
$this->setBrowserUrl("xxxxxxxxxxxxxxxxxx");
}
    public $firstNameArray = array(
    "Howard",
    "Sheldon",
    "Leonard",
    "Rajesh"
    );

    public $lastNameArray = array(
    "Wolowitz",
    "Cooper",
    "Hofstadter",
    "Koothrappali"
    );


public function testCreateNewCustomer()
{

    $name = rand(0,count($firstNameArray));
    $this->byId('customer-name')->value($firstNameArray[$name].' '.$lastNameArray[$name]);
    $random_phone_nr = rand(1000000,9999999);   
    $this->byId('customer-phone')->value('070'.$random_phone_nr);
    $this->byId('customer-email')->value($firstNameArray[$name].'.'.$lastNameArray[$name].'@testcomp.com');
}

This gives me the error "Undefined variable: firstNameArray" on the row where I declare the $name variabel. I dont want to have to have to declare the same array in every function I want to use it. so how do I solve this?

1
  • why do you want to use this data via a class variable? I Think you should consider using a Dataprovider (phpunit.de/manual/3.7/en/…) or simply put this date inside your test-method. Commented Jun 13, 2013 at 10:38

1 Answer 1

1

It's not a global variable, it's a class/instance variable:

$this->byId('customer-name')->value($this->firstNameArray[$name].' '.$this->lastNameArray[$name]);
$this->byId('customer-email')->value($this->firstNameArray[$name].'.'.$this->lastNameArray[$name].'@testcomp.com');

EDIT

Sorry, I missed another reference:

$name = rand(0,count($this->firstNameArray)-1);
Sign up to request clarification or add additional context in comments.

6 Comments

Well I still get an error but a different one "Cannot access empty property" on the same line.
Yes I know, of course I edited all the rows with $firstNameArray pressent to $this->$firstNameArray.
Your rand() should be between 0 and count-1, not between 0 and count
Oh sorry, well If I used fixed numbers since I knwo how big the array is now I get the old warning, "Undefined variable"
Oh sorry, missed that u removed the variable defenition in fron of the array name, why is that btw? why does it work with $this->firstNameArray byt not with $this->$firstNameArray;?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.