Skip to content

Commit

Permalink
Make isXXX available for bool properties only
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Kesselberg <[email protected]>
  • Loading branch information
kesselb committed Nov 15, 2019
1 parent 51d3f98 commit a27c10d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 14 deletions.
24 changes: 18 additions & 6 deletions lib/public/AppFramework/Db/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function getFieldTypes() {
return $this->_fieldTypes;
}


/**
* Marks the entity as clean needed for setting the id after the insertion
* @since 7.0.0
Expand Down Expand Up @@ -115,7 +115,7 @@ protected function setter($name, $args) {
$this->$name = $args[0];

} else {
throw new \BadFunctionCallException($name .
throw new \BadFunctionCallException($name .
' is not a valid attribute');
}
}
Expand All @@ -129,15 +129,15 @@ protected function getter($name) {
if(property_exists($this, $name)){
return $this->$name;
} else {
throw new \BadFunctionCallException($name .
throw new \BadFunctionCallException($name .
' is not a valid attribute');
}
}


/**
* Each time a setter is called, push the part after set
* into an array: for instance setId will save Id in the
* into an array: for instance setId will save Id in the
* updated fields array so it can be easily used to create the
* getter method
* @since 7.0.0
Expand All @@ -147,7 +147,7 @@ public function __call($methodName, $args) {
$this->setter(lcfirst(substr($methodName, 3)), $args);
} elseif (strpos($methodName, 'get') === 0) {
return $this->getter(lcfirst(substr($methodName, 3)));
} elseif (strpos($methodName, 'is') === 0) {
} elseif ($this->isGetterForBoolProperty($methodName)) {
return $this->getter(lcfirst(substr($methodName, 2)));
} else {
throw new \BadFunctionCallException($methodName .
Expand All @@ -156,6 +156,18 @@ public function __call($methodName, $args) {

}

/**
* @param string $methodName
* @return bool
* @since 18.0.0
*/
protected function isGetterForBoolProperty(string $methodName): bool {
if (strpos($methodName, 'is') === 0) {
$fieldName = lcfirst(substr($methodName, 2));
return isset($this->_fieldTypes[$fieldName]) && strpos($this->_fieldTypes[$fieldName], 'bool') === 0;
}
return false;
}

/**
* Mark am attribute as updated
Expand All @@ -168,7 +180,7 @@ protected function markFieldUpdated($attribute){


/**
* Transform a database columnname to a property
* Transform a database columnname to a property
* @param string $columnName the name of the column
* @return string the property name
* @since 7.0.0
Expand Down
43 changes: 35 additions & 8 deletions tests/lib/AppFramework/Db/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@


use OCP\AppFramework\Db\Entity;
use PHPUnit\Framework\Constraint\IsType;


/**
Expand All @@ -38,18 +39,28 @@
* @method void setEmail(string $email)
* @method string getPreName()
* @method void setPreName(string $preName)
* @method bool getTrueOrFalse()
* @method bool isTrueOrFalse()
* @method void setTrueOrFalse(bool $trueOrFalse)
* @method bool getAnotherBool()
* @method bool isAnotherBool()
* @method void setAnotherBool(bool $anotherBool)
*/
class TestEntity extends Entity {
protected $name;
protected $email;
protected $testId;
protected $preName;
protected $trueOrFalse;
protected $anotherBool;

public function __construct($name=null){
public function __construct($name = null) {
$this->addType('testId', 'integer');
$this->addType('trueOrFalse', 'bool');
$this->addType('anotherBool', 'boolean');
$this->name = $name;
}
};
}


class EntityTest extends \Test\TestCase {
Expand All @@ -73,7 +84,7 @@ public function testResetUpdatedFields(){

public function testFromRow(){
$row = array(
'pre_name' => 'john',
'pre_name' => 'john',
'email' => '[email protected]'
);
$this->entity = TestEntity::fromRow($row);
Expand All @@ -93,21 +104,21 @@ public function testGetSetId(){

public function testColumnToPropertyNoReplacement(){
$column = 'my';
$this->assertEquals('my',
$this->assertEquals('my',
$this->entity->columnToProperty($column));
}


public function testColumnToProperty(){
$column = 'my_attribute';
$this->assertEquals('myAttribute',
$this->assertEquals('myAttribute',
$this->entity->columnToProperty($column));
}


public function testPropertyToColumnNoReplacement(){
$property = 'my';
$this->assertEquals('my',
$this->assertEquals('my',
$this->entity->propertyToColumn($property));
}

Expand Down Expand Up @@ -137,7 +148,6 @@ public function testGetterShouldFailIfAttributeNotDefined(){
/**
* @expectedException \BadFunctionCallException
*/

public function testSetterShouldFailIfAttributeNotDefined(){
$this->entity->setTest();
}
Expand Down Expand Up @@ -208,7 +218,9 @@ public function testGetFieldTypes() {
$entity = new TestEntity();
$this->assertEquals(array(
'id' => 'integer',
'testId' => 'integer'
'testId' => 'integer',
'trueOrFalse' => 'bool',
'anotherBool' => 'boolean',
), $entity->getFieldTypes());
}

Expand All @@ -226,5 +238,20 @@ public function testFieldsNotMarkedUpdatedIfNothingChanges() {
$this->assertEquals(0, count($entity->getUpdatedFields()));
}

public function testIsGetter() {
$entity = new TestEntity();
$entity->setTrueOrFalse(false);
$entity->setAnotherBool(false);
$this->assertThat($entity->isTrueOrFalse(), new IsType(IsType::TYPE_BOOL));
$this->assertThat($entity->isAnotherBool(), new IsType(IsType::TYPE_BOOL));
}

/**
* @expectedException BadFunctionCallException
*/
public function testIsGetterShoudFailForOtherType() {
$entity = new TestEntity();
$entity->setName('hello');
$this->assertThat($entity->isName(), new IsType(IsType::TYPE_BOOL));
}
}

0 comments on commit a27c10d

Please sign in to comment.