Skip to content
Draft
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
62 changes: 62 additions & 0 deletions tests/Actions/SendOrderDetailsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

Check warning on line 1 in tests/Actions/SendOrderDetailsTest.php

View workflow job for this annotation

GitHub Actions / 4️⃣ Coding Standards

Found violation(s) of type: new_with_parentheses

declare(strict_types=1);

namespace Cone\Bazar\Tests\Actions;

use Cone\Bazar\Actions\SendOrderDetails;
use Cone\Bazar\Models\Order;
use Cone\Bazar\Models\Product;
use Cone\Bazar\Tests\TestCase;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Notification;

class SendOrderDetailsTest extends TestCase
{
protected SendOrderDetails $action;

protected function setUp(): void
{
parent::setUp();

$this->action = new SendOrderDetails();
}

public function test_action_sends_order_details_notification(): void
{
Notification::fake();

$order = Order::factory()->create();

Product::factory()->count(2)->create()->each(function ($product) use ($order) {
$order->items()->create([
'buyable_id' => $product->id,
'buyable_type' => Product::class,
'quantity' => 2,
'price' => $product->price,
'name' => $product->name,
]);
});

$request = Request::create('/');
$models = new Collection([$order]);

$this->action->handle($request, $models);

$this->assertTrue(true);
}

public function test_action_handles_multiple_orders(): void
{
Notification::fake();

$orders = Order::factory()->count(3)->create();

$request = Request::create('/');

$this->action->handle($request, $orders);

$this->assertTrue(true);
}
}
53 changes: 53 additions & 0 deletions tests/Cart/CookieDriverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Cone\Bazar\Tests\Cart;

use Cone\Bazar\Cart\CookieDriver;
use Cone\Bazar\Models\Cart;
use Cone\Bazar\Tests\TestCase;
use Illuminate\Support\Facades\Cookie;

class CookieDriverTest extends TestCase
{
protected CookieDriver $driver;

protected function setUp(): void
{
parent::setUp();

$this->driver = new CookieDriver($this->app['config']->get('bazar.cart.drivers.cookie', []));
}

public function test_cookie_driver_resolves_cart_from_cookie(): void
{
$cart = Cart::factory()->create();

$this->app['request']->cookies->set('cart_id', $cart->id);

$resolvedCart = $this->driver->getModel();

$this->assertInstanceOf(Cart::class, $resolvedCart);
$this->assertSame($cart->id, $resolvedCart->id);
}

public function test_cookie_driver_creates_new_cart_when_no_cookie(): void
{
$cart = $this->driver->getModel();

$this->assertInstanceOf(Cart::class, $cart);
$this->assertTrue($cart->exists);
}

public function test_cookie_driver_queues_cookie_after_resolution(): void
{
$cart = $this->driver->getModel();

$queuedCookies = Cookie::getQueuedCookies();

$this->assertNotEmpty($queuedCookies);
$this->assertSame('cart_id', $queuedCookies[0]->getName());
$this->assertSame($cart->getKey(), $queuedCookies[0]->getValue());

Check failure on line 51 in tests/Cart/CookieDriverTest.php

View workflow job for this annotation

GitHub Actions / 2️⃣ Unit and functional tests (8.5, lowest)

Failed asserting that '1' is identical to 1.

Check failure on line 51 in tests/Cart/CookieDriverTest.php

View workflow job for this annotation

GitHub Actions / 2️⃣ Unit and functional tests (8.5, highest)

Failed asserting that '1' is identical to 1.

Check failure on line 51 in tests/Cart/CookieDriverTest.php

View workflow job for this annotation

GitHub Actions / 2️⃣ Unit and functional tests (8.4, highest)

Failed asserting that '1' is identical to 1.

Check failure on line 51 in tests/Cart/CookieDriverTest.php

View workflow job for this annotation

GitHub Actions / 2️⃣ Unit and functional tests (8.5, locked)

Failed asserting that '1' is identical to 1.

Check failure on line 51 in tests/Cart/CookieDriverTest.php

View workflow job for this annotation

GitHub Actions / 2️⃣ Unit and functional tests (8.4, lowest)

Failed asserting that '1' is identical to 1.

Check failure on line 51 in tests/Cart/CookieDriverTest.php

View workflow job for this annotation

GitHub Actions / 2️⃣ Unit and functional tests (8.4, locked)

Failed asserting that '1' is identical to 1.
}
}
50 changes: 50 additions & 0 deletions tests/Cart/NullDriverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Cone\Bazar\Tests\Cart;

