Skip to content

Commit

Permalink
test: Add test for PostRepo createMany with media values
Browse files Browse the repository at this point in the history
  • Loading branch information
rjmackay committed Dec 19, 2018
1 parent 6b072d3 commit eeb8d8d
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 4 deletions.
32 changes: 32 additions & 0 deletions tests/DatabaseTransactions.php
Expand Up @@ -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)
));
}
}
89 changes: 85 additions & 4 deletions tests/unit/App/Repository/PostRepositoryTest.php
Expand Up @@ -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')
Expand All @@ -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
Expand Down Expand Up @@ -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
]);
}
}

0 comments on commit eeb8d8d

Please sign in to comment.