mirror of
https://github.com/nothingworksinc/ticketbeast.git
synced 2026-01-26 03:04:05 +00:00
142 - Optional Files and the Null Object Pattern
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers\Backstage;
|
||||
|
||||
use App\Concert;
|
||||
use App\NullFile;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\Rule;
|
||||
@@ -55,7 +56,7 @@ class ConcertsController extends Controller
|
||||
'zip' => request('zip'),
|
||||
'ticket_price' => request('ticket_price') * 100,
|
||||
'ticket_quantity' => (int) request('ticket_quantity'),
|
||||
'poster_image_path' => request('poster_image')->store('posters', 's3'),
|
||||
'poster_image_path' => request('poster_image', new NullFile)->store('posters', 's3'),
|
||||
]);
|
||||
|
||||
return redirect()->route('backstage.concerts.index');
|
||||
|
||||
11
app/NullFile.php
Normal file
11
app/NullFile.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
class NullFile
|
||||
{
|
||||
public function store()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -440,4 +440,24 @@ class AddConcertTest extends TestCase
|
||||
$response->assertSessionHasErrors('poster_image');
|
||||
$this->assertEquals(0, Concert::count());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
function poster_image_is_optional()
|
||||
{
|
||||
$this->disableExceptionHandling();
|
||||
|
||||
$user = factory(User::class)->create();
|
||||
|
||||
$response = $this->actingAs($user)->post('/backstage/concerts', $this->validParams([
|
||||
'poster_image' => null,
|
||||
]));
|
||||
|
||||
tap(Concert::first(), function ($concert) use ($response, $user) {
|
||||
$response->assertRedirect('/backstage/concerts');
|
||||
|
||||
$this->assertTrue($concert->user->is($user));
|
||||
|
||||
$this->assertNull($concert->poster_image_path);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user