1Feature: Collections
2
3  PHP Driver supports all Cassandra collections
4
5  Background:
6    Given a running Cassandra cluster
7
8  Scenario: Using Cassandra collections
9    Given the following schema:
10      """cql
11      CREATE KEYSPACE simplex WITH replication = {
12        'class': 'SimpleStrategy',
13        'replication_factor': 1
14      };
15      USE simplex;
16      CREATE TABLE user (
17        id int PRIMARY KEY,
18        logins list<timestamp>,
19        locations map<timestamp, double>,
20        ip_addresses set<inet>
21      );
22      INSERT INTO user (id, logins, locations, ip_addresses)
23      VALUES (
24        0,
25        ['2014-09-11 10:09:08+0000', '2014-09-12 10:09:00+0000'],
26        {'2014-09-11 10:09:08+0000': 37.397357},
27        {'200.199.198.197', '192.168.1.15'}
28      );
29      """
30    And the following example:
31      """php
32      <?php
33      $cluster   = Cassandra::cluster()->build();
34      $session   = $cluster->connect("simplex");
35      $result    = $session->execute("SELECT * FROM user");
36      $row       = $result->first();
37
38      echo "Logins:" . PHP_EOL;
39      foreach ($row['logins'] as $login) {
40          echo "  {$login}" . PHP_EOL;
41      }
42      echo "Locations:" . PHP_EOL;
43      foreach ($row['locations'] as $key => $location) {
44          echo "  {$key} => {$location}" . PHP_EOL;
45      }
46      echo "Ip Addresses:" . PHP_EOL;
47      foreach ($row['ip_addresses'] as $ip_address) {
48          echo "  {$ip_address}" . PHP_EOL;
49      }
50      """
51    When it is executed
52    Then its output should contain:
53      """
54      Logins:
55        1410430148000
56        1410516540000
57      Locations:
58        1410430148000 => 37.397357
59      Ip Addresses:
60        192.168.1.15
61        200.199.198.197
62      """
63
64  @cassandra-version-2.1
65  Scenario: Using Cassandra nested collections
66    Given the following schema:
67      """cql
68      CREATE KEYSPACE simplex WITH replication = {
69        'class': 'SimpleStrategy',
70        'replication_factor': 1
71      };
72      USE simplex;
73      CREATE TABLE users (
74        id uuid PRIMARY KEY,
75        name text,
76        addresses map<text, frozen<map<text, text>>>
77      );
78      """
79    And the following example:
80      """php
81      <?php
82      $cluster   = Cassandra::cluster()->build();
83      $session   = $cluster->connect("simplex");
84
85      $addressType = Cassandra\Type::map(Cassandra\Type::text(), Cassandra\Type::text());
86      $addressesType = Cassandra\Type::map(Cassandra\Type::text(), $addressType);
87
88      $users = array(
89          array(
90              new Cassandra\Uuid('56357d2b-4586-433c-ad24-afa9918bc415'),
91              'Charles Wallace',
92              $addressesType->create(
93                  'home', $addressType->create(
94                      'city', 'Phoenix',
95                      'street', '9042 Cassandra Lane',
96                      'zip', '85023'))
97          ),
98          array(
99              new Cassandra\Uuid('ce359590-8528-4682-a9f3-add53fc9aa09'),
100              'Kevin Malone',
101              $addressesType->create(
102                  'home', $addressType->create(
103                      'city', 'New York',
104                      'street', '1000 Database Road',
105                      'zip', '10025')
106              )
107          ),
108          array(
109              new Cassandra\Uuid('7d64dca1-dd4d-4f3c-bec4-6a88fa082a13'),
110              'Michael Scott',
111              $addressesType->create(
112                  'work', $addressType->create(
113                      'city', 'Santa Clara',
114                      'street', '20000 Log Ave',
115                      'zip', '95054'))
116          )
117      );
118
119      foreach ($users as $user) {
120          $options = array('arguments' => $user);
121          $session->execute("INSERT INTO users (id, name, addresses) VALUES (?, ?, ?)", $options);
122      }
123
124      $result = $session->execute("SELECT * FROM users");
125
126      foreach ($result as $row) {
127          echo "ID: {$row['id']}" . PHP_EOL;
128          echo "Name: {$row['name']}" . PHP_EOL;
129          echo "Addresses" . PHP_EOL;
130          $addresses = $row['addresses'];
131          foreach ($addresses->keys() as $type) {
132              echo "  {$type}:" . PHP_EOL;
133              foreach ($addresses->get($type) as $name => $value) {
134                  echo "    {$name} => {$value}" . PHP_EOL;
135              }
136          }
137      }
138      """
139    When it is executed
140    Then its output should contain:
141      """
142      ID: 56357d2b-4586-433c-ad24-afa9918bc415
143      Name: Charles Wallace
144      Addresses
145        home:
146          city => Phoenix
147          street => 9042 Cassandra Lane
148          zip => 85023
149      ID: ce359590-8528-4682-a9f3-add53fc9aa09
150      Name: Kevin Malone
151      Addresses
152        home:
153          city => New York
154          street => 1000 Database Road
155          zip => 10025
156      ID: 7d64dca1-dd4d-4f3c-bec4-6a88fa082a13
157      Name: Michael Scott
158      Addresses
159        work:
160          city => Santa Clara
161          street => 20000 Log Ave
162          zip => 95054
163      """
164