Add ingredients-based journal entries test

This commit is contained in:
Christopher C. Wells 2021-03-30 16:27:41 -07:00 committed by Christopher Charbonneau Wells
parent 29638c711f
commit 5c57af5951
1 changed files with 42 additions and 3 deletions

View File

@ -3,14 +3,16 @@
namespace Tests\Feature\Http\Controllers;
use App\Http\Controllers\JournalEntryController;
use App\Models\IngredientAmount;
use App\Models\JournalEntry;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
class JournalEntryControllerTest extends HttpControllerTestCase
{
use RefreshDatabase;
use RefreshDatabase, WithFaker;
/**
* @inheritdoc
@ -47,7 +49,8 @@ class JournalEntryControllerTest extends HttpControllerTestCase
/**
* @doesNotPerformAssertions
*/
public function testCanViewInstance(): void {
public function testCanViewInstance(): void
{
$this->setName('can *not* view instance');
// Journal entries are not independently viewable.
}
@ -55,7 +58,8 @@ class JournalEntryControllerTest extends HttpControllerTestCase
/**
* @doesNotPerformAssertions
*/
public function testCanEditInstance(): void {
public function testCanEditInstance(): void
{
$this->setName('can *not* edit instance');
// Journal entries are not editable.
}
@ -65,10 +69,45 @@ class JournalEntryControllerTest extends HttpControllerTestCase
$create_url = action([$this->class(), 'createFromNutrients']);
$response = $this->get($create_url);
$response->assertOk();
$instance = $this->factory()->make();
$store_url = action([$this->class(), 'storeFromNutrients']);
$response = $this->post($store_url, $instance->toArray());
$response->assertSessionHasNoErrors();
}
public function testCanAddInstanceFromIngredients(): void
{
$create_url = action([$this->class(), 'create']);
$response = $this->get($create_url);
$response->assertOk();
// Create ingredients based on ingredient amounts.
$ingredients = [
'date' => [], 'meal' => [], 'amount' => [], 'unit' => [],
'id' => [], 'type' => [],
];
/** @var \App\Models\IngredientAmount[] $ingredient_amounts */
$ingredient_amounts = IngredientAmount::factory()
->count(10)
->make(['parent_id' => null, 'parent_type' => null]);
foreach ($ingredient_amounts as $ingredient_amount) {
$ingredients['date'][] = $this->faker->dateTimeThisMonth;
$ingredients['meal'][] = $this->faker->randomElement(JournalEntry::meals()->pluck('value')->toArray());
$ingredients['amount'][] = $ingredient_amount->amount;
$ingredients['unit'][] = $ingredient_amount->unit;
$ingredients['id'][] = $ingredient_amount->ingredient->id;
$ingredients['type'][] = $ingredient_amount->ingredient->type;
}
$data = ['ingredients' => $ingredients, 'group_entries' => true];
$store_url = action([$this->class(), 'store']);
$response = $this->post($store_url, $data);
$response->assertSessionHasNoErrors();
$data['group_entries'] = false;
$store_url = action([$this->class(), 'store']);
$response = $this->post($store_url, $data);
$response->assertSessionHasNoErrors();
}
}