Skip to content

Commit

Permalink
redone createUrl <- buildUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
hiqsol committed Nov 30, 2015
1 parent 745cc25 commit dea6779
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Closure;
use Yii;
use yii\helpers\Url;

/**
* Cart Module.
Expand Down Expand Up @@ -71,9 +72,11 @@ public function getCart()
return $this->get(static::CART);
}

public function buildUrl($route = null)
public function createUrl($route = null)
{
return '/' . $this->id . '/' . ($route ?: 'cart/index');
$params = is_array($route) ? $route : [$route];
$params[0] = '/' . $this->id . '/' . (strpos($params[0], '/') !== false ? $params[0] : 'cart/' . ($params[0] ?: 'index'));
return Url::toRoute($params);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/widgets/views/PanelTopCart_view.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<?php if ($cart->count) : ?>
<li class="header">
<div class="row">
<div class="col-md-4"><?= Html::a(Yii::t('cart', 'Cart'), $widget->module->buildUrl()) ?>:</div>
<div class="col-md-4"><?= Html::a(Yii::t('cart', 'Cart'), $widget->module->createUrl()) ?>:</div>
<div class="col-md-8 text-bold text-right"><?= Yii::t('cart', 'Total') ?>: <?= $cart->formatCurrency($cart->total) ?></div>
</div>
</li>
Expand All @@ -28,7 +28,7 @@
<?php endforeach ?>
</ul>
</li>
<li class="footer"><?= Html::a(Yii::t('cart', 'View Cart'), $widget->module->buildUrl()) ?></li>
<li class="footer"><?= Html::a(Yii::t('cart', 'View Cart'), $widget->module->createUrl()) ?></li>
<?php else : ?>
<li class="header">
<div class="row">
Expand Down
17 changes: 15 additions & 2 deletions tests/unit/ModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,21 @@ public function testGetCart()
$this->assertInstanceOf(ShoppingCart::className(), $this->object->getCart());
$this->assertSame(Yii::$app->getModule('cart')->get('cart'), $this->object->cart);
}
public function testBuildUrl()
public function testCreateUrl()
{
$this->assertStringEndsWith('/cart/cart/index', $this->object->buildUrl());
$this->assertSame('/cart/cart/index', $this->object->createUrl());
$this->assertSame('/cart/cart/something', $this->object->createUrl('something'));
$this->assertSame('/cart/cart/order?a=b', $this->object->createUrl(['order', 'a' => 'b']));
$this->assertSame('/cart/test/order?a=b', $this->object->createUrl(['test/order', 'a' => 'b']));
}

protected $methods = 'a and b';

public function testPaymentMethods()
{
$this->object->setPaymentMethods($this->methods);
$this->assertSame($this->methods, $this->object->getPaymentMethods());
$this->object->setPaymentMethods(function () { return $this->methods; });
$this->assertSame($this->methods, $this->object->getPaymentMethods());
}
}

0 comments on commit dea6779

Please sign in to comment.