Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Renamed folder

git-svn-id: http://mongodb-morph.googlecode.com/svn/trunk@21 406649b4-5d4b-11de-8abc-d1c337b90d21
  • Loading branch information...
commit 474507718fbadbed21fd6526c1a5d908c8232f7b 1 parent 0391f3b
XirisR authored
View
26 mongodb-morph/unit-tests/ForTesting.php
@@ -0,0 +1,26 @@
+<?php
+/**
+ * @package Morph
+ * @author Jonathan Moss <xirisr@gmail.com>
+ * @copyright 2009 Jonathan Moss
+ * @version SVN: $Id$
+ */
+
+/**
+ * Short summary of class
+ *
+ * Optional long description
+ *
+ * @package Morph
+ */
+class Morph_ForTesting extends Morph_Object
+{
+
+ public function __construct($id = null)
+ {
+ parent::__construct($id);
+ $this->addProperty(new Morph_Property_Generic('TestField'));
+ }
+
+}
+?>
View
24 mongodb-morph/unit-tests/MorphTests.php
@@ -0,0 +1,24 @@
+<?php
+require_once 'PHPUnit/Framework.php';
+
+require_once dirname(__FILE__).'/TestPropertySet.php';
+require_once dirname(__FILE__).'/TestUtils.php';
+require_once dirname(__FILE__).'/Property/TestProperties.php';
+require_once dirname(__FILE__).'/Query/TestProperty.php';
+require_once dirname(__FILE__).'/TestQuery.php';
+require_once dirname(__FILE__).'/TestObject.php';
+
+class MorphTests
+{
+ public static function suite()
+ {
+ $suite = new PHPUnit_Framework_TestSuite();
+ $suite->addTestSuite('TestPropertySet');
+ $suite->addTestSuite('TestUtils');
+ $suite->addTestSuite('TestProperties');
+ $suite->addTestSuite('TestProperty');
+ $suite->addTestSuite('TestQuery');
+ $suite->addTestSuite('TestObject');
+ return $suite;
+ }
+}
View
119 mongodb-morph/unit-tests/Property/TestProperties.php
@@ -0,0 +1,119 @@
+<?php
+require_once 'PHPUnit/Framework/TestCase.php';
+require_once dirname(__FILE__).'/../../src/Morph/Property/Generic.php';
+require_once dirname(__FILE__).'/../../src/Morph/Property/String.php';
+require_once dirname(__FILE__).'/../../src/Morph/Property/Integer.php';
+require_once dirname(__FILE__).'/../../src/Morph/Property/Float.php';
+require_once dirname(__FILE__).'/../../src/Morph/Property/Enum.php';
+require_once dirname(__FILE__).'/../../src/Morph/Property/Date.php';
+
+/**
+ * @package Morph
+ * @subpackage Property
+ */
+class TestProperties extends PHPUnit_Framework_TestCase
+{
+
+ public function testDefault()
+ {
+ $property = new Morph_Property_Generic('TestProperty', 'TestValue');
+ $this->assertEquals('TestValue', $property->getValue());
+ }
+
+ public function testName()
+ {
+ $property = new Morph_Property_Generic('TestProperty', 'TestValue');
+ $this->assertEquals('TestProperty', $property->getName());
+ }
+
+ public function testValue()
+ {
+ $property = new Morph_Property_Generic('TestProperty', 'TestValue');
+ $property->setValue('AValue');
+ $this->assertEquals('AValue', $property->getValue());
+ }
+
+ public function testStringProperty()
+ {
+ $validString = 'ABCDEFGHIJKLMNOP';
+ $invalidString = 'ABCDEFGHIJKLMNOPQR';
+ $property = new Morph_Property_String('Name', null, 16);
+ $property->setValue($validString);
+ $this->assertEquals($validString, $property->getValue());
+ $property->setValue($invalidString);
+ $this->assertEquals(16, strlen($property->getValue()));
+ }
+
+ public function testIntegerProperty()
+ {
+ $min = 1;
+ $max = 100;
+ $validInteger = 5;
+ $toLarge = 102;
+ $toSmall = 0;
+ $property = new Morph_Property_Integer('AProperty', null, $min, $max);
+
+ $property->setValue($validInteger);
+ $this->assertEquals($validInteger, $property->getValue());
+
+ $property->setValue($toLarge);
+ $this->assertEquals($max, $property->getValue());
+
+ $property->setValue($toSmall);
+ $this->assertEquals($min, $property->getValue());
+ }
+
+ public function testFloatProperty()
+ {
+ $min = 1.0;
+ $max = 100.00;
+ $validFloat = 5.0;
+ $toLarge = 100.1;
+ $toSmall = 0.1;
+ $property = new Morph_Property_Float('AProperty', null, $min, $max);
+
+ $property->setValue($validFloat);
+ $this->assertEquals($validFloat, $property->getValue());
+
+ $property->setValue($toLarge);
+ $this->assertEquals($max, $property->getValue());
+
+ $property->setValue($toSmall);
+ $this->assertEquals($min, $property->getValue());
+ }
+
+ public function testEnumProperty()
+ {
+ $enums = array('Yes','No');
+ $valid = 'Yes';
+ $invalid = 'Bob';
+
+ $property = new Morph_Property_Enum('AProperty', $valid, $enums);
+
+ $this->assertEquals($enums, $property->getEnums());
+
+ $property->setValue($valid);
+ $this->assertEquals($valid, $property->getValue());
+
+ $property->setValue($invalid);
+ $this->assertNotEquals($invalid, $property->getValue());
+ }
+
+ public function testDateProperty()
+ {
+
+ $property = new Morph_Property_Date('AProperty');
+ $time = time();
+
+ //check it stores the value correctly
+ $property->setValue($time);
+ $this->assertEquals($time, $property->getValue());
+
+ //check the output type is correct
+ $this->assertType('MongoDate', $property->__getRawValue());
+
+ //check the MorphDate objects content is correct
+ $this->assertEquals($time, $property->__getRawValue()->sec);
+ }
+}
+?>
View
110 mongodb-morph/unit-tests/Query/TestProperty.php
@@ -0,0 +1,110 @@
+<?php
+require_once 'PHPUnit/Framework/TestCase.php';
+
+require_once dirname(__FILE__).'/../../src/Morph/Query/Property.php';
+
+/**
+ * Short summary of class
+ *
+ * Optional long description
+ *
+ * @package Morph
+ * @subpackage Query
+ */
+class TestProperty extends PHPUnit_Framework_TestCase
+{
+
+
+ public function testEquals()
+ {
+ $property = new Morph_Query_Property($this->getMockQuery());
+ $property->equals('ABC');
+ $this->assertEquals('ABC', $property->getConstraints());
+ }
+
+ public function testLessThan()
+ {
+ $expected = array('$lt' => 25);
+ $property = new Morph_Query_Property($this->getMockQuery());
+ $property->lessThan(25);
+ $this->assertEquals($expected, $property->getConstraints());
+ }
+
+ public function testInvalidConstraintCombo()
+ {
+ $property = new Morph_Query_Property($this->getMockQuery());
+ $property->equals('ABC');
+ $this->setExpectedException('RuntimeException');
+ $property->greaterThan(25);
+ }
+
+ public function testCombinationConstraints()
+ {
+ $expected = array('$lte' => 25, '$gte' => 10);
+ $property = new Morph_Query_Property($this->getMockQuery());
+ $property->lessThanOrEqualTo(25);
+ $property->greaterThanOrEqualTo(10);
+ $this->assertEquals($expected, $property->getConstraints());
+ }
+
+ public function testCombinationConstraintsFluent()
+ {
+ $expected = array('$lte' => 25, '$gte' => 10);
+ $property = new Morph_Query_Property($this->getMockQuery());
+ $property->lessThanOrEqualTo(25)
+ ->greaterThanOrEqualTo(10);
+ $this->assertEquals($expected, $property->getConstraints());
+ }
+
+ public function testNotEqualTo()
+ {
+ $expected = array('$ne' => 25);
+ $property = new Morph_Query_Property($this->getMockQuery());
+ $property->notEqualTo(25);
+ $this->assertEquals($expected, $property->getConstraints());
+ }
+
+ public function testAll()
+ {
+ $expected = array('$all' => array('a', 'b', 'c'));
+ $property = new Morph_Query_Property($this->getMockQuery());
+ $property->all(array('a', 'b', 'c'));
+ $this->assertEquals($expected, $property->getConstraints());
+ }
+
+ public function testIn()
+ {
+ $expected = array('$in' => array('a', 'b', 'c'));
+ $property = new Morph_Query_Property($this->getMockQuery());
+ $property->in(array('a', 'b', 'c'));
+ $this->assertEquals($expected, $property->getConstraints());
+ }
+
+ public function testPropertyFluency()
+ {
+ $property = new Morph_Query_Property($this->getMockQuery(true));
+ $newField = $property->property('bob');
+ $this->assertTrue($newField instanceof Morph_Query_Property);
+ }
+
+ public function testRegexConstraint()
+ {
+ $property = new Morph_Query_Property($this->getMockQuery(false));
+ $property->regex('/abc.*/');
+ $this->assertTrue(($property->getConstraints() instanceof MongoRegex));
+
+ }
+
+ private function getMockQuery($willCallField = false)
+ {
+ $query = $this->getMock('Morph_Query', array('property'));
+ if ($willCallField) {
+ $query->expects($this->once())
+ ->method('property')
+ ->will($this->returnValue(new Morph_Query_Property($query)));
+ }
+ return $query;
+ }
+
+}
+?>
View
82 mongodb-morph/unit-tests/TestObject.php
@@ -0,0 +1,82 @@
+<?php
+require_once 'PHPUnit/Framework/TestCase.php';
+
+
+require_once dirname(__FILE__).'/../src/Morph/Object.php';
+require_once dirname(__FILE__).'/../src/Morph/PropertySet.php';
+require_once dirname(__FILE__).'/../src/Morph/Utils.php';
+require_once dirname(__FILE__).'/../src/Morph/Property/Generic.php';
+require_once dirname(__FILE__).'/ForTesting.php';
+
+/**
+ * @package Morph
+ */
+class TestObject extends PHPUnit_Framework_TestCase
+{
+
+
+ public function testGetCollection()
+ {
+ $obj = new Morph_ForTesting();
+ $this->assertEquals('Morph.ForTesting', $obj->collection());
+ }
+
+ public function testCollectionOverride()
+ {
+ $obj = new Morph_ForTesting();
+ $collectionName = "TestCollection";
+ $obj->collection($collectionName);
+ $this->assertEquals($collectionName, $obj->collection());
+ }
+
+ public function testMagicGettersSetter()
+ {
+ $testValue = 'TestValue';
+ $obj = new Morph_ForTesting();
+ $obj->TestField = $testValue;
+ $this->assertEquals($testValue, $obj->TestField);
+ }
+
+ public function testSetData()
+ {
+ $data = array('TestField' => 'value1');
+ $obj = new Morph_ForTesting();
+ $obj->__setData($data);
+ $this->assertEquals($data['TestField'], $obj->TestField);
+ $this->assertEquals(Morph_Object::STATE_DIRTY, $obj->state());
+
+ }
+
+ public function testGetData()
+ {
+ $data = array(
+ 'TestField' => 'value1',
+ 'instanceOf' => 'Morph_ForTesting'
+ );
+ $obj = new Morph_ForTesting();
+ $obj->__setData($data);
+ $this->assertEquals($data, $obj->__getData());
+ $this->assertEquals(Morph_Object::STATE_DIRTY, $obj->state());
+ }
+
+ public function test__toArray()
+ {
+ $testValue = 'TestValue';
+ $expectedArray = array(
+ 'TestField' => 'TestValue'
+ );
+ $obj = new Morph_ForTesting();
+ $obj->TestField = $testValue;
+ $this->assertEquals($expectedArray, $obj->__toArray());
+ }
+
+ public function test__toString()
+ {
+ $expected = "Id: \nState: New\nTestField: TEST\n";
+ $obj = new Morph_ForTesting();
+ $obj->TestField = 'TEST';
+ $this->assertEquals($expected, $obj->__toString());
+ }
+
+}
+?>
View
155 mongodb-morph/unit-tests/TestPropertySet.php
@@ -0,0 +1,155 @@
+<?php
+/**
+ * @package Morph
+ * @author Jonathan Moss <xirisr@gmail.com>
+ * @copyright 2009 Jonathan Moss
+ * @version SVN: $Id$
+ */
+require_once 'PHPUnit/Framework/TestCase.php';
+
+require_once dirname(__FILE__).'/../src/Morph/PropertySet.php';
+/**
+ *
+ * @package Morph
+ */
+class TestPropertySet extends PHPUnit_Framework_TestCase
+{
+
+ public function testAddingProperty()
+ {
+ $propertySet = new Morph_PropertySet();
+ $property = $this->getMockProperty();
+ $propertySet['AProperty'] = $property;
+ $this->assertContains($property, $propertySet->getArrayCopy());
+ }
+
+ public function testAddingInvalidValue()
+ {
+ $propertySet = new Morph_PropertySet();
+ $property = "I'm just wrong";
+ $this->setExpectedException('InvalidArgumentException');
+ $propertySet['AProperty'] = $property;
+ }
+
+ public function testAddingUnnamedProperty()
+ {
+ $propertySet = new Morph_PropertySet();
+ $property = $this->getMockProperty();
+ $this->setExpectedException('RuntimeException');
+ $propertySet[] = $property;
+ }
+
+ public function testAppendProperty()
+ {
+ $propertySet = new Morph_PropertySet();
+ $property = $this->getMockProperty();
+ $this->setExpectedException('RuntimeException');
+ $propertySet->append($property);
+ }
+
+ public function testAddingMultipleProperties()
+ {
+ $propertySet = new Morph_PropertySet();
+ $property1 = $this->getMockProperty();
+ $property2 = $this->getMockProperty();
+ $property = $this->getMockProperty();
+ $propertySet['AProperty'] = $property1;
+ $propertySet['BProperty'] = $property2;
+ $this->assertContains($property1, $propertySet->getArrayCopy());
+ $this->assertContains($property2, $propertySet->getArrayCopy());
+ }
+
+ public function testGetPropertyValue()
+ {
+ $expected = 'AValue';
+ $propertySet = new Morph_PropertySet();
+ $propertySet['P1'] = $this->getMockPropertyForGet($expected);
+ $this->assertEquals($expected, $propertySet->getPropertyValue('P1'));
+ }
+
+ public function testSetPropertyValue()
+ {
+ $expected = 'AValue';
+ $propertySet = new Morph_PropertySet();
+ $propertySet['P1'] = $this->getMockPropertyForSet($expected);
+ $propertySet->setPropertyValue('P1', $expected);
+ }
+
+ public function testSetStorage()
+ {
+ $storage = $this->getMock('Morph_Storage');
+ $propertySet = new Morph_PropertySet();
+ $propertySet['P1'] = $this->getMockPropertyForSetStorage();
+ $propertySet->setStorage($storage);
+ }
+
+ public function testGetRawPropertyValue()
+ {
+ $expected = 'AValue';
+ $propertySet = new Morph_PropertySet();
+ $propertySet['P1'] = $this->getMockPropertyFor__RawGet($expected);
+ $this->assertEquals($expected, $propertySet->__getRawPropertyValue('P1'));
+ }
+
+ public function testSetRawPropertyValue()
+ {
+ $expected = 'AValue';
+ $propertySet = new Morph_PropertySet();
+ $propertySet['P1'] = $this->getMockPropertyFor__RawSet($expected);
+ $propertySet->__setRawPropertyValue('P1', $expected);
+ }
+
+ // MOCK OBJECT FUNCTIONS
+
+ private function getMockProperty()
+ {
+ return $this->getMock('Morph_Property_Generic', array(), array('name'));
+ }
+
+ private function getMockPropertyForGet($value)
+ {
+ $property = $this->getMock('Morph_Property_Generic', array('getValue'), array('name'));
+ $property->expects($this->once())
+ ->method('getValue')
+ ->will($this->returnValue($value));
+ return $property;
+ }
+
+ private function getMockPropertyForSet($value)
+ {
+ $property = $this->getMock('Morph_Property_Generic', array('setValue'), array('name'));
+ $property->expects($this->once())
+ ->method('setValue')
+ ->with($this->equalTo($value));
+ return $property;
+ }
+
+ private function getMockPropertyForSetStorage()
+ {
+ $property = $this->getMock('Morph_Property_Generic', array('setStorage'), array('name'));
+ $property->expects($this->once())
+ ->method('setStorage')
+ ->with($this->isInstanceOf('Morph_Storage'));
+ return $property;
+ }
+
+ private function getMockPropertyFor__RawGet($value)
+ {
+ $property = $this->getMock('Morph_Property_Generic', array('__getRawValue'), array('name'));
+ $property->expects($this->once())
+ ->method('__getRawValue')
+ ->will($this->returnValue($value));
+ return $property;
+ }
+
+ private function getMockPropertyFor__RawSet($value)
+ {
+ $property = $this->getMock('Morph_Property_Generic', array('__setRawValue'), array('name'));
+ $property->expects($this->once())
+ ->method('__setRawValue')
+ ->with($this->equalTo($value));
+ return $property;
+ }
+
+}
+?>
View
77 mongodb-morph/unit-tests/TestQuery.php
@@ -0,0 +1,77 @@
+<?php
+/**
+ * @package Morph
+ * @author Jonathan Moss <xirisr@gmail.com>
+ * @copyright 2009 Jonathan Moss
+ * @version SVN: $Id$
+ */
+require_once 'PHPUnit/Framework/TestCase.php';
+
+require_once dirname(__FILE__).'/../src/Morph/Query/Property.php';
+require_once dirname(__FILE__).'/../src/Morph/Query.php';
+
+/**
+ * @package Morph
+ */
+class TestQuery extends PHPUnit_Framework_TestCase
+{
+
+ public function testLimit()
+ {
+ $query = new Morph_Query();
+ $query->limit(10);
+ $this->assertEquals(10, $query->getLimit());
+ }
+
+ public function testSkip()
+ {
+ $query = new Morph_Query();
+ $query->skip(10);
+ $this->assertEquals(10, $query->getSkip());
+ }
+
+ public function testProperty()
+ {
+ $expected = array('bob' => array());
+ $query = new Morph_Query();
+ $query->property('bob');
+ $this->assertEquals($expected, $query->getRawQuery());
+
+ }
+
+ public function testFluent()
+ {
+ $expected = array('bob' => 'hoskins');
+ $query = new Morph_Query();
+ $query->property('bob')
+ ->equals('hoskins');
+ $this->assertEquals($expected, $query->getRawQuery());
+ }
+
+ public function testMultipleProperties()
+ {
+ $expected = array('bob' => 'hoskins', 'abc' => 12);
+ $query = new Morph_Query();
+ $query->property('bob')
+ ->equals('hoskins')
+ ->property('abc')
+ ->equals(12);
+ $this->assertEquals($expected, $query->getRawQuery());
+ }
+
+ public function testFullFluency()
+ {
+ $expected = array('bob' => 'hoskins', 'abc' => 12);
+ $query = new Morph_Query();
+ $query->limit(10)->skip(12)
+ ->property('bob')
+ ->equals('hoskins')
+ ->property('abc')
+ ->equals(12);
+ $this->assertEquals($expected, $query->getRawQuery());
+ $this->assertEquals(10, $query->getLimit());
+ $this->assertEquals(12, $query->getSkip());
+ }
+
+}
+?>
View
49 mongodb-morph/unit-tests/TestUtils.php
@@ -0,0 +1,49 @@
+<?php
+require_once 'PHPUnit/Framework/TestCase.php';
+
+require_once dirname(__FILE__).'/../src/Morph/Utils.php';
+
+/**
+ * @package Morph
+ */
+class TestUtils extends PHPUnit_Framework_TestCase
+{
+
+ public function testCollectionName()
+ {
+ $object = $this->getMock('Morph_Object', array(), array(), 'user_Profile');
+ $collectionName = Morph_Utils::collectionName($object);
+ $this->assertEquals('user.Profile', $collectionName);
+ }
+
+ public function testNamespacedCollectionName()
+ {
+ if (version_compare(PHP_VERSION, '5.3.0') === -1) {
+ $this->markTestSkipped("Skipped for PHP Versions lower than 5.3.0");
+ return;
+ }
+ $object = $this->getMock('Morph_Object', array(), array(), 'user\\Profile');
+ $collectionName = Morph_Utils::collectionName($object);
+ $this->assertEquals('user.Profile', $collectionName);
+ }
+
+ public function testObjectReference()
+ {
+ $object = $this->getMock('Morph_Object', array('id', 'collection'));
+ $object->expects($this->once())
+ ->method('id')
+ ->will($this->returnValue('anId'));
+ $object->expects($this->once())
+ ->method('collection')
+ ->will($this->returnValue('aCollection'));
+ $ref = Morph_Utils::objectReference($object);
+ $expectedArray = array(
+ '$ref' => 'aCollection',
+ '$id' => 'anId'
+ );
+
+ $this->assertEquals($ref, $expectedArray);
+ }
+
+}
+?>
Please sign in to comment.
Something went wrong with that request. Please try again.