Skip to content

Commit

Permalink
Added UnknownPropertyException when passing an inexistent property …
Browse files Browse the repository at this point in the history
…slug
  • Loading branch information
fulopattila122 committed Apr 22, 2024
1 parent cf5beaa commit 96e6506
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
- Added the processing of the `payment_method_id` field to the OrderFactory (Foundation)
- Added the `BillpayerChanged` checkout event
- Added the `is_hidden` field to the `Property` model
- Changed the behavior of assignPropertyValues/assignPropertyValue methods so that it throws an `UnknownPropertyException` when passing an inexistent property slug
- Added the `withImages` and `withChannels` methods to the product search class (eager loads media)
- Added the `channelables` table for being many-to-many polymorphic relationships with channels and arbitrary models
- Added the `mapInto()` method to the `RelationAdjustmentCollection` class, which forwards the call to the underlying Eloquent collection
Expand Down
1 change: 1 addition & 0 deletions src/Properties/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- Added PHP 8.3 Support
- Added Laravel 11 Support
- Changed minimum Laravel version to v10.43
- Changed the behavior of assignPropertyValues/assignPropertyValue methods so that it throws an `UnknownPropertyException` when passing an inexistent property slug

## 3.x Series

Expand Down
25 changes: 25 additions & 0 deletions src/Properties/Exceptions/UnknownPropertyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

/**
* Contains the UnknownPropertyException class.
*
* @copyright Copyright (c) 2024 Vanilo UG
* @author Attila Fulop
* @license MIT
* @since 2024-04-22
*
*/

namespace Vanilo\Properties\Exceptions;

use RuntimeException;

class UnknownPropertyException extends RuntimeException
{
public static function createFromSlug(string $slug): self
{
return new self("The property `{$slug}` does not exist.");
}
}
12 changes: 12 additions & 0 deletions src/Properties/Tests/ModelPropertyValuesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace Vanilo\Properties\Tests;

use Illuminate\Database\Schema\Blueprint;
use Vanilo\Properties\Exceptions\UnknownPropertyException;
use Vanilo\Properties\Models\Property;
use Vanilo\Properties\Models\PropertyValue;
use Vanilo\Properties\Tests\Examples\Product;
Expand Down Expand Up @@ -146,6 +147,17 @@ public function multiple_values_can_be_assigned_to_a_model_by_scalar_value()
$this->assertEquals('Hua lu guy', $sword->valueOfProperty('shape')->getCastedValue());
}

/** @test */
public function attempting_to_assign_values_with_inexistent_properties_throws_an_exception()
{
$this->expectException(UnknownPropertyException::class);

$shelf = Product::create(['name' => 'Shelf']);
$shelf->assignPropertyValues([
'no-such-thing' => 'here',
]);
}

protected function setUpDatabase($app)
{
parent::setUpDatabase($app);
Expand Down
6 changes: 5 additions & 1 deletion src/Properties/Traits/HasPropertyValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Vanilo\Properties\Contracts\Property;
use Vanilo\Properties\Contracts\PropertyValue;
use Vanilo\Properties\Exceptions\UnknownPropertyException;
use Vanilo\Properties\Models\PropertyProxy;
use Vanilo\Properties\Models\PropertyValueProxy;

Expand All @@ -32,8 +33,11 @@ public function assignPropertyValue(string|Property $property, mixed $value): vo

$propertyValue = PropertyValueProxy::findByPropertyAndValue($property, $value);
if (null === $propertyValue) {
if (null === $propertyId = $property instanceof Property ? $property->id : PropertyProxy::findBySlug($property)?->id) {
throw UnknownPropertyException::createFromSlug($property);
}
$propertyValue = PropertyValueProxy::create([
'property_id' => $property instanceof Property ? $property->id : PropertyProxy::findBySlug($property)?->id,
'property_id' => $propertyId,
'value' => $value,
'title' => $value,
]);
Expand Down

0 comments on commit 96e6506

Please sign in to comment.