Cassandra\UserTypeIntegrationTest::generateAddressValue PHP Method

generateAddressValue() public static method

Generate a valid address user type with values that can be used for testing.
public static generateAddressValue ( ) : UserTypeValue
return UserTypeValue Valid address user type for testing
    public static function generateAddressValue()
    {
        // Create the phone numbers and add them to a set
        $homePhone = UserTypeIntegrationTest::getPhoneUserType();
        $homePhone->set("alias", "Home");
        $homePhone->set("number", self::PHONE_HOME_NUMBER);
        $workPhone = UserTypeIntegrationTest::getPhoneUserType();
        $workPhone->set("alias", "Work");
        $workPhone->set("number", self::PHONE_WORK_NUMBER);
        $phoneNumbers = new Set($homePhone->type());
        $phoneNumbers->add($homePhone);
        $phoneNumbers->add($workPhone);
        // Create the address and add the set of phone numbers
        $address = UserTypeIntegrationTest::getAddressUserType();
        $address->set("street", self::ADDRESS_STREET);
        $address->set("zip", self::ADDRESS_ZIP_CODE);
        $address->set("phone_numbers", $phoneNumbers);
        // Return the generated address
        return $address;
    }

Usage Example

 /**
  * Tuple using a nested user type.
  *
  * This test will ensure that the PHP driver supports the tuples collection
  * with user types.
  *
  * @test
  * @ticket PHP-57
  * @ticket PHP-58
  */
 public function testUserType()
 {
     // Create the user types
     $this->session->execute(new SimpleStatement(UserTypeIntegrationTest::PHONE_USER_TYPE_CQL));
     $this->session->execute(new SimpleStatement(UserTypeIntegrationTest::ADDRESS_USER_TYPE_CQL));
     // Create the table
     $query = "CREATE TABLE " . $this->tableNamePrefix . " (key timeuuid PRIMARY KEY, value " . "frozen<tuple<address>>)";
     $this->session->execute(new SimpleStatement($query));
     // Generate a valid address user type and assign it to a tuple
     $address = UserTypeIntegrationTest::generateAddressValue();
     $tuple = new Tuple(array($address->type()));
     $tuple->set(0, $address);
     // Assign the values for the statement
     $key = new Timeuuid();
     $values = array($key, $tuple);
     // Insert the value into the table
     $query = "INSERT INTO " . $this->tableNamePrefix . " (key, value) VALUES (?, ?)";
     $statement = new SimpleStatement($query);
     $options = new ExecutionOptions(array("arguments" => $values));
     $this->session->execute($statement, $options);
     // Select the tuple
     $query = "SELECT value FROM " . $this->tableNamePrefix . " WHERE key=?";
     $statement = new SimpleStatement($query);
     $options = new ExecutionOptions(array("arguments" => array($key)));
     $rows = $this->session->execute($statement, $options);
     // Ensure the tuple collection is valid
     $this->assertCount(1, $rows);
     $row = $rows->first();
     $this->assertNotNull($row);
     $this->assertArrayHasKey("value", $row);
     $tuple = $row["value"];
     $this->assertInstanceOf('Cassandra\\Tuple', $tuple);
     $this->assertCount(1, $tuple);
     // Verify the value can be read from the table
     UserTypeIntegrationTest::assertAddressValue($tuple->get(0));
 }