|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace GetStream\Integration; |
| 4 | + |
| 5 | +use DateTime; |
| 6 | +use DateTimeZone; |
| 7 | +use Firebase\JWT\JWT; |
| 8 | +use GetStream\Stream\Client; |
| 9 | +use GetStream\Stream\Feed; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use Ramsey\Uuid\Uuid; |
| 12 | + |
| 13 | +class CollectionTest extends TestCase |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @var Client |
| 17 | + */ |
| 18 | + protected $client; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var Feed |
| 22 | + */ |
| 23 | + protected $user1; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var Feed |
| 27 | + */ |
| 28 | + protected $user2; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var Feed |
| 32 | + */ |
| 33 | + protected $aggregated2; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var Feed |
| 37 | + */ |
| 38 | + protected $aggregated3; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var Feed |
| 42 | + */ |
| 43 | + protected $flat3; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var string |
| 47 | + */ |
| 48 | + protected $activity_id; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var Collections |
| 52 | + */ |
| 53 | + protected $collections; |
| 54 | + |
| 55 | + protected function setUp() |
| 56 | + { |
| 57 | + $this->client = new Client( |
| 58 | + getenv('STREAM_API_KEY'), |
| 59 | + getenv('STREAM_API_SECRET'), |
| 60 | + 'v1.0', |
| 61 | + getenv('STREAM_REGION') |
| 62 | + ); |
| 63 | + $this->client->setLocation('qa'); |
| 64 | + $this->client->timeout = 10000; |
| 65 | + $this->user1 = $this->client->feed('user', Uuid::uuid4()); |
| 66 | + $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 | + $this->collections = $this->client->collections(); |
| 74 | + } |
| 75 | + |
| 76 | + public function testUpsert() |
| 77 | + { |
| 78 | + $collection = $this->collections->upsert('animals', ['id' => '1', 'name' => 'bear', 'color' => 'blue']); |
| 79 | + $collection = $this->collections->upsert('items', ['id' => '42', 'name' => 'towel']); |
| 80 | + |
| 81 | + } |
| 82 | + |
| 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() |
| 94 | + { |
| 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); |
| 102 | + } |
| 103 | + |
| 104 | + public function testAddChildCollection() |
| 105 | + { |
| 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']); |
| 113 | + } |
| 114 | + |
| 115 | + public function testAddTargetFeedsCollection() |
| 116 | + { |
| 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) |
| 128 | + ); |
| 129 | + $this->assertSame($latest_activity["verb"], "like"); |
| 130 | + } |
| 131 | + |
| 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']); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * @expectedException \GetStream\Stream\StreamFeedException |
| 143 | + */ |
| 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']); |
| 149 | + } |
| 150 | + |
| 151 | + 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); |
| 159 | + } |
| 160 | + |
| 161 | + 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 | + } |
| 172 | + } |
| 173 | + |
| 174 | +} |
0 commit comments