コード例 #1
0
ファイル: DefinitionTest.php プロジェクト: NicolasSchmutz/cm
 public function testDecode()
 {
     CM_Config::get()->CM_Model_Abstract->types[CM_Model_Mock_Validation::getTypeStatic()] = 'CM_Model_Mock_Validation';
     CM_Config::get()->CM_Model_Abstract->types[CM_Model_Mock_Validation2::getTypeStatic()] = 'CM_Model_Mock_Validation2';
     $testDataList = array(array('value' => 12, 'schema' => array(), 'returnValue' => 12), array('value' => null, 'schema' => array('optional' => true), 'returnValue' => null), array('value' => -12, 'schema' => array('type' => 'integer'), 'returnValue' => -12), array('value' => '-12', 'schema' => array('type' => 'integer'), 'returnValue' => -12), array('value' => 14, 'schema' => array('type' => 'int'), 'returnValue' => 14), array('value' => 'foo bar', 'schema' => array('type' => 'string'), 'returnValue' => 'foo bar'), array('value' => 'foo 繁體字 bar', 'schema' => array('type' => 'string'), 'returnValue' => 'foo 繁體字 bar'), array('value' => '', 'schema' => array('type' => 'string'), 'returnValue' => ''), array('value' => -12, 'schema' => array('type' => 'float'), 'returnValue' => -12.0), array('value' => '-123', 'schema' => array('type' => 'float'), 'returnValue' => -123.0), array('value' => 12.01, 'schema' => array('type' => 'float'), 'returnValue' => 12.01), array('value' => '12.01', 'schema' => array('type' => 'float'), 'returnValue' => 12.01), array('value' => true, 'schema' => array('type' => 'boolean'), 'returnValue' => true), array('value' => false, 'schema' => array('type' => 'boolean'), 'returnValue' => false), array('value' => '1', 'schema' => array('type' => 'boolean'), 'returnValue' => true), array('value' => '0', 'schema' => array('type' => 'boolean'), 'returnValue' => false), array('value' => true, 'schema' => array('type' => 'bool'), 'returnValue' => true), array('value' => array('foo' => 'bar'), 'schema' => array('type' => 'array'), 'returnValue' => array('foo' => 'bar')), array('value' => 1378904141, 'schema' => array('type' => 'DateTime'), 'returnValue' => DateTime::createFromFormat('U', 1378904141)), array('value' => '{"id": 3}', 'schema' => array('type' => 'CM_Model_Mock_Validation'), 'returnValue' => new CM_Model_Mock_Validation(3)), array('value' => '2', 'schema' => array('type' => 'CM_Model_Mock_Validation'), 'returnValue' => new CM_Model_Mock_Validation(2)), array('value' => '{"id": 4, "foo": "bar"}', 'schema' => array('type' => 'CM_Model_Mock_Validation'), 'returnValue' => new CM_Model_Mock_Validation(4, 'bar')), array('value' => '{"id": "4", "foo": "bar"}', 'schema' => array('type' => 'CM_Model_Mock_Validation2'), 'returnValue' => new CM_Model_Mock_Validation2('4', 'bar')), array('value' => 'mongo123mixed321id', 'schema' => array('type' => 'CM_Model_Mock_Validation'), 'returnValue' => new CM_Model_Mock_Validation('mongo123mixed321id')));
     foreach ($testDataList as $testData) {
         $schema = new CM_Model_Schema_Definition(array('foo' => $testData['schema']));
         $value = $schema->decodeField('foo', $testData['value']);
         if (is_object($testData['returnValue'])) {
             $this->assertEquals($testData['returnValue'], $value, 'Unexpected return value (' . CM_Util::var_line($testData) . ')');
         } else {
             $this->assertSame($testData['returnValue'], $value, 'Unexpected return value (' . CM_Util::var_line($testData) . ')');
         }
     }
 }
コード例 #2
0
ファイル: DefinitionTest.php プロジェクト: cargomedia/cm
 public function testDecodeArrayConvertible()
 {
     $class = $this->mockInterface('CM_ArrayConvertible');
     $className = $class->getClassName();
     $arrayConvertible = $class->newInstanceWithoutConstructor();
     $fromArray = $class->mockStaticMethod('fromArray')->set($arrayConvertible);
     $schema = new CM_Model_Schema_Definition(['arrayConvertible' => ['type' => $className]]);
     $jsonData = '{"key":"value"}';
     $value = $schema->decodeField('arrayConvertible', $jsonData);
     $this->assertSame(['key' => 'value'], $fromArray->getLastCall()->getArgument(0));
     $this->assertSame($arrayConvertible, $value);
 }