106 - Converting Empty Strings to Null

This commit is contained in:
Adam Wathan
2017-05-19 14:30:50 -04:00
parent a3aaa98ce0
commit cdc9b81a56
4 changed files with 62 additions and 1 deletions

View File

@@ -15,6 +15,9 @@ class Kernel extends HttpKernel
*/
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];
/**

View File

@@ -0,0 +1,18 @@
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\TrimStrings as BaseTrimmer;
class TrimStrings extends BaseTrimmer
{
/**
* The names of the attributes that should not be trimmed.
*
* @var array
*/
protected $except = [
'password',
'password_confirmation',
];
}

View File

@@ -16,7 +16,7 @@ class CreateConcertsTable extends Migration
Schema::create('concerts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('subtitle');
$table->string('subtitle')->nullable();
$table->datetime('date');
$table->integer('ticket_price');
$table->string('venue');

View File

@@ -125,4 +125,44 @@ class AddConcertTest extends TestCase
$response->assertSessionHasErrors('title');
$this->assertEquals(0, Concert::count());
}
/** @test */
function subtitle_is_optional()
{
$this->disableExceptionHandling();
$user = factory(User::class)->create();
$response = $this->actingAs($user)->post('/backstage/concerts', [
'title' => 'No Warning',
'subtitle' => '',
'additional_information' => "You must be 19 years of age to attend this concert.",
'date' => '2017-11-18',
'time' => '8:00pm',
'venue' => 'The Mosh Pit',
'venue_address' => '123 Fake St.',
'city' => 'Laraville',
'state' => 'ON',
'zip' => '12345',
'ticket_price' => '32.50',
'ticket_quantity' => '75',
]);
tap(Concert::first(), function ($concert) use ($response) {
$response->assertStatus(302);
$response->assertRedirect("/concerts/{$concert->id}");
$this->assertEquals('No Warning', $concert->title);
$this->assertNull($concert->subtitle);
$this->assertEquals("You must be 19 years of age to attend this concert.", $concert->additional_information);
$this->assertEquals(Carbon::parse('2017-11-18 8:00pm'), $concert->date);
$this->assertEquals('The Mosh Pit', $concert->venue);
$this->assertEquals('123 Fake St.', $concert->venue_address);
$this->assertEquals('Laraville', $concert->city);
$this->assertEquals('ON', $concert->state);
$this->assertEquals('12345', $concert->zip);
$this->assertEquals(3250, $concert->ticket_price);
$this->assertEquals(75, $concert->ticketsRemaining());
});
}
}