use Cone\Bazar\Cart\NullDriver;
use Cone\Bazar\Models\Cart;
use Cone\Bazar\Tests\TestCase;
use Cone\Bazar\Tests\User;

class NullDriverTest extends TestCase
{
protected NullDriver $driver;

protected function setUp(): void
{
parent::setUp();

$this->driver = new NullDriver([]);
}

public function test_null_driver_resolves_new_cart_instance(): void
{
$cart = $this->driver->getModel();

$this->assertInstanceOf(Cart::class, $cart);
$this->assertFalse($cart->exists);

Check failure on line 28 in tests/Cart/NullDriverTest.php

View workflow job for this annotation

GitHub Actions / 2️⃣ Unit and functional tests (8.5, lowest)

Failed asserting that true is false.

Check failure on line 28 in tests/Cart/NullDriverTest.php

View workflow job for this annotation

GitHub Actions / 2️⃣ Unit and functional tests (8.5, highest)

Failed asserting that true is false.

Check failure on line 28 in tests/Cart/NullDriverTest.php

View workflow job for this annotation

GitHub Actions / 2️⃣ Unit and functional tests (8.4, highest)

Failed asserting that true is false.

Check failure on line 28 in tests/Cart/NullDriverTest.php

View workflow job for this annotation

GitHub Actions / 2️⃣ Unit and functional tests (8.5, locked)

Failed asserting that true is false.

Check failure on line 28 in tests/Cart/NullDriverTest.php

View workflow job for this annotation

GitHub Actions / 2️⃣ Unit and functional tests (8.4, lowest)

Failed asserting that true is false.

Check failure on line 28 in tests/Cart/NullDriverTest.php

View workflow job for this annotation

GitHub Actions / 2️⃣ Unit and functional tests (8.4, locked)

Failed asserting that true is false.
}

public function test_null_driver_associates_cart_with_authenticated_user(): void
{
$user = User::factory()->create();

$this->actingAs($user);

$cart = $this->driver->getModel();

$this->assertInstanceOf(Cart::class, $cart);
$this->assertSame($user->id, $cart->user_id);

Check failure on line 40 in tests/Cart/NullDriverTest.php

View workflow job for this annotation

GitHub Actions / 2️⃣ Unit and functional tests (8.5, lowest)

Failed asserting that null is identical to 1.

Check failure on line 40 in tests/Cart/NullDriverTest.php

View workflow job for this annotation

GitHub Actions / 2️⃣ Unit and functional tests (8.5, highest)

Failed asserting that null is identical to 1.

Check failure on line 40 in tests/Cart/NullDriverTest.php

View workflow job for this annotation

GitHub Actions / 2️⃣ Unit and functional tests (8.4, highest)

Failed asserting that null is identical to 1.

Check failure on line 40 in tests/Cart/NullDriverTest.php

View workflow job for this annotation

GitHub Actions / 2️⃣ Unit and functional tests (8.5, locked)

Failed asserting that null is identical to 1.

Check failure on line 40 in tests/Cart/NullDriverTest.php

View workflow job for this annotation

GitHub Actions / 2️⃣ Unit and functional tests (8.4, lowest)

Failed asserting that null is identical to 1.

Check failure on line 40 in tests/Cart/NullDriverTest.php

View workflow job for this annotation

GitHub Actions / 2️⃣ Unit and functional tests (8.4, locked)

Failed asserting that null is identical to 1.
}

public function test_null_driver_creates_cart_without_user_when_not_authenticated(): void
{
$cart = $this->driver->getModel();

$this->assertInstanceOf(Cart::class, $cart);
$this->assertNull($cart->user_id);
}
}
48 changes: 48 additions & 0 deletions tests/Cart/SessionDriverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Cone\Bazar\Tests\Cart;

use Cone\Bazar\Cart\SessionDriver;
use Cone\Bazar\Models\Cart;
use Cone\Bazar\Tests\TestCase;

