-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add exception for models with conflicting url/urls columns #154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Support\Facades\Schema; | ||
use Vlados\LaravelUniqueUrls\LaravelUniqueUrlsController; | ||
use Vlados\LaravelUniqueUrls\Models\Url; | ||
use Vlados\LaravelUniqueUrls\Tests\Models\ChildModel; | ||
|
@@ -169,3 +171,81 @@ | |
expect($model->relative_url)->toEqual(app()->getLocale()."/parent/test".($i > 0 ? "_".$i : "")); | ||
} | ||
}); | ||
|
||
test('12. Check if exception is thrown when model has conflicting url column', function () { | ||
// Create a table with 'url' column | ||
Schema::create('models_with_url_column', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->string('name'); | ||
$table->string('url'); | ||
}); | ||
|
||
// Create a model class that uses HasUniqueUrls trait | ||
// The exception should be thrown during model instantiation | ||
expect(function () { | ||
$modelClass = new class () extends \Illuminate\Database\Eloquent\Model { | ||
use \Vlados\LaravelUniqueUrls\HasUniqueUrls; | ||
|
||
protected $table = 'models_with_url_column'; | ||
protected $guarded = []; | ||
public $timestamps = false; | ||
|
||
public function urlHandler(): array | ||
{ | ||
return [ | ||
'controller' => \Vlados\LaravelUniqueUrls\Tests\TestUrlHandler::class, | ||
'method' => 'view', | ||
'arguments' => [], | ||
]; | ||
} | ||
|
||
public function urlStrategy($language, $locale): string | ||
{ | ||
return \Illuminate\Support\Str::slug($this->name); | ||
} | ||
}; | ||
|
||
$modelClass::create(['name' => 'test', 'url' => 'test-url']); | ||
})->toThrow(Exception::class, "has a conflicting column 'url'"); | ||
|
||
Schema::dropIfExists('models_with_url_column'); | ||
}); | ||
Comment on lines
+175
to
+212
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major 🧩 Analysis chainVerify exception timing and improve test reliability. Several concerns with this test:
Consider this refactor to make the test more explicit and reliable: test('12. Check if exception is thrown when model has conflicting url column', function () {
// Create a table with 'url' column
Schema::create('models_with_url_column', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('url');
});
- // Create a model class that uses HasUniqueUrls trait
- // The exception should be thrown during model instantiation
- expect(function () {
+ try {
+ // The exception should be thrown during model instantiation
$modelClass = new class () extends \Illuminate\Database\Eloquent\Model {
use \Vlados\LaravelUniqueUrls\HasUniqueUrls;
protected $table = 'models_with_url_column';
protected $guarded = [];
public $timestamps = false;
public function urlHandler(): array
{
return [
'controller' => \Vlados\LaravelUniqueUrls\Tests\TestUrlHandler::class,
'method' => 'view',
'arguments' => [],
];
}
public function urlStrategy($language, $locale): string
{
return \Illuminate\Support\Str::slug($this->name);
}
};
-
- $modelClass::create(['name' => 'test', 'url' => 'test-url']);
- })->toThrow(Exception::class, "has a conflicting column 'url'");
-
- Schema::dropIfExists('models_with_url_column');
+
+ // If we reach here, no exception was thrown during instantiation
+ // Try to trigger it via create() as fallback
+ $modelClass::create(['name' => 'test', 'url' => 'test-url']);
+
+ throw new \Exception('Expected exception was not thrown');
+ } catch (\Exception $e) {
+ expect($e->getMessage())->toContain("has a conflicting column 'url'");
+ } finally {
+ Schema::dropIfExists('models_with_url_column');
+ }
}); Alternatively, verify the exact behavior by running: Also verify the exception type being thrown: 🏁 Script executed: #!/bin/bash
# Check the HasUniqueUrls trait implementation to confirm when the exception is thrown
ast-grep --pattern $'trait HasUniqueUrls {
$$$
initializeHasUniqueUrls($$$) {
$$$
}
$$$
}' Length of output: 0 🏁 Script executed: #!/bin/bash
# Search for the exception being thrown for conflicting columns
rg -nP --type=php -A3 -B3 "throw.*conflicting column" Length of output: 0 🏁 Script executed: #!/bin/bash
# Locate the HasUniqueUrls trait file
rg -lP '\btrait\s+HasUniqueUrls' -g '*.php'
# Inspect the first match if found
FILE=$(rg -lP '\btrait\s+HasUniqueUrls' -g '*.php' | head -n1)
echo "Trait file: $FILE"
if [ -n "$FILE" ]; then
echo "=== File Header ==="
sed -n '1,200p' "$FILE"
echo "=== initializeHasUniqueUrls ==="
rg -n "function initializeHasUniqueUrls" -C3 "$FILE"
echo "=== throw statements ==="
rg -n "throw new" -C3 "$FILE"
fi Length of output: 5092 Clarify exception timing and enforce cleanup
🤖 Prompt for AI Agents
|
||
|
||
test('13. Check if exception is thrown when model has conflicting urls column', function () { | ||
// Create a table with 'urls' column | ||
Schema::create('models_with_urls_column', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->string('name'); | ||
$table->string('urls'); | ||
}); | ||
|
||
// Create a model class that uses HasUniqueUrls trait | ||
// The exception should be thrown during model instantiation | ||
expect(function () { | ||
$modelClass = new class () extends \Illuminate\Database\Eloquent\Model { | ||
use \Vlados\LaravelUniqueUrls\HasUniqueUrls; | ||
|
||
protected $table = 'models_with_urls_column'; | ||
protected $guarded = []; | ||
public $timestamps = false; | ||
|
||
public function urlHandler(): array | ||
{ | ||
return [ | ||
'controller' => \Vlados\LaravelUniqueUrls\Tests\TestUrlHandler::class, | ||
'method' => 'view', | ||
'arguments' => [], | ||
]; | ||
} | ||
|
||
public function urlStrategy($language, $locale): string | ||
{ | ||
return \Illuminate\Support\Str::slug($this->name); | ||
} | ||
}; | ||
|
||
$modelClass::create(['name' => 'test', 'urls' => 'test-urls']); | ||
})->toThrow(Exception::class, "has a conflicting column 'urls'"); | ||
|
||
Schema::dropIfExists('models_with_urls_column'); | ||
}); | ||
Comment on lines
+214
to
+251
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same concerns as test 12, plus code duplication. This test has the same structural issues as test 12:
Consider extracting a helper function or using Pest's dataset feature to reduce duplication: test('12. Check if exception is thrown when model has conflicting url column', function () {
testConflictingColumn('url');
});
test('13. Check if exception is thrown when model has conflicting urls column', function () {
testConflictingColumn('urls');
});
function testConflictingColumn(string $columnName): void
{
$tableName = "models_with_{$columnName}_column";
Schema::create($tableName, function (Blueprint $table) use ($columnName) {
$table->increments('id');
$table->string('name');
$table->string($columnName);
});
try {
$modelClass = new class ($tableName) extends \Illuminate\Database\Eloquent\Model {
use \Vlados\LaravelUniqueUrls\HasUniqueUrls;
protected $guarded = [];
public $timestamps = false;
public function __construct(string $table = null, array $attributes = [])
{
if ($table) {
$this->table = $table;
}
parent::__construct($attributes);
}
public function urlHandler(): array
{
return [
'controller' => \Vlados\LaravelUniqueUrls\Tests\TestUrlHandler::class,
'method' => 'view',
'arguments' => [],
];
}
public function urlStrategy($language, $locale): string
{
return \Illuminate\Support\Str::slug($this->name);
}
};
$modelClass::create(['name' => 'test', $columnName => "test-{$columnName}"]);
throw new \Exception('Expected exception was not thrown');
} catch (\Exception $e) {
expect($e->getMessage())->toContain("has a conflicting column '{$columnName}'");
} finally {
Schema::dropIfExists($tableName);
}
} Or use Pest's dataset feature: test('check if exception is thrown when model has conflicting column', function (string $column) {
// test implementation using $column
})->with(['url', 'urls']); 🤖 Prompt for AI Agents
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update the misleading comment on line 42.
The comment states "Check if attribute exists in fillable, guarded, or as a database column" but the implementation only checks database columns via
hasColumn()
. This could confuse future maintainers.Apply this diff to fix the comment:
Alternatively, if you intended to also check
fillable
andguarded
arrays, consider adding those checks. For example:However, checking only database columns is likely sufficient for this use case since that's where the actual conflict would occur.
🧰 Tools
🪛 GitHub Actions: run-tests
[error] 44-48: Model [{$modelClass}] has a conflicting column '{$attribute}'. The HasUniqueUrls trait uses 'urls' as a relationship name and provides 'relative_url' and 'absolute_url' attributes. Please rename the '{$attribute}' column in your model to avoid conflicts.