Author: gorbiz
Date: Thu Aug 28 18:08:24 2008
New Revision: 11
Added:
trunk/PHPSaves/Transaction/
trunk/PHPSaves/Transaction/Collection.php (contents, props changed)
trunk/tests/PHPSaves/Transaction/
trunk/tests/PHPSaves/Transaction/AllTests.php (contents, props changed)
trunk/tests/PHPSaves/Transaction/CollectionTest.php (contents, props
changed)
Modified:
trunk/PHPSaves/Parser/Abstract.php
trunk/PHPSaves/Parser/Swedbank.php
trunk/PHPSaves/Transaction.php
trunk/examples/qif.php
trunk/tests/PHPSaves/AllTests.php
trunk/tests/PHPSaves/Parser/AllTests.php
trunk/tests/PHPSaves/Parser/SwedbankTest.php
trunk/tests/PHPSaves/Parser/_files/threeTransactions.qif
trunk/tests/PHPSaves/TransactionTest.php
Log:
Implemented Issue 4
Introduces PHPSaves_Transaction_Collection and moved the transaction
conversion methods there (from the parser
classes)
Modified: trunk/PHPSaves/Parser/Abstract.php
==============================================================================
--- trunk/PHPSaves/Parser/Abstract.php (original)
+++ trunk/PHPSaves/Parser/Abstract.php Thu Aug 28 18:08:24 2008
@@ -2,13 +2,13 @@
/**
* Enter description here...
*
- * @package Buxfer_Parser
+ * @package PHPSaves_Parser
*/
/**
* Enter description here...
*
- * @package Buxfer_Parser
+ * @package PHPSaves_Parser
*/
abstract class PHPSaves_Parser_Abstract
{
@@ -29,21 +29,5 @@
* @return array<PHPSaves_Transaction>
*/
abstract public function parse($html);
-
- /**
- * Enter description here...
- *
- * @param array<PHPSaves_Transaction> $transactions
- * @return string
- */
- public function toQif($transactions)
- {
- $result = "!Type:Cash" . PHP_EOL
- . PHP_EOL;
- foreach ($transactions as $transaction) {
- $result .= $transaction->toQif() . PHP_EOL;
- }
- return $result;
- }
}
Modified: trunk/PHPSaves/Parser/Swedbank.php
==============================================================================
--- trunk/PHPSaves/Parser/Swedbank.php (original)
+++ trunk/PHPSaves/Parser/Swedbank.php Thu Aug 28 18:08:24 2008
@@ -2,18 +2,20 @@
/**
* Enter description here...
*
- * @package Buxfer_Parser
+ * @package PHPSaves_Parser
*/
/** PHPSaves_Parser_Abstract */
require_once 'PHPSaves/Parser/Abstract.php';
/** PHPSaves_Transaction */
require_once 'PHPSaves/Transaction.php';
+/** PHPSaves_Transaction_Collection */
+require_once 'PHPSaves/Transaction/Collection.php';
/**
* Enter description here...
*
- * @package Buxfer_Parser
+ * @package PHPSaves_Parser
*/
class PHPSaves_Parser_Swedbank extends PHPSaves_Parser_Abstract
{
@@ -73,7 +75,7 @@
$transactions[] = new PHPSaves_Transaction($amount, $date, $event,
$balance);
}
- return $transactions;
+ return new PHPSaves_Transaction_Collection($transactions);
}
protected function htmlEntityTrim($html)
Modified: trunk/PHPSaves/Transaction.php
==============================================================================
--- trunk/PHPSaves/Transaction.php (original)
+++ trunk/PHPSaves/Transaction.php Thu Aug 28 18:08:24 2008
@@ -2,13 +2,13 @@
/**
* Enter description here...
*
- * @package Buxfer
+ * @package PHPSaves
*/
/**
* Enter description here...
*
- * @package Buxfer
+ * @package PHPSaves
*/
class PHPSaves_Transaction
{
@@ -42,6 +42,18 @@
. 'M' . $this->getEvent() . PHP_EOL // "Memo" or something
. '^' . PHP_EOL;
return $qif;
+ }
+
+ public function toRssItem()
+ {
+ $summary = number_format($this->getAmount(), 2) . ' ' .
$this->getEvent();
+ $result = '<item>' . PHP_EOL
+ . '<title>' . $summary . '</title>' . PHP_EOL
+ . '<link>
http://www.example.com</link>' . PHP_EOL
+ . '<description>' . $summary . '</description>' . PHP_EOL
+ . '<pubDate>'.date('r', strtotime($this->getDate())).'</pubDate>' .
PHP_EOL
+ . '</item>' . PHP_EOL;
+ return $result;
}
/**
Added: trunk/PHPSaves/Transaction/Collection.php
==============================================================================
--- (empty file)
+++ trunk/PHPSaves/Transaction/Collection.php Thu Aug 28 18:08:24 2008
@@ -0,0 +1,69 @@
+<?php
+/**
+ * Enter description here...
+ *
+ * @package PHPSaves
+ */
+
+/**
+ * Enter description here...
+ *
+ * @package PHPSaves
+ */
+class PHPSaves_Transaction_Collection extends ArrayIterator
+{
+ /**
+ * Enter description here...
+ *
+ * @param mixed $items A PHPSaves_Transaction on an
array<PHPSaves_Transaction>
+ * @param int $flags
+ */
+ public function __construct($items, $flags = 0)
+ {
+ if (! is_array($items)) {
+ $items = array($items);
+ }
+ parent::__construct($items, $flags);
+ }
+
+ /**
+ * Enter description here...
+ *
+ * @return string The complete Feed in RSS 2.0 format
+ */
+ public function toRss()
+ {
+ $rssItems = array();
+ foreach ($this as $transaction) {
+ $rssItems[] = $transaction->toRssItem() . PHP_EOL;
+ }
+
+ $result = '<rss version="2.0">'
+ . ' <channel>'
+ . ' <title>transactions</title>'
+ . ' <link>
http://www.example.com</link>'
+ . ' <description>transactions</description>'
+ . implode(PHP_EOL, $rssItems)
+ . ' </channel>'
+ . '</rss>';
+
+ return $result;
+ }
+
+ /**
+ * Enter description here...
+ *
+ * @param array<PHPSaves_Transaction> $transactions
+ * @return string
+ */
+ public function toQif()
+ {
+ $result = "!Type:Cash" . PHP_EOL
+ . PHP_EOL;
+ foreach ($this as $transaction) {
+ $result .= $transaction->toQif();
+ }
+ return $result;
+ }
+
+}
Modified: trunk/examples/qif.php
==============================================================================
--- trunk/examples/qif.php (original)
+++ trunk/examples/qif.php Thu Aug 28 18:08:24 2008
@@ -8,4 +8,4 @@
header('Content-Type: application/qif; charset=iso-8859-1');
$parser = new PHPSaves_Parser_Swedbank;
-echo
utf8_decode($parser->toQif($parser->parse($_REQUEST['transactionHtml'])));
+echo utf8_decode($parser->parse($_REQUEST['transactionHtml'])->toQif());
Modified: trunk/tests/PHPSaves/AllTests.php
==============================================================================
--- trunk/tests/PHPSaves/AllTests.php (original)
+++ trunk/tests/PHPSaves/AllTests.php Thu Aug 28 18:08:24 2008
@@ -2,6 +2,7 @@
require_once 'PHPUnit/Framework/TestSuite.php';
require_once 'tests/PHPSaves/Parser/AllTests.php';
require_once 'tests/PHPSaves/TransactionTest.php';
+require_once 'tests/PHPSaves/Transaction/AllTests.php';
/**
* Static test suite.
@@ -17,6 +18,7 @@
$this->setName('PHPSaves_AllTests');
$this->addTestSuite('PHPSaves_Parser_AllTests');
$this->addTestSuite('PHPSaves_TransactionTest');
+ $this->addTestSuite('PHPSaves_Transaction_CollectionTest');
}
/**
Modified: trunk/tests/PHPSaves/Parser/AllTests.php
==============================================================================
--- trunk/tests/PHPSaves/Parser/AllTests.php (original)
+++ trunk/tests/PHPSaves/Parser/AllTests.php Thu Aug 28 18:08:24 2008
@@ -14,7 +14,7 @@
public function __construct()
{
$this->setName('PHPSaves_Parser_AllTests');
- $this->addTestSuite('PHPSaves_SwedbankTest');
+ $this->addTestSuite('PHPSaves_Parser_SwedbankTest');
}
/**
Modified: trunk/tests/PHPSaves/Parser/SwedbankTest.php
==============================================================================
--- trunk/tests/PHPSaves/Parser/SwedbankTest.php (original)
+++ trunk/tests/PHPSaves/Parser/SwedbankTest.php Thu Aug 28 18:08:24 2008
@@ -3,7 +3,7 @@
require_once 'PHPSaves/Parser/Swedbank.php';
require_once 'PHPUnit/Framework/TestCase.php';
-class PHPSaves_SwedbankTest extends PHPUnit_Framework_TestCase
+class PHPSaves_Parser_SwedbankTest extends PHPUnit_Framework_TestCase
{
/**
@@ -66,13 +66,19 @@
$this->assertEquals(21391.49, $transaction->getAmount());
}
+ public function testParseReturnsTransactionCollection()
+ {
+ $transactions =
$this->swedbank->parse(file_get_contents(dirname(__FILE__) . '/_files/threeTransactions.html'));
+ $this->assertType('PHPSaves_Transaction_Collection', $transactions);
+ }
+
public function testToQif()
{
$transactions =
$this->swedbank->parse(file_get_contents(dirname(__FILE__) . '/_files/threeTransactions.html'));
$this->assertEquals(
file_get_contents(dirname(__FILE__) . '/_files/threeTransactions.qif'),
- $this->swedbank->toQif($transactions));
+ $transactions->toQif());
}
}
Modified: trunk/tests/PHPSaves/Parser/_files/threeTransactions.qif
==============================================================================
--- trunk/tests/PHPSaves/Parser/_files/threeTransactions.qif (original)
+++ trunk/tests/PHPSaves/Parser/_files/threeTransactions.qif Thu Aug 28
18:08:24 2008
@@ -5,16 +5,13 @@
PRESTAURANG VASAS
MRESTAURANG VASAS
^
-
D12/07/08
T-200.00
PSJ KUNDBOKNING I
MSJ KUNDBOKNING I
^
-
D14/07/08
T-3,000.00
PÖvf via internet 810599042562398
MÖvf via internet 810599042562398
^
-
Added: trunk/tests/PHPSaves/Transaction/AllTests.php
==============================================================================
--- (empty file)
+++ trunk/tests/PHPSaves/Transaction/AllTests.php Thu Aug 28 18:08:24 2008
@@ -0,0 +1,28 @@
+<?php
+require_once 'PHPUnit/Framework/TestSuite.php';
+require_once 'tests/PHPSaves/Transaction/CollectionTest.php';
+
+/**
+ * Static test suite.
+ */
+class PHPSaves_Transaction_AllTests extends PHPUnit_Framework_TestSuite
+{
+
+ /**
+ * Constructs the test suite handler.
+ */
+ public function __construct()
+ {
+ $this->setName('PHPSaves_Transaction_AllTests');
+ $this->addTestSuite('PHPSaves_Transaction_CollectionTest');
+ }
+
+ /**
+ * Creates the suite.
+ */
+ public static function suite()
+ {
+ return new self();
+ }
+}
+
Added: trunk/tests/PHPSaves/Transaction/CollectionTest.php
==============================================================================
--- (empty file)
+++ trunk/tests/PHPSaves/Transaction/CollectionTest.php Thu Aug 28 18:08:24
2008
@@ -0,0 +1,70 @@
+<?php
+require_once 'PHPSaves/Transaction/Collection.php';
+require_once 'PHPSaves/Transaction.php';
+require_once 'PHPUnit/Framework/TestCase.php';
+
+/**
+ * PHPSaves_Transaction test case.
+ */
+class PHPSaves_Transaction_CollectionTest extends
PHPUnit_Framework_TestCase
+{
+
+ public function testCreateCollectionWithOneTransaction()
+ {
+ $transactions = new PHPSaves_Transaction_Collection(
+ new PHPSaves_Transaction(-100.00, '08-05-29', 'Willys', 200.00));
+ $this->assertType('PHPSaves_Transaction', current($transactions));
+ }
+
+ public function testToRss()
+ {
+ $transactions = new PHPSaves_Transaction_Collection(
+ new PHPSaves_Transaction(-100.00, '08-05-29', 'Willys', 200.00));
+
+ $expectedRss = '
+ <rss version="2.0">
+ <channel>
+ <title>transactions</title>
+ <link>
http://www.example.com</link>
+ <description>transactions</description>
+ <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 testCorrectOrderfromToRss()
+ {
+ $this->markTestIncomplete('The items should be orderd by date');
+ }
+
+ 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')
+ ));
+
+ $expectedQif = '!Type:Cash' . PHP_EOL
+ . PHP_EOL
+ .'D29/05/08' . PHP_EOL
+ . 'T-100.00' . PHP_EOL
+ . 'PWillys' . PHP_EOL
+ . 'MWillys' . PHP_EOL
+ . '^' . PHP_EOL
+ . 'D18/06/11' . PHP_EOL
+ . 'T-50.00' . PHP_EOL
+ . 'PCity Pizza' . PHP_EOL
+ . 'MCity Pizza' . PHP_EOL
+ . '^' . PHP_EOL;
+
+ $this->assertEquals($expectedQif, $transactions->toQif());
+ }
+
+}
Modified: trunk/tests/PHPSaves/TransactionTest.php
==============================================================================
--- trunk/tests/PHPSaves/TransactionTest.php (original)
+++ trunk/tests/PHPSaves/TransactionTest.php Thu Aug 28 18:08:24 2008
@@ -20,6 +20,21 @@
$this->assertEquals($expectedQif, $transaction->toQif());
}
+ public function testToRssItem()
+ {
+ $transaction = new PHPSaves_Transaction(-100.00, '08-05-29', 'Willys',
200.00);
+
+ $expectedRss = '
+ <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>';
+
+ $this->assertXmlStringEqualsXmlString($expectedRss,
$transaction->toRssItem());
+ }
+
/**
* Related to issue #3
*/