Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
repl6669 committed Apr 5, 2024
1 parent 55d8a03 commit ee1440c
Show file tree
Hide file tree
Showing 8 changed files with 563 additions and 2 deletions.
119 changes: 119 additions & 0 deletions tests/Stubs/Lunar/TestTaxDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace Dystcz\LunarRewards\Tests\Stubs\Lunar;

use Lunar\Base\Addressable;
use Lunar\Base\Purchasable;
use Lunar\Base\TaxDriver;
use Lunar\Base\ValueObjects\Cart\TaxBreakdown;
use Lunar\Base\ValueObjects\Cart\TaxBreakdownAmount;
use Lunar\DataTypes\Price;
use Lunar\Models\CartLine;
use Lunar\Models\Currency;
use Lunar\Models\ProductVariant;
use Lunar\Models\TaxRateAmount;

class TestTaxDriver implements TaxDriver
{
/**
* The taxable shipping address.
*/
protected ?Addressable $shippingAddress = null;

/**
* The taxable billing address.
*/
protected ?Addressable $billingAddress = null;

/**
* The currency model.
*/
protected Currency $currency;

/**
* The purchasable item.
*/
protected Purchasable $purchasable;

/**
* The cart line model.
*/
protected CartLine $cartLine;

/**
* {@inheritDoc}
*/
public function setShippingAddress(?Addressable $address = null): self
{
$this->shippingAddress = $address;

return $this;
}

/**
* {@inheritDoc}
*/
public function setCurrency(Currency $currency): self
{
$this->currency = $currency;

return $this;
}

/**
* {@inheritDoc}
*/
public function setBillingAddress(?Addressable $address = null): self
{
$this->billingAddress = $address;

return $this;
}

/**
* {@inheritDoc}
*/
public function setPurchasable(Purchasable $purchasable): self
{
$this->purchasable = $purchasable;

return $this;
}

/**
* Set the cart line.
*/
public function setCartLine(CartLine $cartLine): self
{
$this->cartLine = $cartLine;

return $this;
}

/**
* {@inheritDoc}
*/
public function getBreakdown($subTotal): TaxBreakdown
{
$breakdown = new TaxBreakdown;

$currency = Currency::first() ?: Currency::factory()->create();

$taxAmount = TaxRateAmount::factory()->create();

$result = round($subTotal * ($taxAmount->percentage / 100));

$variant = ProductVariant::factory()->create();

$amount = new TaxBreakdownAmount(
price: new Price((int) $result, $currency, $variant->getUnitQuantity()),
description: $taxAmount->taxRate->name,
identifier: "tax_rate_{$taxAmount->taxRate->id}",
percentage: $taxAmount->percentage
);

$breakdown->addAmount($amount);

return $breakdown;
}
}
23 changes: 23 additions & 0 deletions tests/Stubs/Lunar/TestUrlGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Dystcz\LunarRewards\Tests\Stubs\Lunar;

use Illuminate\Database\Eloquent\Model;

class TestUrlGenerator
{
/**
* The instance of the model.
*
* @var \Illuminate\Database\Eloquent\Model
*/
protected $model;

/**
* Handle the URL generation.
*/
public function handle(Model $model): void
{
// ...
}
}
53 changes: 53 additions & 0 deletions tests/Stubs/Users/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Dystcz\LunarRewards\Tests\Stubs\Users;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Lunar\Base\Traits\LunarUser;

class User extends Authenticatable
{
use HasFactory;
use LunarUser;
use Notifiable;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'email',
'password',
];

/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];

/**
* Return a new factory instance for the model.
*/
protected static function newFactory(): UserFactory
{
return UserFactory::new();
}
}
48 changes: 48 additions & 0 deletions tests/Stubs/Users/UserFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Dystcz\LunarRewards\Tests\Stubs\Users;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

/**
* @extends Factory<Model>
*/
class UserFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = User::class;

/**
* Define the model's default state.
*/
public function definition(): array
{
return [
'name' => $this->faker->name(),
'email' => $this->faker->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
];
}

/**
* Indicate that the model's email address should be unverified.
*
* @return Factory
*/
public function unverified(): UserFactory
{
return $this->state(function (array $attributes) {
return [
'email_verified_at' => null,
];
});
}
}
32 changes: 32 additions & 0 deletions tests/Stubs/Users/UserSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Dystcz\LunarRewards\Tests\Stubs\Users;

use LaravelJsonApi\Eloquent\Fields\ID;
use LaravelJsonApi\Eloquent\Schema;

class UserSchema extends Schema
{
/**
* The model the schema corresponds to.
*/
public static string $model = User::class;

/**
* Get the resource fields.
*/
public function fields(): array
{
return [
ID::make(),
];
}

/**
* Get the JSON:API resource type.
*/
public static function type(): string
{
return 'users';
}
}
14 changes: 12 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@
namespace Dystcz\LunarRewards\Tests;

use Dystcz\LunarApi\Base\Facades\SchemaManifestFacade;
use Dystcz\LunarApi\Tests\Stubs\Lunar\TestUrlGenerator;
use Dystcz\LunarApi\Tests\Stubs\Users\JsonApi\V1\UserSchema;
use Dystcz\LunarRewards\Tests\Stubs\Lunar\TestTaxDriver;
use Dystcz\LunarRewards\Tests\Stubs\Lunar\TestUrlGenerator;
use Dystcz\LunarRewards\Tests\Stubs\Users\User;
use Dystcz\LunarRewards\Tests\Stubs\Users\UserSchema;
use Dystcz\LunarRewards\Tests\Traits\CreatesTestingModels;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use LaravelJsonApi\Testing\MakesJsonApiRequests;
use LaravelJsonApi\Testing\TestExceptionHandler;
use Lunar\Facades\Taxes;
use Orchestra\Testbench\TestCase as Orchestra;

abstract class TestCase extends Orchestra
{
use CreatesTestingModels;
use MakesJsonApiRequests;

protected function setUp(): void
Expand All @@ -28,6 +33,11 @@ protected function setUp(): void
]);

Config::set('lunar.urls.generator', TestUrlGenerator::class);
Config::set('lunar.taxes.driver', 'test');

Taxes::extend('test', function ($app) {
return $app->make(TestTaxDriver::class);
});

activity()->disableLogging();
}
Expand Down
Loading

0 comments on commit ee1440c

Please sign in to comment.