Skip to content

Commit 0cbde2f

Browse files
committed
fix collection tests
1 parent 785b1f9 commit 0cbde2f

1 file changed

Lines changed: 80 additions & 74 deletions

File tree

tests/integration/CollectionTest.php

Lines changed: 80 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use GetStream\Stream\Feed;
1010
use PHPUnit\Framework\TestCase;
1111
use Ramsey\Uuid\Uuid;
12+
use GuzzleHttp\Exception\ClientException;
1213

1314
class CollectionTest extends TestCase
1415
{
@@ -64,111 +65,116 @@ protected function setUp()
6465
$this->client->timeout = 10000;
6566
$this->user1 = $this->client->feed('user', Uuid::uuid4());
6667
$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'];
7368
$this->collections = $this->client->collections();
7469
}
7570

71+
public function cleanUp(){
72+
try {
73+
$this->collections->delete("food","cheese-burger");
74+
} catch(ClientException $e) {
75+
// pass
76+
}
77+
}
78+
7679
public function testUpsert()
7780
{
7881
$collection = $this->collections->upsert('animals', ['id' => '1', 'name' => 'bear', 'color' => 'blue']);
7982
$collection = $this->collections->upsert('items', ['id' => '42', 'name' => 'towel']);
8083

8184
}
8285

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()
9487
{
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');
10295
}
10396

104-
public function testAddChildCollection()
97+
public function testAddCollectionWithId()
10598
{
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');
113106
}
114107

115-
public function testAddTargetFeedsCollection()
108+
/**
109+
* @expectedException \GetStream\Stream\StreamFeedException
110+
*/
111+
public function testAddCollectionAgain()
116112
{
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"
128116
);
129-
$this->assertSame($latest_activity["verb"], "like");
130117
}
131118

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");
139122
}
140123

141124
/**
142125
* @expectedException \GetStream\Stream\StreamFeedException
143126
*/
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']);
149147
}
150148

151149
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');
159156
}
160157

161158
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+
172178
}
173179

174180
}

0 commit comments

Comments
 (0)