diff --git a/tests/DatabaseTransactions.php b/tests/DatabaseTransactions.php index cf02d7673d..48d3968fc8 100644 --- a/tests/DatabaseTransactions.php +++ b/tests/DatabaseTransactions.php @@ -82,4 +82,36 @@ protected function seeInOhanzeeDatabase($table, array $data) json_encode($data) )); } + + /** + * Assert that a given where condition exists in the database. + * + * We have to use a custom version because the transaction is isolated + * to the individual connection + * + * @param string $table + * @param array $data + * @param string|null $onConnection + * @return $this + */ + protected function seeCountInOhanzeeDatabase($table, array $data, $assertCount) + { + $query = DB::select([DB::expr('COUNT(*)'), 'total']) + ->from($table); + + foreach ($data as $column => $value) { + $predicate = is_array($value) ? 'IN' : '='; + $query->where($column, $predicate, $value); + } + + $count = (int) $query + ->execute($this->database) + ->get('total', 0); + + $this->assertEquals($assertCount, $count, sprintf( + 'Count in database table [%s] doesnt match for attributes [%s].', + $table, + json_encode($data) + )); + } } diff --git a/tests/unit/App/Repository/PostRepositoryTest.php b/tests/unit/App/Repository/PostRepositoryTest.php index 8bd973baae..e0c8f61851 100644 --- a/tests/unit/App/Repository/PostRepositoryTest.php +++ b/tests/unit/App/Repository/PostRepositoryTest.php @@ -29,6 +29,7 @@ public function setUp() ->values(['test-location', 'point', 'location', 'location']) ->values(['test-tags', 'tags', 'tags', 'categories']) ->values(['test-test', 'varchar', 'text', 'some text']) + ->values(['test-media', 'media', 'upload', 'photos']) ->execute($this->database); DB::insert('tags') @@ -37,14 +38,15 @@ public function setUp() ->values(['test-tag2', 'test-tag2', 99992]) ->values(['test-tag3-test', 'test-tag3', 99993]) ->execute($this->database); + + DB::insert('media') + ->columns(['o_filename', 'caption', 'id', 'mime', 'o_size']) + ->values(['junk.png', 'Junk', 8889, 'image/png', 0]) + ->execute($this->database); } public function testCreateMany() { - // Do stuff - // Check data in DB - // rollback transaction - $faker = Faker\Factory::create(); // Generate post data @@ -158,4 +160,83 @@ public function testCreateMany() 'value' => 'text3' ]); } + + public function testCreateManyWithMedia() + { + $faker = Faker\Factory::create(); + + // Generate post data + $post1 = new Post([ + 'title' => $faker->sentence, + 'context' => $faker->paragraph, + 'type' => 'report', + 'status' => 'published', + 'locale' => 'en_US', + 'values' => [ + 'test-location' => [[ + 'lat' => 1, + 'lon' => 2 + ]], + 'test-tags' => [99991,99993], + 'test-test' => ['text'], + 'test-media' => [ + [ + 'o_filename' => 'somefile.png', + 'caption' => 'a title', + 'mime' => 'image/png', + ] + ] + ] + ]); + $post2 = new Post([ + 'title' => $faker->sentence, + 'context' => $faker->paragraph, + 'type' => 'report', + 'status' => 'published', + 'locale' => 'en_US', + 'values' => [ + 'test-location' => [[ + 'lat' => 2, + 'lon' => 4 + ]], + 'test-tags' => [99992], + 'test-test' => ['text2'], + 'test-media' => [ + [ + 'o_filename' => 'somefile2.png', + 'caption' => 'a title', + 'mime' => 'image/png', + ], + 8889 + ] + ] + ]); + + $repo = service('repository.post'); + $inserted = $repo->createMany(collect([ + $post1, + $post2 + ])); + + $this->assertCount(2, $inserted); + $this->seeInOhanzeeDatabase('posts', [ + 'id' => $inserted[0], + 'title' => $post1->title, + ]); + $this->seeInOhanzeeDatabase('posts', [ + 'id' => $inserted[1], + 'title' => $post2->title, + ]); + + $this->seeCountInOhanzeeDatabase('post_media', [ + 'post_id' => $inserted[0], + ], 1); + $this->seeCountInOhanzeeDatabase('post_media', [ + 'post_id' => $inserted[1], + ], 2); + $this->seeInOhanzeeDatabase('post_media', [ + 'post_id' => $inserted[1], + 'value' => 8889 + ]); + } }