|
9 | 9 | use GetStream\Stream\Feed; |
10 | 10 | use PHPUnit\Framework\TestCase; |
11 | 11 | use Ramsey\Uuid\Uuid; |
| 12 | +use GuzzleHttp\Exception\ClientException; |
12 | 13 |
|
13 | 14 | class CollectionTest extends TestCase |
14 | 15 | { |
@@ -64,111 +65,116 @@ protected function setUp() |
64 | 65 | $this->client->timeout = 10000; |
65 | 66 | $this->user1 = $this->client->feed('user', Uuid::uuid4()); |
66 | 67 | $this->user2 = $this->client->feed('user', Uuid::uuid4()); |
67 | | - // $this->aggregated2 = $this->client->feed('aggregated', Uuid::uuid4()); |
68 | | - // $this->aggregated3 = $this->client->feed('aggregated', Uuid::uuid4()); |
69 | | - // $this->flat3 = $this->client->feed('flat', Uuid::uuid4()); |
70 | | - // $activity_data = ['actor' => 1, 'verb' => 'tweet', 'object' => 1]; |
71 | | - // $response = $this->user1->addActivity($activity_data); |
72 | | - // $this->activity_id = $response['id']; |
73 | 68 | $this->collections = $this->client->collections(); |
74 | 69 | } |
75 | 70 |
|
| 71 | + public function cleanUp(){ |
| 72 | + try { |
| 73 | + $this->collections->delete("food","cheese-burger"); |
| 74 | + } catch(ClientException $e) { |
| 75 | + // pass |
| 76 | + } |
| 77 | + } |
| 78 | + |
76 | 79 | public function testUpsert() |
77 | 80 | { |
78 | 81 | $collection = $this->collections->upsert('animals', ['id' => '1', 'name' => 'bear', 'color' => 'blue']); |
79 | 82 | $collection = $this->collections->upsert('items', ['id' => '42', 'name' => 'towel']); |
80 | 83 |
|
81 | 84 | } |
82 | 85 |
|
83 | | - public function testAddDataCollection() |
84 | | - { |
85 | | - $data = array('client' => 'php'); |
86 | | - $collection = $this->collections->add('like', $this->activity_id, 'bob', $data); |
87 | | - $this->assertSame($collection['user_id'], 'bob'); |
88 | | - $this->assertSame($collection['kind'], 'like'); |
89 | | - $this->assertSame($collection['activity_id'], $this->activity_id); |
90 | | - $this->assertSame($collection['data'], $data); |
91 | | - } |
92 | | - |
93 | | - public function testCreateReference() |
| 86 | + public function testAddCollection() |
94 | 87 | { |
95 | | - $data = array('client' => 'php'); |
96 | | - $collection = $this->collections->add('like', $this->activity_id, 'bob', $data); |
97 | | - $collectionId = $collection['id']; |
98 | | - $refId = $this->collections->createReference($collection['id']); |
99 | | - $this->assertSame($refId, 'SR:' . $collectionId); |
100 | | - $refObj = $this->collections->createReference($collection); |
101 | | - $this->assertSame($refObj, 'SR:' . $collectionId); |
| 88 | + $response = $this->collections->add( |
| 89 | + "food", ["name" => "Cheese Burger", "rating" => "4 stars"] |
| 90 | + ); |
| 91 | + $this->assertNotSame($response["id"], "cheese-burger"); |
| 92 | + $this->assertSame($response["collection"], 'food'); |
| 93 | + $this->assertSame($response["foreign_id"], "food:" . $response['id']); |
| 94 | + $this->assertSame($response['data']['name'], 'Cheese Burger'); |
102 | 95 | } |
103 | 96 |
|
104 | | - public function testAddChildCollection() |
| 97 | + public function testAddCollectionWithId() |
105 | 98 | { |
106 | | - $initial_collection = $this->collections->add('like', $this->activity_id, 'bob'); |
107 | | - $child_collection = $this->collections->addChild('like', $initial_collection['id'], 'alice'); |
108 | | - $this->assertSame($child_collection['user_id'], 'alice'); |
109 | | - $this->assertSame($initial_collection['user_id'], 'bob'); |
110 | | - $this->assertSame($child_collection['kind'], 'like'); |
111 | | - $this->assertSame($child_collection['activity_id'], $this->activity_id); |
112 | | - $this->assertSame($child_collection['parent'], $initial_collection['id']); |
| 99 | + $response = $this->collections->add( |
| 100 | + "food", ["name" => "Cheese Burger", "rating" => "4 stars"], "cheese-burger" |
| 101 | + ); |
| 102 | + $this->assertSame($response["id"], "cheese-burger"); |
| 103 | + $this->assertSame($response["collection"], "food"); |
| 104 | + $this->assertSame($response["foreign_id"], "food:cheese-burger"); |
| 105 | + $this->assertSame($response['data']['name'], 'Cheese Burger'); |
113 | 106 | } |
114 | 107 |
|
115 | | - public function testAddTargetFeedsCollection() |
| 108 | + /** |
| 109 | + * @expectedException \GetStream\Stream\StreamFeedException |
| 110 | + */ |
| 111 | + public function testAddCollectionAgain() |
116 | 112 | { |
117 | | - $target_feeds = array($this->aggregated2->getId(), $this->aggregated3->getId()); |
118 | | - $collection = $this->collections->add('like', $this->activity_id, 'bob', null, $target_feeds); |
119 | | - $this->assertSame($collection['user_id'], 'bob'); |
120 | | - $this->assertSame($collection['kind'], 'like'); |
121 | | - $this->assertSame($collection['activity_id'], $this->activity_id); |
122 | | - $response = $this->aggregated2->getActivities($offset=0, $limit=3); |
123 | | - // check a targeted feed |
124 | | - $latest_activity = $response["results"][0]['activities'][0]; |
125 | | - $this->assertSame( |
126 | | - $latest_activity["collection"], |
127 | | - $this->collections->createReference($collection) |
| 113 | + // Adding again should throw error |
| 114 | + $response = $this->collections->add( |
| 115 | + "food", ["name" => "Cheese Burger", "rating" => "4 stars"], "cheese-burger" |
128 | 116 | ); |
129 | | - $this->assertSame($latest_activity["verb"], "like"); |
130 | 117 | } |
131 | 118 |
|
132 | | - public function testGetCollection(){ |
133 | | - $created_collection = $this->collections->add('like', $this->activity_id, 'bob'); |
134 | | - $retrieved_collection = $this->collections->get($created_collection['id']); |
135 | | - $this->assertSame($created_collection['id'], $retrieved_collection['id']); |
136 | | - $this->assertSame($created_collection['user_id'], $retrieved_collection['user_id']); |
137 | | - $this->assertSame($created_collection['kind'], $retrieved_collection['kind']); |
138 | | - $this->assertSame($created_collection['created_at'], $retrieved_collection['created_at']); |
| 119 | + public function testDeleteCollection() |
| 120 | + { |
| 121 | + $response = $this->collections->delete("food","cheese-burger"); |
139 | 122 | } |
140 | 123 |
|
141 | 124 | /** |
142 | 125 | * @expectedException \GetStream\Stream\StreamFeedException |
143 | 126 | */ |
144 | | - public function testDeleteCollection(){ |
145 | | - $created_collection = $this->collections->add('like', $this->activity_id, 'bob'); |
146 | | - $retrieved_collection = $this->collections->get($created_collection['id']); |
147 | | - $this->collections->delete($created_collection['id']); |
148 | | - $retrieved_collection = $this->collections->get($created_collection['id']); |
| 127 | + public function testDeleteCollectionAgain() |
| 128 | + { |
| 129 | + $response = $this->collections->delete("food","cheese-burger"); |
| 130 | + } |
| 131 | + |
| 132 | + public function testCreateReference() |
| 133 | + { |
| 134 | + $refId = $this->collections->createReference("item", "42"); |
| 135 | + $this->assertSame($refId, 'SO:item:42'); |
| 136 | + } |
| 137 | + |
| 138 | + public function testGetCollection(){ |
| 139 | + $created_collection = $this->collections->add( |
| 140 | + "food", ["name" => "Cheese Burger", "rating" => "4 stars"] |
| 141 | + ); |
| 142 | + $retrieved_collection = $this->collections->get('food', $created_collection['id']); |
| 143 | + $this->assertSame($created_collection['id'], $retrieved_collection['id']); |
| 144 | + $this->assertSame($created_collection['data']['name'], $retrieved_collection['data']['name']); |
| 145 | + $this->assertSame($created_collection['collection'], $retrieved_collection['collection']); |
| 146 | + $this->assertSame($created_collection['created_at'], $retrieved_collection['created_at']); |
149 | 147 | } |
150 | 148 |
|
151 | 149 | public function testUpdateCollection(){ |
152 | | - $data = array('client' => 'php'); |
153 | | - $created_collection = $this->collections->add('unlike', $this->activity_id, 'bob', $data); |
154 | | - $retrieved_collection = $this->collections->get($created_collection['id']); |
155 | | - $updated_data = array('client' => 'updated-php', 'more' => 'kets'); |
156 | | - $updated_collection = $this->collections->update($created_collection['id'], $updated_data); |
157 | | - $this->assertSame($retrieved_collection['data'], $data); |
158 | | - $this->assertSame($updated_collection['data'], $updated_data); |
| 150 | + $created_collection = $this->collections->add( |
| 151 | + "food", ["name" => "Cheese Burger", "rating" => "4 stars"] |
| 152 | + ); |
| 153 | + $response = $this->collections->update("food", $created_collection['id'], ["name" => "Cheese Burger", "rating" => "1 stars"]); |
| 154 | + $this->assertSame($response['data']['rating'], '1 stars'); |
| 155 | + $this->assertSame($response['data']['name'], 'Cheese Burger'); |
159 | 156 | } |
160 | 157 |
|
161 | 158 | public function testFilterCollection(){ |
162 | | - $collections = $this->collections->filter('user_id', 'bob', 'like'); |
163 | | - foreach($collections['results'] as $collection){ |
164 | | - $this->assertSame($collection['kind'], 'like'); |
165 | | - $this->assertSame($collection['user_id'], 'bob'); |
166 | | - } |
167 | | - $collections = $this->collections->filter('user_id', 'bob', 'unlike'); |
168 | | - foreach($collections['results'] as $collection){ |
169 | | - $this->assertSame($collection['kind'], 'unlike'); |
170 | | - $this->assertSame($collection['user_id'], 'bob'); |
171 | | - } |
| 159 | + $created_collection = $this->collections->add( |
| 160 | + "food", ["name" => "Cheese Burger", "rating" => "4 stars"], 'cheese-burger' |
| 161 | + ); |
| 162 | + $response = $this->collections->select('food', ['cheese-burger', '124']); |
| 163 | + $this->assertSame($response['response']['data'][0]['id'], 'cheese-burger'); |
| 164 | + $response = $this->collections->delete("food","cheese-burger"); |
| 165 | + } |
| 166 | + |
| 167 | + public function testDeleteManyCollection() |
| 168 | + { |
| 169 | + // Adding again should throw error |
| 170 | + $response = $this->collections->add( |
| 171 | + "food", ["name" => "Cheese Burger", "rating" => "4 stars"], "cheese-burger-1" |
| 172 | + ); |
| 173 | + $response = $this->collections->add( |
| 174 | + "food", ["name" => "Cheese Burger", "rating" => "4 stars"], "cheese-burger-2" |
| 175 | + ); |
| 176 | + $response = $this->collections->deleteMany("food",["cheese-burger-1", "cheese-burger-2"]); |
| 177 | + |
172 | 178 | } |
173 | 179 |
|
174 | 180 | } |
0 commit comments