1<?php
2
3/**
4 * Copyright 2015-2017 DataStax, Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19namespace Cassandra;
20
21class SetIntegrationTest extends CollectionsIntegrationTest
22{
23    /**
24     * Set with scalar types
25     *
26     * This test ensures that sets work with all Cassandra's
27     * scalar types.
28     *
29     * @test
30     * @dataProvider setWithScalarTypes
31     */
32    public function testScalarTypes($type, $value) {
33        $this->createTableInsertAndVerifyValueByIndex($type, $value);
34        $this->createTableInsertAndVerifyValueByName($type, $value);
35    }
36
37    /**
38     * Data provider for sets with scalar types
39     */
40    public function setWithScalarTypes() {
41        // Ensure duration data type is not used as a key for set
42        $scalarCassandraTypes = array_filter($this->scalarCassandraTypes(),
43            function($cassandraType) {
44                if ($cassandraType[0] != "duration") {
45                    return $cassandraType;
46                }
47            }
48        );
49
50        return array_merge(
51            array_map(function ($cassandraType) {
52                $setType = Type::set($cassandraType[0]);
53                $set = $setType->create();
54                foreach ($cassandraType[1] as $value) {
55                    $set->add($value);
56                }
57                return array($setType, $set);
58            }, $scalarCassandraTypes),
59            array_map(function ($cassandraType) {
60                $set = new Set($cassandraType[0]);
61                foreach ($cassandraType[1] as $value) {
62                    $set->add($value);
63                }
64                return array($set->type(), $set);
65            }, $this->constantScalarCassandraTypes())
66        );
67    }
68
69    /**
70     * Set with composite types
71     *
72     * This test ensures that sets work with other nested collections
73     * and other composite types such as UDTs and tuples.
74     *
75     * @test
76     * @ticket PHP-62
77     * @ticket PHP-57
78     * @ticket PHP-58
79     * @dataProvider setWithCompositeTypes
80     */
81    public function testCompositeTypes($type, $value) {
82        $this->createTableInsertAndVerifyValueByIndex($type, $value);
83        $this->createTableInsertAndVerifyValueByName($type, $value);
84    }
85
86    /**
87     * Data provider for sets with composite types
88     */
89    public function setWithCompositeTypes() {
90        return array_map(function ($cassandraType) {
91            $setType = Type::set($cassandraType[0]);
92            $set = $setType->create();
93            foreach ($cassandraType[1] as $value) {
94                $set->add($value);
95            }
96            return array($setType, $set);
97        }, $this->compositeCassandraTypes());
98    }
99
100    /**
101     * Set with nested composite types
102     *
103     * This test ensures that sets work with other nested collections
104     * and other composite types such as UDTs and tuples.
105     *
106     * @test
107     * @ticket PHP-62
108     * @ticket PHP-57
109     * @ticket PHP-58
110     * @dataProvider setWithNestedTypes
111     */
112    public function testNestedTypes($type, $value) {
113        $this->createTableInsertAndVerifyValueByIndex($type, $value);
114        $this->createTableInsertAndVerifyValueByName($type, $value);
115    }
116
117    /**
118     * Data provider for sets with nested composite types
119     */
120    public function setWithNestedTypes() {
121        return array_map(function ($cassandraType) {
122            $setType = Type::set($cassandraType[0]);
123            $set = $setType->create();
124            foreach ($cassandraType[1] as $value) {
125                $set->add($value);
126            }
127            return array($setType, $set);
128        }, $this->nestedCassandraTypes());
129    }
130
131    /**
132     * Bind statement with an empty set
133     */
134    public function testEmpty() {
135        $setType = Type::set(Type::int());
136        $this->createTableInsertAndVerifyValueByIndex($setType, $setType->create());
137        $this->createTableInsertAndVerifyValueByName($setType, $setType->create());
138    }
139
140    /**
141     * Bind statement with an null set
142     */
143    public function testNull() {
144        $setType = Type::set(Type::int());
145        $this->createTableInsertAndVerifyValueByIndex($setType, null);
146        $this->createTableInsertAndVerifyValueByName($setType, null);
147    }
148}
149