Skip to content

Commit

Permalink
Fix bug when creating slug from null value
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Jan 12, 2016
1 parent 2986996 commit 2b6fd8d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All Notable changes to `laravel-sluggable` will be documented in this file

## 1.0.2 - 2016-01-12

- Fix bug when creating slugs from null values

## 1.0.1 - 2015-12-27

- Fix Postgres bug
Expand Down
7 changes: 4 additions & 3 deletions src/HasSlug.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ protected function addSlug()
protected function getSlugSourceString() : string
{
$slugSourceString = collect($this->slugOptions->generateSlugFrom)
->map(function (string $fieldName) : string {
return $this->$fieldName;
})
->map(function (string $fieldName) : string
{
return $this->$fieldName ?? '';
})
->implode('-');

return substr($slugSourceString, 0, $this->slugOptions->maximumLength);
Expand Down
34 changes: 22 additions & 12 deletions tests/Integration/HasSlugTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ public function it_will_save_a_slug_when_saving_a_model()
$this->assertEquals('this-is-a-test', $model->url);
}

/**
* @test
*/
public function it_can_handle_null_values_when_creating_slugs()
{
$model = TestModel::create(['name' => null]);

$this->assertEquals('-1', $model->url);
}

/**
* @test
*/
Expand Down Expand Up @@ -73,12 +83,12 @@ public function it_can_generate_duplicate_slugs()
{
foreach (range(1, 10) as $i) {
$model = new class extends TestModel
{
public function getSlugOptions() : SlugOptions
{
return parent::getSlugOptions()->allowDuplicateSlugs();
}
};
{
public function getSlugOptions() : SlugOptions
{
return parent::getSlugOptions()->allowDuplicateSlugs();
}
};

$model->name = 'this is a test';
$model->save();
Expand All @@ -93,12 +103,12 @@ public function getSlugOptions() : SlugOptions
public function it_can_generate_slugs_with_a_maximum_length()
{
$model = new class extends TestModel
{
public function getSlugOptions() : SlugOptions
{
return parent::getSlugOptions()->slugsShouldBeNoLongerThan(5);
}
};
{
public function getSlugOptions() : SlugOptions
{
return parent::getSlugOptions()->slugsShouldBeNoLongerThan(5);
}
};

$model->name = '123456789';
$model->save();
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function setUpDatabase(Application $app)

$app['db']->connection()->getSchemaBuilder()->create('test_models', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('name')->nullable();
$table->string('other_field')->nullable();
$table->string('url')->nullable();
});
Expand Down

0 comments on commit 2b6fd8d

Please sign in to comment.