[phpsaves commit] r12 - in trunk: PHPSaves PHPSaves/Transaction tests/PHPSaves tests/PHPSaves/Transaction

0 views
Skip to first unread message

codesite...@google.com

unread,
Aug 28, 2008, 9:54:51 PM8/28/08
to cod...@googlegroups.com
Author: gorbiz
Date: Thu Aug 28 18:53:49 2008
New Revision: 12

Modified:
trunk/PHPSaves/Transaction.php
trunk/PHPSaves/Transaction/Collection.php
trunk/tests/PHPSaves/Transaction/CollectionTest.php
trunk/tests/PHPSaves/TransactionTest.php

Log:
Made sure that RSS items get orderd by date


Modified: trunk/PHPSaves/Transaction.php
==============================================================================
--- trunk/PHPSaves/Transaction.php (original)
+++ trunk/PHPSaves/Transaction.php Thu Aug 28 18:53:49 2008
@@ -88,4 +88,14 @@
return $this->newBalance;
}

+ /**
+ * Get Date as Unix Timestamp
+ *
+ * @return int
+ */
+ public function getTimestamp()
+ {
+ return strtotime($this->getDate());
+ }
+
}

Modified: trunk/PHPSaves/Transaction/Collection.php
==============================================================================
--- trunk/PHPSaves/Transaction/Collection.php (original)
+++ trunk/PHPSaves/Transaction/Collection.php Thu Aug 28 18:53:49 2008
@@ -34,7 +34,7 @@
public function toRss()
{
$rssItems = array();
- foreach ($this as $transaction) {
+ foreach ($this->orderByDate() as $transaction) {
$rssItems[] = $transaction->toRssItem() . PHP_EOL;
}

@@ -48,6 +48,33 @@
. '</rss>';

return $result;
+ }
+
+ /**
+ * Order transactions by date and return new
PHPSaves_Transaction_Collection
+ *
+ * @return PHPSaves_Transaction_Collection
+ */
+ private function orderByDate()
+ {
+ $orderdTransactions = $this->getArrayCopy();
+ usort($orderdTransactions, array($this, 'sortByDateCallback'));
+ return new self($orderdTransactions);
+ }
+
+ /**
+ * Sort callback function for sorting transactions by date
+ *
+ * @param PHPSaves_Transaction $a
+ * @param PHPSaves_Transaction $b
+ * @return int
+ */
+ private function sortByDateCallback(PHPSaves_Transaction $a,
PHPSaves_Transaction $b)
+ {
+ if ($a->getTimestamp() == $b->getTimestamp()) {
+ return 0;
+ }
+ return ($a->getTimestamp() > $b->getTimestamp()) ? -1 : 1;
}

/**

Modified: trunk/tests/PHPSaves/Transaction/CollectionTest.php
==============================================================================
--- trunk/tests/PHPSaves/Transaction/CollectionTest.php (original)
+++ trunk/tests/PHPSaves/Transaction/CollectionTest.php Thu Aug 28 18:53:49
2008
@@ -41,14 +41,40 @@

public function testCorrectOrderfromToRss()
{
- $this->markTestIncomplete('The items should be orderd by date');
+ $transactions = new PHPSaves_Transaction_Collection(array(
+ new PHPSaves_Transaction(-100.00, '08-05-29', 'Willys', 200.00),
+ new PHPSaves_Transaction(-50.00, '08-06-18', 'City Pizza')
+ ));
+
+ $expectedRss = '
+ <rss version="2.0">
+ <channel>
+ <title>transactions</title>
+ <link>http://www.example.com</link>
+ <description>transactions</description>
+ <item>
+ <title>-50.00 City Pizza</title>
+ <link>http://www.example.com</link>
+ <description>-50.00 City Pizza</description>
+ <pubDate>'.date('r', strtotime('08-06-18')).'</pubDate>
+ </item>
+ <item>
+ <title>-100.00 Willys</title>
+ <link>http://www.example.com</link>
+ <description>-100.00 Willys</description>
+ <pubDate>'.date('r', strtotime('08-05-29')).'</pubDate>
+ </item>
+ </channel>
+ </rss>';
+
+ $this->assertXmlStringEqualsXmlString($expectedRss,
$transactions->toRss());
}

public function testToQif()
{
$transactions = new PHPSaves_Transaction_Collection(array(
new PHPSaves_Transaction(-100.00, '08-05-29', 'Willys', 200.00),
- new PHPSaves_Transaction(-50.00, '11-06-18', 'City Pizza')
+ new PHPSaves_Transaction(-50.00, '08-06-18', 'City Pizza')
));

$expectedQif = '!Type:Cash' . PHP_EOL
@@ -58,7 +84,7 @@
. 'PWillys' . PHP_EOL
. 'MWillys' . PHP_EOL
. '^' . PHP_EOL
- . 'D18/06/11' . PHP_EOL
+ . 'D18/06/08' . PHP_EOL
. 'T-50.00' . PHP_EOL
. 'PCity Pizza' . PHP_EOL
. 'MCity Pizza' . PHP_EOL

Modified: trunk/tests/PHPSaves/TransactionTest.php
==============================================================================
--- trunk/tests/PHPSaves/TransactionTest.php (original)
+++ trunk/tests/PHPSaves/TransactionTest.php Thu Aug 28 18:53:49 2008
@@ -44,5 +44,11 @@
$this->assertEquals(0, get_magic_quotes_gpc());
}

+ public function testGetTimestamp()
+ {
+ $transaction = new PHPSaves_Transaction(-100.00, '08-05-29', 'Willys',
200.00);
+ $this->assertEquals(strtotime('2008-05-29'),
$transaction->getTimestamp());
+ }
+
}

Reply all
Reply to author
Forward
0 new messages