Skip to content

Commit

Permalink
Merge pull request #192 from vanilophp/feat-add-gtin-to-products
Browse files Browse the repository at this point in the history
Added GTIN field to products and master product variants tables
  • Loading branch information
fulopattila122 authored Feb 3, 2025
2 parents 14e495d + 7d48d99 commit abba6fa
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
## 5.x Series

## Unreleased
##### 2024-04-XX
##### 2025-XX-XX

- Dropped PHP 8.2 Support
- Dropped Laravel 10 Support
- Added Laravel 12 Support
- Added the `gtin` field to the products and master product variants tables

## 4.x Series

Expand Down
38 changes: 38 additions & 0 deletions src/MasterProduct/Tests/Unit/MasterProductVariantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,42 @@ public function a_variant_does_not_have_meta_description()

$this->assertNull($variant->meta_description);
}

/** @test */
public function the_variant_gtin_field_can_be_set(): void
{
$master = MasterProduct::create([
'name' => 'Kosmodisk',
]);

$variant = MasterProductVariant::create([
'master_product_id' => $master->id,
'sku' => 'kosmodisk',
'gtin' => '777888999'
]);

$this->assertEquals('777888999', $variant->gtin);
}

/** @test */
public function the_variant_gtin_field_is_nullable(): void
{
$master = MasterProduct::create([
'name' => 'Kosmodisk',
]);

$variant = MasterProductVariant::create([
'master_product_id' => $master->id,
'sku' => 'kosmodisk',
'gtin' => '777888999'
]);

$variant->update([
'gtin' => null,
]);

$variant->fresh();

$this->assertNull($variant->gtin);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('master_product_variants', function (Blueprint $table) {
$table->string('gtin')->nullable();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('master_product_variants', function (Blueprint $table) {
$table->dropColumn('gtin');
});
}
};
22 changes: 21 additions & 1 deletion src/Product/Tests/BaseProductAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public function all_fields_can_be_properly_set()
'description' => 'Maxi Baxi 2000 makes your dreams come true. See: https://youtu.be/5RKM_VLEbOc',
'state' => 'active',
'meta_keywords' => 'maxi, baxi, dreams',
'meta_description' => 'The THING you always have dreamt of'
'meta_description' => 'The THING you always have dreamt of',
'gtin' => '8675309',
]);

$this->assertGreaterThanOrEqual(1, $product->id);
Expand All @@ -80,6 +81,7 @@ public function all_fields_can_be_properly_set()
$this->assertEquals('active', $product->state->value());
$this->assertEquals('maxi, baxi, dreams', $product->meta_keywords);
$this->assertEquals('The THING you always have dreamt of', $product->meta_description);
$this->assertEquals('8675309', $product->gtin);
}

/**
Expand Down Expand Up @@ -160,4 +162,22 @@ public function find_by_sku_returns_null_if_no_product_was_found_by_the_requeste
{
$this->assertNull(Product::findBySku('Oh man such SKU could not exist in the Wild. Nor in a lab'));
}

/** @test */
public function the_product_gtin_field_is_nullable(): void
{
$product = Product::create([
'name' => 'Kosmodisk',
'sku' => 'kosmodisk',
'gtin' => '9876543210',
]);

$product->update([
'gtin' => null,
]);

$product->fresh();

$this->assertNull($product->gtin);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('products', function (Blueprint $table) {
$table->string('gtin')->nullable();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('products', function (Blueprint $table) {
$table->dropColumn('gtin');
});
}
};

0 comments on commit abba6fa

Please sign in to comment.