From 7690913f1905b277cd01c0187f7b13d8c4978dae Mon Sep 17 00:00:00 2001 From: h_ebrahimzadeh Date: Tue, 11 Jul 2023 09:54:09 +0330 Subject: [PATCH 1/3] add vendor model and relation in product --- src/Model/Product.php | 15 +++++++++++++++ src/Model/Vendor.php | 27 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 src/Model/Vendor.php diff --git a/src/Model/Product.php b/src/Model/Product.php index d44271b..a7b3b84 100644 --- a/src/Model/Product.php +++ b/src/Model/Product.php @@ -448,4 +448,19 @@ public function tags(): BelongsToMany 'term_taxonomy_id' ); } + + /** + * Get the related tags. + * + * @return BelongsToMany + */ + public function vendor() + { + return $this->belongsToMany( + Vendor::class, + 'term_relationships', + 'object_id', + 'term_taxonomy_id' + ); + } } diff --git a/src/Model/Vendor.php b/src/Model/Vendor.php new file mode 100644 index 0000000..ce617ee --- /dev/null +++ b/src/Model/Vendor.php @@ -0,0 +1,27 @@ + Date: Tue, 11 Jul 2023 09:54:27 +0330 Subject: [PATCH 2/3] add vendor test --- tests/Unit/Model/ProductVendorTest.php | 39 ++++++++++++++++++++++ tests/database/factories/TermFactory.php | 29 ++++++++++++++++ tests/database/factories/VendorFactory.php | 29 ++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 tests/Unit/Model/ProductVendorTest.php create mode 100644 tests/database/factories/TermFactory.php create mode 100644 tests/database/factories/VendorFactory.php diff --git a/tests/Unit/Model/ProductVendorTest.php b/tests/Unit/Model/ProductVendorTest.php new file mode 100644 index 0000000..914cc7e --- /dev/null +++ b/tests/Unit/Model/ProductVendorTest.php @@ -0,0 +1,39 @@ +createProduct(); + $vendorName = 'vendor name'; + $vendorTerm = (new TermFactory())->create(['name' => $vendorName]); + $vendorTax = Vendor::factory()->create(['term_id' => $vendorTerm->term_id]); + + DB::table('term_relationships')->insert([ + 'object_id' => $product->ID, + 'term_taxonomy_id' => $vendorTax->term_taxonomy_id, + 'term_order' => 0, + ]); + + $this->assertModelExists($product->vendor->first()); + $this->assertSame($product->vendor->first()->name, $vendorName); + } + + private function createProduct(): Product + { + /** @var Product */ + $product = Product::factory()->create(); + + return $product; + } +} diff --git a/tests/database/factories/TermFactory.php b/tests/database/factories/TermFactory.php new file mode 100644 index 0000000..6995792 --- /dev/null +++ b/tests/database/factories/TermFactory.php @@ -0,0 +1,29 @@ + + */ +class TermFactory extends Factory +{ + protected $model = Term::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition() + { + return [ + 'name' => $this->faker->name + ]; + } +} diff --git a/tests/database/factories/VendorFactory.php b/tests/database/factories/VendorFactory.php new file mode 100644 index 0000000..5b10d3a --- /dev/null +++ b/tests/database/factories/VendorFactory.php @@ -0,0 +1,29 @@ + + */ +class VendorFactory extends Factory +{ + protected $model = Vendor::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition() + { + return [ + 'taxonomy' => 'wcpv_product_vendors', + 'description' => '', + ]; + } +} From f7d6b2352f304b891e230f68473c5833484a59bc Mon Sep 17 00:00:00 2001 From: h_ebrahimzadeh Date: Tue, 11 Jul 2023 19:13:01 +0330 Subject: [PATCH 3/3] correct php doc comment --- src/Model/Product.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/Product.php b/src/Model/Product.php index a7b3b84..746a4bd 100644 --- a/src/Model/Product.php +++ b/src/Model/Product.php @@ -450,7 +450,7 @@ public function tags(): BelongsToMany } /** - * Get the related tags. + * Get the related vendors. * * @return BelongsToMany */