示例#1
0
 public function testStringMap()
 {
     $message = MapsMessage::create()->addToMap('String', 'test1', '123')->addToMap('String', 'test2', '456');
     $this->assertSame($message->get('String'), ['test1' => '123', 'test2' => '456']);
     $message->removeFromMap('String', 'test2');
     $this->assertSame($message->get('String'), ['test1' => '123']);
     $message->addToMap('String', 'test2', '456')->addToMap('String', 'test3', '789');
     $this->assertSame($message->get('String'), ['test1' => '123', 'test2' => '456', 'test3' => '789']);
     //echo json_encode($message->schema(), JSON_PRETTY_PRINT);
     //echo json_encode($message, JSON_PRETTY_PRINT);
 }
示例#2
0
 public function testAnyOfMessageInList()
 {
     $message = EmailMessage::create()->addToList('any_of_message', [MapsMessage::create()->addToMap('String', 'test:field:name', 'value1'), NestedMessage::create()->set('test1', 'value1')]);
     $this->assertCount(2, $message->get('any_of_message'));
 }
示例#3
0
 public function testAddInvalidTypeToMap()
 {
     // todo: refactor, this test is weird and confusing
     $shouldWork = MapsMessage::create();
     $shouldFail = clone $shouldWork;
     /*
      * some int types won't fail because they're all ints of course, just different ranges.
      * e.g. an Int is also all other unsigned ints (except BigInt but that's a class so we're fine)
      */
     $allInts = ['TinyInt', 'SmallInt', 'MediumInt', 'Int', 'SignedTinyInt', 'SignedSmallInt', 'SignedMediumInt', 'SignedInt', 'Timestamp'];
     $allStrings = ['Binary', 'Blob', 'MediumBlob', 'MediumText', 'String', 'Text'];
     foreach ($shouldWork::getAllTypes() as $type => $class) {
         foreach ($this->getTypeValues() as $k => $v) {
             $thrown = false;
             if ($type == $k) {
                 if (is_array($v)) {
                     $shouldWork->addToMap($type, 'test1', $v[0]);
                     $shouldWork->addToMap($type, 'test2', $v[1]);
                 } else {
                     $shouldWork->addToMap($type, 'test1', $v);
                 }
                 continue;
             }
             try {
                 if (is_array($v)) {
                     $shouldFail->addToMap($type, 'test1', $v[0]);
                     $shouldFail->addToMap($type, 'test2', $v[1]);
                 } else {
                     $shouldFail->addToMap($type, 'test1', $v);
                 }
                 switch ($type) {
                     case 'Binary':
                         if (in_array($k, $allStrings)) {
                             continue 2;
                         }
                         break;
                     case 'Blob':
                         if (in_array($k, $allStrings)) {
                             continue 2;
                         }
                         break;
                     case 'Decimal':
                         if (in_array($k, ['Float'])) {
                             continue 2;
                         }
                         break;
                     case 'Date':
                         if (in_array($k, ['DateTime'])) {
                             continue 2;
                         }
                         break;
                     case 'DateTime':
                         if (in_array($k, ['Date'])) {
                             continue 2;
                         }
                         break;
                     case 'Float':
                         if (in_array($k, ['Decimal'])) {
                             continue 2;
                         }
                         break;
                     case 'Identifier':
                         if (in_array($k, ['TimeUuid', 'Uuid'])) {
                             continue 2;
                         }
                         break;
                     case 'MediumBlob':
                         if (in_array($k, $allStrings)) {
                             continue 2;
                         }
                         break;
                     case 'MediumText':
                         if (in_array($k, $allStrings)) {
                             continue 2;
                         }
                         break;
                     case 'String':
                         if (in_array($k, $allStrings)) {
                             continue 2;
                         }
                         break;
                     case 'Text':
                         if (in_array($k, $allStrings)) {
                             continue 2;
                         }
                         break;
                     case 'Timestamp':
                         if (in_array($k, $allInts)) {
                             continue 2;
                         }
                         break;
                     case 'Uuid':
                         if (in_array($k, ['Identifier', 'TimeUuid'])) {
                             continue 2;
                         }
                         break;
                     default:
                 }
                 if (false !== strpos($type, 'Int') && in_array($k, $allInts)) {
                     continue;
                 }
             } catch (\Exception $e) {
                 $thrown = true;
             }
             if (!$thrown) {
                 if (is_array($v)) {
                     $this->fail(sprintf('[%s] accepted an invalid/mismatched [%s] value', $type, StringUtils::varToString($v[0])));
                     $this->fail(sprintf('[%s] accepted an invalid/mismatched [%s] value', $type, StringUtils::varToString($v[1])));
                 } else {
                     $this->fail(sprintf('[%s] accepted an invalid/mismatched [%s] value', $type, StringUtils::varToString($v)));
                 }
             }
         }
     }
     //echo json_encode($shouldWork, JSON_PRETTY_PRINT);
 }