summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBernhard Posselt <nukeawhale@gmail.com>2013-03-20 10:46:37 +0100
committerBernhard Posselt <nukeawhale@gmail.com>2013-03-20 10:46:37 +0100
commitfa3a2e469db519571fa7dc13bc832e7de1f8057f (patch)
tree44a3a31876e9d1b3365b848a235477b86a18d1e8
parentacb40d091fc096674b12ab3c95cabf49a42172c9 (diff)
throw exceptions when attributes dont exist
-rw-r--r--db/entity.php33
-rw-r--r--db/item.php38
-rw-r--r--db/itemmapper.php6
-rw-r--r--db/statusflag.php25
-rw-r--r--tests/db/EntityTest.php52
-rw-r--r--tests/db/ItemMapperTest.php6
-rw-r--r--tests/db/ItemTest.php43
7 files changed, 121 insertions, 82 deletions
diff --git a/db/entity.php b/db/entity.php
index 5654e1b80..31a0296cd 100644
--- a/db/entity.php
+++ b/db/entity.php
@@ -42,17 +42,36 @@ abstract class Entity {
* getter method
*/
public function __call($methodName, $args){
+
+ // setters
if(strpos($methodName, 'set') === 0){
- $setterPart = substr($methodName, 3);
- $attr = lcfirst($setterPart);
+ $attr = lcfirst( substr($methodName, 3) );
- $this->markFieldUpdated($attr);
- $this->$attr = $args[0];
+ // setters should only work for existing attributes
+ if(property_exists($this, $attr)){
+ $this->markFieldUpdated($attr);
+ $this->$attr = $args[0];
+ } else {
+ throw new \BadFunctionCallException($attr .
+ ' is not a valid attribute');
+ }
+
+ // getters
} elseif(strpos($methodName, 'get') === 0) {
- $getterPart = substr($methodName, 3);
- $attr = lcfirst($getterPart);
- return $this->$attr;
+ $attr = lcfirst( substr($methodName, 3) );
+
+ // getters should only work for existing attributes
+ if(property_exists($this, $attr)){
+ return $this->$attr;
+ } else {
+ throw new \BadFunctionCallException($attr .
+ ' is not a valid attribute');
+ }
+ } else {
+ throw new \BadFunctionCallException($methodName .
+ ' does not exist');
}
+
}
diff --git a/db/item.php b/db/item.php
index ad245d486..66ee0ef7e 100644
--- a/db/item.php
+++ b/db/item.php
@@ -1,12 +1,25 @@
<?php
+
/**
-* ownCloud - News app
+* ownCloud - News
*
* @author Alessandro Cosentino
-* Copyright (c) 2012 - Alessandro Cosentino <cosenal@gmail.com>
+* @author Bernhard Posselt
+* @copyright 2012 Alessandro Cosentino cosenal@gmail.com
+* @copyright 2012 Bernhard Posselt nukeawhale@gmail.com
+*
+* This library is free software; you can redistribute it and/or
+* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+* License as published by the Free Software Foundation; either
+* version 3 of the License, or any later version.
+*
+* This library is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
-* This file is licensed under the Affero General Public License version 3 or later.
-* See the COPYING-README file
+* You should have received a copy of the GNU Affero General Public
+* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
@@ -25,6 +38,15 @@ class Item extends Entity {
public $feedTitle;
public $enclosure;
+ public function setRead() {
+ $this->markFieldUpdated('status');
+ $this->status &= ~StatusFlag::UNREAD;
+ }
+
+ public function isRead() {
+ return !($this->status & StatusFlag::UNREAD);
+ }
+
}
@@ -60,17 +82,13 @@ class Item extends Entity {
- public function setRead() {
- $this->status &= ~StatusFlag::UNREAD;
- }
+
public function setUnread() {
$this->status |= StatusFlag::UNREAD;
}
- public function isRead() {
- return !($this->status & StatusFlag::UNREAD);
- }
+
public function setImportant() {
$this->status |= StatusFlag::IMPORTANT;
diff --git a/db/itemmapper.php b/db/itemmapper.php
index 7dca01dcc..e79cabb7c 100644
--- a/db/itemmapper.php
+++ b/db/itemmapper.php
@@ -23,9 +23,9 @@ class ItemMapper extends Mapper {
public function findAllFromFeed($feedId, $userId){
- $sql = 'SELECT * FROM `*PREFIX*news_items`
- WHERE user_id = ?
- AND feed_id = ?';
+ $sql = 'SELECT * FROM `*PREFIX*news_items` ' .
+ 'WHERE user_id = ? ' .
+ 'AND feed_id = ?';
$result = $this->execute($sql, array($feedId, $userId));
$items = array();
diff --git a/db/statusflag.php b/db/statusflag.php
index c4920a192..1b01ab42b 100644
--- a/db/statusflag.php
+++ b/db/statusflag.php
@@ -1,18 +1,31 @@
<?php
+
/**
-* ownCloud - News app
+* ownCloud - News
*
* @author Alessandro Cosentino
-* Copyright (c) 2012 - Alessandro Cosentino <cosenal@gmail.com>
+* @author Bernhard Posselt
+* @copyright 2012 Alessandro Cosentino cosenal@gmail.com
+* @copyright 2012 Bernhard Posselt nukeawhale@gmail.com
+*
+* This library is free software; you can redistribute it and/or
+* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+* License as published by the Free Software Foundation; either
+* version 3 of the License, or any later version.
+*
+* This library is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
-* This file is licensed under the Affero General Public License version 3 or later.
-* See the COPYING-README file
+* You should have received a copy of the GNU Affero General Public
+* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
-namespace OCA\News;
+namespace OCA\News\Db;
-class StatusFlag{
+class StatusFlag {
const UNREAD = 0x02;
const IMPORTANT = 0x04;
const DELETED = 0x08;
diff --git a/tests/db/EntityTest.php b/tests/db/EntityTest.php
index 793ca8ded..9002f5f5e 100644
--- a/tests/db/EntityTest.php
+++ b/tests/db/EntityTest.php
@@ -36,8 +36,10 @@ class TestEntity extends Entity {
class EntityTest extends \PHPUnit_Framework_TestCase {
- protected function setUp(){
+ private $entity;
+ protected function setUp(){
+ $this->entity = new TestEntity();
}
@@ -46,54 +48,70 @@ class EntityTest extends \PHPUnit_Framework_TestCase {
'pre_name' => 'john',
'email' => 'john@something.com'
);
- $entity = new TestEntity();
+ $this->entity = new TestEntity();
- $entity->fromRow($row);
+ $this->entity->fromRow($row);
- $this->assertEquals($row['pre_name'], $entity->getPreName());
- $this->assertEquals($row['email'], $entity->getEmail());
+ $this->assertEquals($row['pre_name'], $this->entity->getPreName());
+ $this->assertEquals($row['email'], $this->entity->getEmail());
}
public function testGetSetId(){
$id = 3;
- $entity = new TestEntity();
- $entity->setId(3);
+ $this->entity->setId(3);
- $this->assertEquals($id, $entity->getId());
+ $this->assertEquals($id, $this->entity->getId());
}
public function testColumnToPropertyNoReplacement(){
$column = 'my';
- $entity = new TestEntity();
$this->assertEquals('my',
- $entity->columnToProperty($column));
+ $this->entity->columnToProperty($column));
}
public function testColumnToProperty(){
$column = 'my_attribute';
- $entity = new TestEntity();
$this->assertEquals('myAttribute',
- $entity->columnToProperty($column));
+ $this->entity->columnToProperty($column));
}
public function testPropertyToColumnNoReplacement(){
$property = 'my';
- $entity = new TestEntity();
$this->assertEquals('my',
- $entity->propertyToColumn($property));
+ $this->entity->propertyToColumn($property));
}
public function testSetterMarksFieldUpdated(){
$id = 3;
- $entity = new TestEntity();
- $entity->setId(3);
+ $this->entity->setId(3);
+
+ $this->assertContains('id', $this->entity->getUpdatedFields());
+ }
+
+
+ public function testCallShouldOnlyWorkForGetterSetter(){
+ $this->setExpectedException('\BadFunctionCallException');
+
+ $this->entity->something();
+ }
+
+
+ public function testGetterShouldFailIfAttributeNotDefined(){
+ $this->setExpectedException('\BadFunctionCallException');
+
+ $this->entity->getTest();
+ }
+
+
+ public function testSetterShouldFailIfAttributeNotDefined(){
+ $this->setExpectedException('\BadFunctionCallException');
- $this->assertContains('id', $entity->getUpdatedFields());
+ $this->entity->setTest();
}
} \ No newline at end of file
diff --git a/tests/db/ItemMapperTest.php b/tests/db/ItemMapperTest.php
index ab7cfa043..40ff691b6 100644
--- a/tests/db/ItemMapperTest.php
+++ b/tests/db/ItemMapperTest.php
@@ -59,9 +59,9 @@ class Test extends \OCA\AppFramework\Utility\MapperTestUtility {
array('test' => 1),
array('test' => 2)
);
- $sql = 'SELECT * FROM `*PREFIX*news_items`
- WHERE user_id = ?
- AND feed_id = ?';
+ $sql = 'SELECT * FROM `*PREFIX*news_items` ' .
+ 'WHERE user_id = ? ' .
+ 'AND feed_id = ?';
$this->setMapperResult($sql, array($feedId, $userId), $rows);
diff --git a/tests/db/ItemTest.php b/tests/db/ItemTest.php
index 35eb2bec4..a590856c4 100644
--- a/tests/db/ItemTest.php
+++ b/tests/db/ItemTest.php
@@ -30,46 +30,17 @@ require_once(__DIR__ . "/../classloader.php");
class ItemTest extends \PHPUnit_Framework_TestCase {
+ private $item;
- protected function assertSetterGetter($name){
- $value = 'value';
-
- $item = new Item();
- $setMethod = 'set' . $name;
- $getMethod = 'get' . $name;
- $item->$setMethod($value);
-
- $this->assertEquals($value, $item->$getMethod());
- }
-
-
- public function testGetUrl(){
- $this->assertSetterGetter('Url');
- }
-
-
- public function testSetFeedId(){
- $this->assertSetterGetter('FeedId');
- }
-
-
- public function testSetGUID(){
- $this->assertSetterGetter('GUID');
- }
-
-
- public function testSetStatus(){
- $this->assertSetterGetter('Status');
- }
-
-
- public function testSetTitle(){
- $this->assertSetterGetter('Title');
+ protected function setUp(){
+ $this->item = new Item();
+ $this->item->setStatus(0);
}
+ public function testSetRead(){
+ $this->item->setRead();
- public function testSetFeedTitle(){
- $this->assertSetterGetter('FeedTitle');
+ $this->assertTrue($this->item->isRead());
}
} \ No newline at end of file