class SessionDriverTest extends TestCase
{
protected SessionDriver $driver;

protected function setUp(): void
{
parent::setUp();

$this->driver = new SessionDriver($this->app['config']->get('bazar.cart.drivers.session', []));
}

public function test_session_driver_resolves_cart_from_session(): void
{
$cart = Cart::factory()->create();

$this->app['request']->session()->put('cart_id', $cart->id);

$resolvedCart = $this->driver->getModel();

$this->assertInstanceOf(Cart::class, $resolvedCart);
$this->assertSame($cart->id, $resolvedCart->id);
}

public function test_session_driver_creates_new_cart_when_no_session(): void
{
$cart = $this->driver->getModel();

$this->assertInstanceOf(Cart::class, $cart);
$this->assertTrue($cart->exists);
}

public function test_session_driver_stores_cart_id_in_session_after_resolution(): void
{
$cart = $this->driver->getModel();

$this->assertSame($cart->getKey(), $this->app['request']->session()->get('cart_id'));
}
}
66 changes: 66 additions & 0 deletions tests/Gateway/CashDriverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Cone\Bazar\Tests\Gateway;

use Cone\Bazar\Gateway\CashDriver;
use Cone\Bazar\Models\Order;
use Cone\Bazar\Models\Product;
use Cone\Bazar\Tests\TestCase;

class CashDriverTest extends TestCase
{
protected CashDriver $driver;

protected Order $order;

protected function setUp(): void
{
parent::setUp();

$this->driver = new CashDriver(['enabled' => true]);

$this->order = Order::factory()->create();

Product::factory()->count(2)->create()->each(function ($product) {
$this->order->items()->create([
'buyable_id' => $product->id,
'buyable_type' => Product::class,
'quantity' => 2,
'price' => $product->price,
'name' => $product->name,
]);
});
}

public function test_cash_driver_has_correct_name(): void
{
$this->assertSame('Cash', $this->driver->getName());
}

public function test_cash_driver_can_pay(): void
{
$transaction = $this->driver->pay($this->order, 100);

$this->assertEquals(100, $transaction->amount);
$this->assertTrue($transaction->completed());
}

public function test_cash_driver_can_refund(): void
{
$this->driver->pay($this->order);

$transaction = $this->driver->refund($this->order, 50);

$this->assertEquals(50, $transaction->amount);
$this->assertTrue($transaction->completed());
}

public function test_cash_driver_marks_transactions_as_completed(): void
{
$transaction = $this->driver->pay($this->order);

$this->assertNotNull($transaction->completed_at);
}
}
68 changes: 68 additions & 0 deletions tests/Gateway/ManualDriverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace Cone\Bazar\Tests\Gateway;

use Cone\Bazar\Gateway\ManualDriver;
use Cone\Bazar\Models\Order;
use Cone\Bazar\Models\Product;
use Cone\Bazar\Tests\TestCase;
use Exception;

class ManualDriverTest extends TestCase
{
protected ManualDriver $driver;

protected Order $order;

protected function setUp(): void
{
parent::setUp();

$this->driver = new ManualDriver(['enabled' => true]);

$this->order = Order::factory()->create();

Product::factory()->count(2)->create()->each(function ($product) {
$this->order->items()->create([
'buyable_id' => $product->id,
'buyable_type' => Product::class,
'quantity' => 2,
'price' => $product->price,
'name' => $product->name,
]);
});
}

public function test_manual_driver_has_correct_name(): void
{
$this->assertSame('Manual', $this->driver->getName());
}

public function test_manual_driver_can_pay(): void
{
$transaction = $this->driver->pay($this->order, 100);

$this->assertEquals(100, $transaction->amount);

Check failure on line 47 in tests/Gateway/ManualDriverTest.php

View workflow job for this annotation

GitHub Actions / 2️⃣ Unit and functional tests (8.4, highest)

Failed asserting that 78.0 matches expected 100.

Check failure on line 47 in tests/Gateway/ManualDriverTest.php

View workflow job for this annotation

GitHub Actions / 2️⃣ Unit and functional tests (8.5, locked)

Failed asserting that 72.0 matches expected 100.
$this->assertTrue($transaction->completed());
}

public function test_manual_driver_can_refund(): void
{
$this->driver->pay($this->order);

$transaction = $this->driver->refund($this->order, 50);

$this->assertEquals(50, $transaction->amount);
$this->assertTrue($transaction->completed());
}

public function test_manual_driver_throws_exception_on_notification(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('This payment gateway does not support payment notifications.');

$this->driver->handleNotification($this->app['request'], $this->order);
}
}
Loading
Loading