Skip to content

Instantly share code, notes, and snippets.

@luckily
Last active March 10, 2016 04:18
Show Gist options
  • Save luckily/0ce8b7065a57cfd90505 to your computer and use it in GitHub Desktop.
Save luckily/0ce8b7065a57cfd90505 to your computer and use it in GitHub Desktop.
Skilltree TDD Day1 HomeWork
<?php
namespace App;
/**
* 銷售記錄商業邏輯
* Class SellRecordService
* @package App
*/
class SellRecordService
{
protected $sellRecords = array();
public function __construct(array $sellRecords = null)
{
$this->setSellRecords($sellRecords);
}
/**
* 設定sellRecords
* @param array $sellRecords
*/
public function setSellRecords(array $sellRecords = null)
{
if(isset($sellRecords))
$this->sellRecords = $sellRecords;
}
/**
* 取得sellRecords
* @return array
*/
public function getSellRecords()
{
return $this->sellRecords;
}
/**
* 依照給定的欄位跟每幾筆一組,回傳加總陣列
* @param $attribute
* @param int $perNum
* @return array
*/
public function getValueSumBy($attribute, $perNum = 1)
{
$temp = 0;
$sums = array();
$recordSize = count($this->sellRecords);
foreach($this->sellRecords as $i => $sellRecord) {
$method = 'get' . ucfirst($attribute);
$temp += $sellRecord->$method();
if((($i+1) % $perNum == 0) || ($i+1) == $recordSize ) {
$sums[] = $temp;
$temp = 0;
}
}
return $sums;
}
}
<?php
/**
* 測試銷售記錄
* Class SellRecordServiceTest
*/
class SellRecordServiceTest extends PHPUnit_Framework_TestCase
{
/**
* 3筆一組,取Cost總和 6,15,24,21
*/
public function test_get_cost_sum_per_3_records_should_be_6_15_24_21()
{
//Arrange
$target = new \App\SellRecordService();
$sellRecords = array(
(new StubSellRecord())->setCost(1)->setRevenue(11)->setSellPrice(21),
(new StubSellRecord())->setCost(2)->setRevenue(12)->setSellPrice(22),
(new StubSellRecord())->setCost(3)->setRevenue(13)->setSellPrice(23),
(new StubSellRecord())->setCost(4)->setRevenue(14)->setSellPrice(24),
(new StubSellRecord())->setCost(5)->setRevenue(15)->setSellPrice(25),
(new StubSellRecord())->setCost(6)->setRevenue(16)->setSellPrice(26),
(new StubSellRecord())->setCost(7)->setRevenue(17)->setSellPrice(27),
(new StubSellRecord())->setCost(8)->setRevenue(18)->setSellPrice(28),
(new StubSellRecord())->setCost(9)->setRevenue(19)->setSellPrice(29),
(new StubSellRecord())->setCost(10)->setRevenue(20)->setSellPrice(30),
(new StubSellRecord())->setCost(11)->setRevenue(21)->setSellPrice(31),
);
//Act
$target->setSellRecords($sellRecords);
$actual = $target->getValueSumBy('cost', 3);
$expected = array(6, 15, 24, 21);
//Assert
$this->assertTrue($this->arrayAreSimilar($expected, $actual));
}
/**
* 4筆一組,取Revenue總和 50,66,60
*/
public function test_get_revenue_sums_per_4_records_should_be_50_66_60()
{
//Arrange
$target = new \App\SellRecordService();
$sellRecords = array(
(new StubSellRecord())->setCost(1)->setRevenue(11)->setSellPrice(21),
(new StubSellRecord())->setCost(2)->setRevenue(12)->setSellPrice(22),
(new StubSellRecord())->setCost(3)->setRevenue(13)->setSellPrice(23),
(new StubSellRecord())->setCost(4)->setRevenue(14)->setSellPrice(24),
(new StubSellRecord())->setCost(5)->setRevenue(15)->setSellPrice(25),
(new StubSellRecord())->setCost(6)->setRevenue(16)->setSellPrice(26),
(new StubSellRecord())->setCost(7)->setRevenue(17)->setSellPrice(27),
(new StubSellRecord())->setCost(8)->setRevenue(18)->setSellPrice(28),
(new StubSellRecord())->setCost(9)->setRevenue(19)->setSellPrice(29),
(new StubSellRecord())->setCost(10)->setRevenue(20)->setSellPrice(30),
(new StubSellRecord())->setCost(11)->setRevenue(21)->setSellPrice(31),
);
//Act
$target->setSellRecords($sellRecords);
$actual = $target->getValueSumBy('revenue', 4);
$expected = array(50, 66, 60);
//Assert
$this->assertTrue($this->arrayAreSimilar($expected, $actual));
}
/**
* 比較兩組陣列是否相等
* @param $a
* @param $b
* @return bool
*/
public function arrayAreSimilar($a, $b)
{
// if the indexes don't match, return immediately
if (count(array_diff_assoc($a, $b))) {
return false;
}
// we know that the indexes, but maybe not values, match.
// compare the values between the two arrays
foreach($a as $k => $v) {
if ($v !== $b[$k]) {
return false;
}
}
// we have identical indexes, and no unequal values
return true;
}
}
/**
* 假銷售記錄Class
* Class StubSellRecord
*/
class StubSellRecord
{
protected $cost, $revenue, $sellPrice;
public function getCost()
{
return $this->cost;
}
public function getRevenue()
{
return $this->revenue;
}
public function getSellPrice()
{
return $this->sellPrice;
}
public function setCost($cost)
{
$this->cost = $cost;
return $this;
}
public function setRevenue($revenue)
{
$this->revenue = $revenue;
return $this;
}
public function setSellPrice($sellPrice)
{
$this->sellPrice = $sellPrice;
return $this;
}
}
@hatelove
Copy link

hatelove commented Mar 9, 2016

如果需要 1~100 筆一組 by Cost or Revenue, 您需要開幾個 function 呢?

@luckily
Copy link
Author

luckily commented Mar 9, 2016

謝謝Joey老師,已更新!

@hatelove
Copy link

good job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment