Skip to content

Commit

Permalink
플러그인 유닛테스트 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
kimyu711 committed Dec 11, 2018
1 parent 1d00cec commit 967a457
Show file tree
Hide file tree
Showing 16 changed files with 1,263 additions and 0 deletions.
33 changes: 33 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="./vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="true">
<testsuites>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>

<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
</php>
</phpunit>
49 changes: 49 additions & 0 deletions tests/Unit/BadgeHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Created by PhpStorm.
* User: hero
* Date: 11/12/2018
* Time: 12:02 PM
*/

namespace Xpressengine\Plugins\XeroCommerce\Test\Unit;

use Xpressengine\Plugins\XeroCommerce\Handlers\BadgeHandler;
use PHPUnit\Framework\TestCase;
use Xpressengine\Plugins\XeroCommerce\Models\Badge;

class BadgeHandlerTest extends DefaultSet
{

public function testStore()
{
$handler = new BadgeHandler();
$args=[
'name'=>'test',
'eng_name'=>'test',
'background_color'=>'test',
'text_color'=>'test'
];
$handler->store($args);
$this->assertNotEquals(0, Badge::count());
}

public function testUpdate()
{
$this->testStore();
$badge=Badge::first();
$handler = new BadgeHandler();
$handler->update($badge,['name'=>'updateTest']);
$this->assertEquals('updateTest',Badge::find($badge->id)->name);
}

public function testDestroy()
{
$handler = new BadgeHandler();
$this->testStore();
$badge=Badge::first();
$handler->destroy($badge->id);
$this->assertNull(Badge::find($badge->id));

}
}
112 changes: 112 additions & 0 deletions tests/Unit/CartHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
/**
* Created by PhpStorm.
* User: hero
* Date: 10/12/2018
* Time: 10:56 AM
*/

namespace Xpressengine\Plugins\XeroCommerce\Test\Unit;

use Xpressengine\Plugins\XeroCommerce\Handlers\CartHandler;
use Xpressengine\Plugins\XeroCommerce\Models\Cart;
use Xpressengine\Plugins\XeroCommerce\Models\CartGroup;
use Xpressengine\Plugins\XeroCommerce\Models\Product;
use Xpressengine\Plugins\XeroCommerce\Models\ProductOptionItem;

class CartHandlerTest extends DefaultSet
{

public function testGetCartList()
{
$cartHandler = new CartHandler();
$list = $cartHandler->getCartList();
$this->assertTrue(is_iterable($list));
}

public function testGetCartListByCartIds()
{
$cartHandler = new CartHandler();
$list = $cartHandler->getCartListByCartIds(1);
$this->assertTrue(is_iterable($list));
$list = $cartHandler->getCartListByCartIds([1]);
$this->assertTrue(is_iterable($list));
}

public function testMakeCartGroup()
{
$this->makeProduct();
$cartHandler = new CartHandler();
$sellUnit = ProductOptionItem::first();
$cartGroup = $cartHandler->makeCartGroup($sellUnit, 4);
$this->assertInstanceOf(CartGroup::class, $cartGroup);
}

public function testAddCart()
{
$handler = new CartHandler();
$this->makeProduct();
$cartGroup = $handler->makeCartGroup(ProductOptionItem::first(), 4);
$cart = $handler->addCart(Product::first(), collect([$cartGroup]), '선불');
$this->assertNotNull($cart);
$this->assertInstanceOf(Cart::class, $cart);
return $cart;
}

/**
* @depends testAddCart
*/
public function testChangeCartItem(Cart $cart)
{
$handler = new CartHandler();
$original = $cart->sellGroups()->get();
$id = $cart->id;
$this->makeProduct();
$handler->changeCartItem($cart, $handler->makeCartGroup(ProductOptionItem::first(), 2), '착불');
$this->assertNotEquals($original, $cart->sellGroups()->get());
$handler->changeCartItem($cart, $handler->makeCartGroup(ProductOptionItem::first(), 0), '착불');
$this->assertNull(Cart::find($id));
}

/**
* @depends testAddCart
*/
public function testDrawCart()
{
$handler = new CartHandler();
$this->makeProduct();
$cartGroup = $handler->makeCartGroup(ProductOptionItem::first(), 4);
$cart = $handler->addCart(Product::first(), collect([$cartGroup]), '선불');
$handler->drawCart($cart->id);
$this->assertNull(Cart::find($cart->id));
}

public function testResetCart()
{
$handler = new CartHandler();
$this->makeProduct();
$cartGroup = $handler->makeCartGroup(ProductOptionItem::first(), 4);
$cart = $handler->addCart(Product::first(), collect([$cartGroup]), '선불');
$handler->resetCart();
$this->assertEquals(0, $handler->getCartList()->count());
}

public function testSellSetList()
{
$handler = new CartHandler();
$list = $handler->getSellSetList();
$this->assertTrue(is_iterable($list));
}

public function testGetSummary()
{
$handler = new CartHandler();
$summary = $handler->getSummary();
$this->assertArrayHasKey('original_price',$summary);
$this->assertArrayHasKey('sell_price',$summary);
$this->assertArrayHasKey('discount_price',$summary);
$this->assertArrayHasKey('fare',$summary);
$this->assertArrayHasKey('sum',$summary);
}

}
144 changes: 144 additions & 0 deletions tests/Unit/DefaultSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php
/**
* Created by PhpStorm.
* User: hero
* Date: 10/12/2018
* Time: 10:58 AM
*/
namespace Xpressengine\Plugins\XeroCommerce\Test\Unit;

