public static assertAddressValue ( |
||
$address | Address user type to validate | |
$expected | Expected address user type value (DEFAULT: self::generateAddressValue()) |
public static function assertAddressValue(UserTypeValue $address, UserTypeValue $expected = null)
{
// Determine if the expected value should be defaulted
if (is_null($expected)) {
$expected = self::generateAddressValue();
}
// Verify the address
self::assertEquals($expected->type(), $address->type());
self::assertCount(count($expected), $address);
self::assertEquals($expected->get("street"), $address->get("street"));
self::assertEquals($expected->get("zip"), $address->get("zip"));
$expectedPhoneNumbers = $address->get("phone_numbers");
$phoneNumbers = $address->get("phone_numbers");
if (!is_null($phoneNumbers)) {
self::assertInstanceOf('Cassandra\\Set', $phoneNumbers);
$expectedNumberOfPhoneNumbers = count($expectedPhoneNumbers);
self::assertCount($expectedNumberOfPhoneNumbers, $phoneNumbers);
// Verify phone numbers
self::assertEquals($expectedPhoneNumbers, $phoneNumbers);
if (count($expectedPhoneNumbers) > 0) {
foreach (range(0, $expectedNumberOfPhoneNumbers - 1) as $i) {
$expectedNumber = $expectedPhoneNumbers->values()[$i];
}
$number = $phoneNumbers->values()[$i];
self::assertCount(count($expectedNumber), $number);
self::assertInstanceOf('Cassandra\\UserTypeValue', $number);
self::assertEquals($expectedNumber->get("alias"), $number->get("alias"));
self::assertEquals($expectedNumber->get("number"), $number->get("number"));
}
}
}
/** * 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)); }