Skip to content
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

vendor model and it's relationship #36

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Model/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -448,4 +448,19 @@ public function tags(): BelongsToMany
'term_taxonomy_id'
);
}

/**
* Get the related vendors.
*
* @return BelongsToMany<Vendor>
*/
public function vendor()
{
return $this->belongsToMany(
Vendor::class,
'term_relationships',
'object_id',
'term_taxonomy_id'
);
}
}
27 changes: 27 additions & 0 deletions src/Model/Vendor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Corcel\WooCommerce\Model;

use Corcel\Model\Taxonomy;
use Database\Factories\VendorFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;

/**
* @property-read string name
*/
class Vendor extends Taxonomy
{
use HasFactory;

protected $taxonomy = 'wcpv_product_vendors';

/**
* Create a new factory instance for the model.
*
* @return VendorFactory
*/
protected static function newFactory(): VendorFactory
{
return VendorFactory::new();
}
}
39 changes: 39 additions & 0 deletions tests/Unit/Model/ProductVendorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Tests\Unit\Model;

use Corcel\WooCommerce\Model\Product;
use Corcel\WooCommerce\Model\Vendor;
use Database\Factories\TermFactory;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class ProductVendorTest extends TestCase
{
public function testProductVendor(): void
{
$product = $this->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;
}
}
29 changes: 29 additions & 0 deletions tests/database/factories/TermFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Database\Factories;

use Corcel\Model\Term;
use Corcel\WooCommerce\Model\Product;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends Factory<Product>
*/
class TermFactory extends Factory
{
protected $model = Term::class;

/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'name' => $this->faker->name
];
}
}
29 changes: 29 additions & 0 deletions tests/database/factories/VendorFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Database\Factories;

use Corcel\WooCommerce\Model\Product;
use Corcel\WooCommerce\Model\Vendor;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends Factory<Product>
*/
class VendorFactory extends Factory
{
protected $model = Vendor::class;

/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'taxonomy' => 'wcpv_product_vendors',
'description' => '',
];
}
}