use App\Http\Kernel;
use App\Providers\CategoryServiceProvider;
use App\Providers\ConfigServiceProvider;
use App\Providers\DatabaseServiceProvider;
use App\Providers\EventServiceProvider;
use App\Providers\InterceptionServiceProvider;
use Composer\EventDispatcher\Event;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Artisan;
use Mockery as m;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Xpressengine\Config\ConfigManager;
use Xpressengine\Config\ConfigRepository;
use Xpressengine\Config\Repositories\CacheDecorator;
use Xpressengine\Config\Repositories\DatabaseRepository;
use Xpressengine\Database\Eloquent\DynamicModel;
use Xpressengine\Database\ProxyManager;
use Xpressengine\Database\VirtualConnection;
use Xpressengine\Keygen\Keygen;
use Xpressengine\Migrations\ConfigMigration;
use Xpressengine\Plugins\XeroCommerce\Handlers\ShopHandler;
use Xpressengine\Plugins\XeroCommerce\Models\DeliveryCompany;
use Xpressengine\Plugins\XeroCommerce\Models\Product;
use Xpressengine\Plugins\XeroCommerce\Models\Shop;
use Xpressengine\Plugins\XeroCommerce\Plugin\EventManager;
use Xpressengine\Plugins\XeroCommerce\Plugin\Resources;
use Xpressengine\User\Rating;

class DefaultSet extends \PHPUnit\Framework\TestCase
{
public function setUp()
{
parent::setUp();

$app = new \Illuminate\Container\Container();
$app->singleton('app', 'Illuminate\Container\Container');

$app->singleton('config', 'Illuminate\Config\Repository');
//
$app['config']->set('database.default', 'test');
$app['config']->set('database.connections.test', [
'driver' => 'sqlite',
'database' => __DIR__.'/test.db'
]);
$app['config']->set('auth.defaults.guard', 'test');
$app['config']->set('auth.guards.test',[
'driver' => 'test',
'provider' => 'users',
]);

$app->bind('db', function ($app) {
return new \Illuminate\Database\DatabaseManager($app, new \Illuminate\Database\Connectors\ConnectionFactory($app));
});

$app->bind('auth',function ($app) {
$authmanager = new \Illuminate\Auth\AuthManager($app);
$authmanager->extend('test', function ($app, $name, $config) {
$guard = m::mock(\Illuminate\Contracts\Auth\Guard::class);
$guard->shouldReceive('id')->andReturn(1);
$guard->shouldReceive('check')->andReturn(true);
$guard->shouldReceive('user')->andReturn((object)['rating'=>Rating::USER]);
return $guard;
});
return $authmanager;
});


$app->singleton(
\Illuminate\Contracts\Http\Kernel::class,
Kernel::class
);

$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
App\Console\Kernel::class
);

DynamicModel::setConnectionResolver($app['db']);
Model::setConnectionResolver($app['db']);
DynamicModel::setKeyGen(new Keygen());

\Illuminate\Support\Facades\Facade::setFacadeApplication($app);
\Xpressengine\Plugins\XeroCommerce\Plugin\Database::create();
}

public function tearDown()
{
parent::tearDown();
\Xpressengine\Plugins\XeroCommerce\Plugin\Database::drop();
}

protected function makeProduct()
{
$product = new Product();
$product->shop_id = 1;
$product->product_code = 'test';
$product->detail_info = json_encode([
'상품정보' => '샘플 상품',
'비고' => '수정해서 사용'
]);
$product->name = '지금부터 봄까지 입는 데일리 인기신상 ITEM';
$product->sub_name = '간단한 상품설명';
$product->original_price = 1000;
$product->sell_price = $product->original_price;
$product->discount_percentage = 100;
$product->description = '상품설명페이지';
$product->tax_type = rand(Product::TAX_TYPE_TAX, Product::TAX_TYPE_FREE);
$product->state_display = Product::DISPLAY_VISIBLE;
$product->state_deal = Product::DEAL_ON_SALE;
$product->shop_delivery_id = 1;
$product->save();
Resources::storeProductOption($product->id);
return $product;
}

protected function makeShop()
{
$args['user_id'] = 'test';
$args['shop_type'] = Shop::TYPE_STORE;
$args['shop_name'] = Shop::BASIC_SHOP_NAME;
$newShop = new Shop();

$newShop->fill($args);

$newShop->save();

Resources::storeDefaultDeliveryCompanySet();

$newShop->deliveryCompanys()->attach(DeliveryCompany::pluck('id'), [
'delivery_fare'=>0,
'up_to_free'=>0,
'is_default'=>0
]);
}
}
Loading

0 comments on commit 967a457

Please sign in to comment.