Skip to content

Commit

Permalink
Test and lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Feb 22, 2019
1 parent 3ef2e27 commit ea0a5ee
Show file tree
Hide file tree
Showing 17 changed files with 91 additions and 69 deletions.
4 changes: 2 additions & 2 deletions src/Extensions/Constraints/ProductsDiscountConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function check(Discount $discount)

if (!$products->exists()) {
Versioned::withVersionedMode(
function () use ($discount, $productIds) {
function () use ($discount, &$productIds) {
Versioned::set_stage(Versioned::DRAFT);

$products = $discount->Products();
Expand All @@ -67,7 +67,7 @@ function () use ($discount, $productIds) {
$productIds = $products->map('ID', 'ID')->toArray();
}

if (!productIds) {
if (!$productIds) {
return true;
}

Expand Down
File renamed without changes.
5 changes: 4 additions & 1 deletion src/Model/OrderCoupon.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\TextField;
use SilverStripe\Security\RandomGenerator;
use SilverStripe\ORM\ValidationResult;

/**
* Applies a discount to current order, if applicable, when entered at checkout.
Expand Down Expand Up @@ -94,13 +95,15 @@ public function validate()
$result = parent::validate();
$minLength = $this->config()->minimum_code_length;
$code = $this->getField('Code');

if ($minLength && $code && $this->isChanged('Code') && strlen($code) < $minLength) {
$result->error(
$result->addError(
_t(
'OrderCoupon.INVALIDMINLENGTH',
'Coupon code must be at least {length} characters in length',
['length' => $this->config()->minimum_code_length]
),
ValidationResult::TYPE_ERROR,
'INVALIDMINLENGTH'
);
}
Expand Down
34 changes: 17 additions & 17 deletions src/Model/PartialUseDiscount.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,20 @@ class PartialUseDiscount extends Discount

public function getCMSFields($params = null)
{
$fields = parent::getCMSFields(
[
$fields = parent::getCMSFields([
'forcetype' => 'Amount'
]
);
]);

$fields->removeByName(
[
$fields->removeByName([
"ForCart",
"ForItems",
"ForShipping",
"For"
]
);
$limitfield = $fields->fieldByName("Root.Main.Constraints")
->fieldByName("Main.UseLimit");
$fields->replaceField("UseLimit", $limitfield->performReadOnlyTransformation());
]);

$limitfield = $fields->dataFieldByName('UseLimit');

$fields->replaceField("UseLimit", $limitfield->performReadonlyTransformation());
return $fields;
}

Expand All @@ -66,13 +63,16 @@ public function createRemainder($used)
$amount = $this->getAmount();

if ($used < $amount) {
//duplicate dataobject and update accordingly
// duplicate dataobject and update accordingly
$remainder = $this->duplicate(false);
$remainder->write();
//delete any relationships that might be sitting in DB for whatever reason

// delete any relationships that might be sitting in DB for whatever
// reason
$remainder->deleteRelationships();
//create proper new relationships
$this->duplicateManyManyRelations($this, $remainder);

// create proper new relationships
$this->duplicateManyManyRelations($this, $remainder, true);

//TODO: there may be some relationships that shouldn't be copied?
$remainder->Amount = $amount - $used;
Expand Down Expand Up @@ -103,8 +103,8 @@ public function validate()
*/
protected function deleteRelationships()
{
if ($this->many_many()) {
foreach ($this->many_many() as $name => $type) {
if ($this->manyMany()) {
foreach ($this->manyMany() as $name => $type) {
$this->{$name}()->removeAll();
}
}
Expand Down
10 changes: 5 additions & 5 deletions templates/email/GiftVoucherEmail.ss
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<p>Here is your gift voucher</p>

<% with Coupon %>
<% with $Coupon %>
<div class="giftvoucher" style="padding:10px; font-size:2em;">
<p>Code: <strong>$Code</strong></p>
<% if Type = Percent %>
<% if $Type = 'Percent' %>
<p>Percent: $Percent.Nice</p>
<% else %>
<p>Amount: $Amount.Nice</p>
<% end_if %>
<% if EndDate %>

<% if $EndDate %>
<p>This voucher must be used by $EndDate.Long</p>
<% end_if %>
</div>
<% end_with %>
<% end_with %>
10 changes: 5 additions & 5 deletions tests/CalculatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace SilverShop\Discounts\Tests;

use SilverShop\Discounts\Calculator;
use SilverShop\Discount\Adjustment;
use SilverShop\Discount\PriceInfo;
use SilverShop\Discounts\Adjustment;
use SilverShop\Discounts\PriceInfo;
use SilverStripe\Dev\SapphireTest;
use SilverShop\Tests\ShopTest;
use SilverShop\Model\Order;
Expand Down Expand Up @@ -88,7 +88,7 @@ public function testBasicItemDiscount()
$this->assertEquals(1, $discount->getDiscountValue(10), "10% of 10 is 1");
//check that discount matches order
$matching = Discount::get_matching($this->cart);
$this->assertDOSEquals(
$this->assertListEquals(
[
["Title" => "10% off"]
],
Expand Down Expand Up @@ -138,7 +138,7 @@ function testItemLevelPercentAndAmountDiscounts()

//check that discount matches order
$matching = Discount::get_matching($this->cart);
$this->assertDOSEquals(
$this->assertListEquals(
[
["Title" => "10% off"],
["Title" => "$5 off"]
Expand All @@ -162,7 +162,7 @@ function testItemLevelPercentAndAmountDiscounts()
$calculator = new Calculator($this->megacart);
$this->assertEquals(190, $calculator->calculate(), "complex savings example");

$this->assertDOSEquals(
$this->assertListEquals(
[
["Title" => "10% off"],
["Title" => "$5 off"]
Expand Down
27 changes: 18 additions & 9 deletions tests/CategoriesDiscountConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,29 @@
use SilverStripe\Dev\SapphireTest;
use SilverShop\Tests\ShopTest;
use SilverShop\Discounts\Model\OrderDiscount;
use SilverShop\Model\Order;
use SilverShop\Page\Product;
use SilverShop\Page\ProductCategory;

class CategoriesDiscountConstraintTest extends SapphireTest
{

protected static $fixture_file = [
'shop.yml',
'Carts.yml'
'vendor/silvershop/core/tests/php/Fixtures/Carts.yml'
];

public function setUp()
{
parent::setUp();
ShopTest::setConfiguration();

$this->socks = $this->objFromFixture("Product", "socks");
$this->socks->publish("Stage", "Live");
$this->tshirt = $this->objFromFixture("Product", "tshirt");
$this->tshirt->publish("Stage", "Live");
$this->mp3player = $this->objFromFixture("Product", "mp3player");
$this->mp3player->publish("Stage", "Live");
$this->socks = $this->objFromFixture(Product::class, "socks");
$this->socks->publishRecursive();
$this->tshirt = $this->objFromFixture(Product::class, "tshirt");
$this->tshirt->publishRecursive();
$this->mp3player = $this->objFromFixture(Product::class, "mp3player");
$this->mp3player->publishRecursive();

$this->cart = $this->objFromFixture(Order::class, "cart");
$this->othercart = $this->objFromFixture(Order::class, "othercart");
Expand All @@ -41,8 +44,11 @@ public function testCategoryDiscount()
"Percent" => 0.05
]
);

$discount->write();
$discount->Categories()->add($this->objFromFixture("ProductCategory", "clothing"));
$discount->Categories()->add(
$this->objFromFixture(ProductCategory::class, "clothing")
);

$this->assertTrue($discount->validateOrder($this->cart), "Order contains a t-shirt. ".$discount->getMessage());
$calculator = new Calculator($this->cart);
Expand All @@ -54,7 +60,10 @@ public function testCategoryDiscount()

$discount->Categories()->removeAll();

$discount->Categories()->add($this->objFromFixture("ProductCategory", "kites"));
$discount->Categories()->add(
$this->objFromFixture(ProductCategory::class, "kites")
);

$this->assertTrue($discount->validateOrder($this->kitecart), "Order contains a kite. ".$discount->getMessage());
$calculator = new Calculator($this->kitecart);
$this->assertEquals($calculator->calculate(), 1.75, "5% discount for kite in cart");
Expand Down
3 changes: 2 additions & 1 deletion tests/CouponFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
use SilverStripe\Control\Session;
use SilverShop\Discounts\Model\OrderCoupon;
use SilverShop\Discounts\Form\CouponForm;
use SilverShop\Model\Order;

class CouponFormTest extends FunctionalTest
{

protected static $fixture_file = [
'shop.yml',
'Pages.yml'
'Page.yml'
];

protected function setUp()
Expand Down
2 changes: 1 addition & 1 deletion tests/GiftVoucherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function setUp()

public function testCusomisableVoucher()
{
$controller = new GiftVoucherProduct_Controller($this->variable);
$controller = new GiftVoucherProductController($this->variable);
$form = $controller->Form();

$form->loadDataFrom(
Expand Down
2 changes: 1 addition & 1 deletion tests/GiftVouchers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ SilverShop\Model\Order:

SilverShop\Discounts\Model\GiftVoucherOrderItem:
giftcart_10fixed:
Product: '=>SilverShop\Discounts\Page\GiftVoucherProduct.10fixed'
Product: =>SilverShop\Discounts\Page\GiftVoucherProduct.10fixed
Quantity: 2
6 changes: 4 additions & 2 deletions tests/GroupDiscountConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use SilverStripe\Dev\SapphireTest;
use SilverShop\Tests\ShopTest;

use SilverShop\Model\Order;
use SilverShop\Discounts\Model\OrderCoupon;
use SilverStripe\Security\Group;
use SilverStripe\Security\Member;

class GroupDiscountConstraintTest extends SapphireTest
{
Expand All @@ -31,7 +33,7 @@ public function testMemberGroup()
"Type" => "Percent",
"Percent" => 0.9,
"Active" => 1,
"GroupID" => $this->objFromFixture("Group", "resellers")->ID
"GroupID" => $this->objFromFixture(Group::class, "resellers")->ID
]
);
$coupon->write();
Expand Down
6 changes: 3 additions & 3 deletions tests/MembershipDiscountConstraintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use SilverStripe\Dev\SapphireTest;
use SilverShop\Tests\ShopTest;

use SilverStripe\Security\Member;
use SilverShop\Discounts\Model\OrderDiscount;

class MembershipDiscountConstraintTest extends SapphireTest
Expand Down Expand Up @@ -32,12 +32,12 @@ public function testMembership()
);
$discount->write();

$member = $this->objFromFixture("Member", "joebloggs");
$member = $this->objFromFixture(Member::class, "joebloggs");
$discount->Members()->add($member);

$this->assertFalse($discount->validateOrder($this->cart), "Invalid, because no member");
$context = [
"Member" => $this->objFromFixture("Member", "bobjones")
"Member" => $this->objFromFixture(Member::class, "bobjones")
];
$this->assertFalse($discount->validateOrder($this->cart, $context), "Invalid because wrong member present");
$context = ["Member" => $member];
Expand Down
12 changes: 7 additions & 5 deletions tests/OrderDiscountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use SilverShop\Model\Order;
use SilverShop\Discounts\Model\OrderDiscount;
use SilverShop\Discounts\Model\Discount;
use SilverStripe\Omnipay\Model\Payment;

class OrderDiscountTest extends SapphireTest
{
Expand Down Expand Up @@ -42,7 +43,7 @@ public function testManyMatches()
]
)->write();
$matches = OrderDiscount::get_matching($this->cart);
$this->assertDOSEquals(
$this->assertListEquals(
[
["Title" => "10% off"],
["Title" => "$5 off"],
Expand All @@ -60,7 +61,7 @@ public function testPercent()
"Percent" => 0.10
]
)->write();
$this->assertDOSEquals(
$this->assertListEquals(
[
["Title" => "10% off"]
],
Expand All @@ -77,7 +78,7 @@ public function testAmount()
"Amount" => 5
]
)->write();
$this->assertDOSEquals(
$this->assertListEquals(
[
["Title" => "$5 off"]
],
Expand All @@ -89,8 +90,9 @@ public function testUseCount()
{
//check that order with payment started counts as a use
$discount = $this->objFromFixture(OrderDiscount::class, "paymentused");
$payment = $this->objFromFixture("Payment", "paymentstarted_recent");
//set timeout to 60 minutes
$payment = $this->objFromFixture(Payment::class, "paymentstarted_recent");

// set timeout to 60 minutes
Discount::config()->unpaid_use_timeout = 60;
//set payment to be created 20 min ago
$payment->Created = date('Y-m-d H:i:s', strtotime("-20 minutes"));
Expand Down
9 changes: 6 additions & 3 deletions tests/PartialUseDiscount.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ SilverStripe\Security\Member:
hubert:
FirstName: Hubert

SilverShop\Model\PartialUseDiscount:
SilverShop\Discounts\Model\PartialUseDiscount:
partial:
Amount: 90
constrained:
Amount: 50
Products: '=>SilverShop\Page\Product.producta','=>SilverShop\Page\Product.productb'
Members: '=>SilverStripe\Security\Member.joe'
Products:
- =>SilverShop\Page\Product.producta
- =>SilverShop\Page\Product.productb
Members:
- =>SilverStripe\Security\Member.joe
4 changes: 2 additions & 2 deletions tests/PartialUseDiscountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ public function testCreateRemainder()
//check constraints copying works
$discount = $this->objFromFixture(PartialUseDiscount::class, "constrained");
$remainder = $discount->createRemainder(40);
$this->assertDOSEquals(
$this->assertListEquals(
[
["FirstName" => "Joe"]
],
$remainder->Members()
);
$this->assertDOSEquals(
$this->assertListEquals(
[
["Title" => "ProductA"],
["Title" => "ProductB"]
Expand Down
Loading

0 comments on commit ea0a5ee

Please sign in to comment.