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); }
<?php error_reporting(-1); date_default_timezone_set('UTC'); // Ensure that composer has installed all dependencies if (!file_exists(dirname(__DIR__) . '/composer.lock')) { die("Dependencies must be installed using composer:\n\nphp composer.phar install\n\n" . "See http://getcomposer.org for help with installing composer\n"); } // Include the composer autoloader $loader = (require dirname(__DIR__) . '/vendor/autoload.php'); // auto registers the schema with the MessageResolver // only done for tests or dynamic messages. \Gdbots\Tests\Pbj\Fixtures\EmailMessage::schema(); \Gdbots\Tests\Pbj\Fixtures\NestedMessage::schema(); \Gdbots\Tests\Pbj\Fixtures\MapsMessage::schema();
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')); }
public function testCreate() { $type = new Type(new Index(new Client(), $this->indexName), 'pbj_test_type'); $schema = EmailMessage::schema(); $mapping = $this->factory->create($schema, 'english'); $mapping->setType($type); $expected = <<<JSON { "pbj_test_type": { "properties": { "_schema": { "type": "string", "index": "not_analyzed", "include_in_all": false }, "id": { "type": "string", "index": "not_analyzed", "include_in_all": false }, "from_name": { "type": "string", "analyzer": "english" }, "from_email": { "type": "string", "analyzer": "pbj_keyword_analyzer", "include_in_all": false }, "subject": { "type": "string", "analyzer": "english" }, "body": { "type": "string", "analyzer": "english" }, "priority": { "type": "integer", "include_in_all": false }, "sent": { "type": "boolean", "include_in_all": false }, "date_sent": { "type": "date", "include_in_all": false }, "microtime_sent": { "type": "long", "include_in_all": false }, "provider": { "type": "string", "index": "not_analyzed", "include_in_all": false }, "labels": { "type": "string", "analyzer": "pbj_keyword_analyzer", "include_in_all": false }, "nested": { "type": "object", "properties": { "_schema": { "type": "string", "index": "not_analyzed", "include_in_all": false }, "test1": { "type": "string", "analyzer": "english" }, "test2": { "type": "long", "include_in_all": false }, "location": { "type": "geo_point", "include_in_all": false }, "refs": { "type": "object", "properties": { "curie": { "type": "string", "index": "not_analyzed", "include_in_all": false }, "id": { "type": "string", "index": "not_analyzed", "include_in_all": false }, "tag": { "type": "string", "index": "not_analyzed", "include_in_all": false } } } } }, "enum_in_set": { "type": "string", "index": "not_analyzed", "include_in_all": false }, "enum_in_list": { "type": "string", "index": "not_analyzed", "include_in_all": false }, "any_of_message": { "type": "nested", "properties": { "_schema": { "type": "string", "index": "not_analyzed", "include_in_all": false } } }, "dynamic_fields": { "type": "nested", "properties": { "name": { "type": "string", "index": "not_analyzed", "include_in_all": false }, "bool_val": { "type": "boolean", "include_in_all": false }, "date_val": { "type": "date", "include_in_all": false }, "float_val": { "type": "float", "include_in_all": false }, "int_val": { "type": "long", "include_in_all": false }, "string_val": { "type": "string", "analyzer": "english" }, "text_val": { "type": "string", "analyzer": "english" } } } } } } JSON; $this->assertSame($expected, json_encode($mapping->toArray(), JSON_PRETTY_PRINT)); $schema = MapsMessage::schema(); $mapping = $this->factory->create($schema, 'english'); $mapping->setType($type); $expected = <<<JSON { "pbj_test_type": { "properties": { "_schema": { "type": "string", "index": "not_analyzed", "include_in_all": false } }, "dynamic_templates": [ { "bigint_template": { "path_match": "BigInt.*", "mapping": { "type": "long", "include_in_all": false } } }, { "binary_template": { "path_match": "Binary.*", "mapping": { "type": "binary" } } }, { "blob_template": { "path_match": "Blob.*", "mapping": { "type": "binary" } } }, { "boolean_template": { "path_match": "Boolean.*", "mapping": { "type": "boolean", "include_in_all": false } } }, { "datetime_template": { "path_match": "DateTime.*", "mapping": { "type": "date", "include_in_all": false } } }, { "date_template": { "path_match": "Date.*", "mapping": { "type": "date", "include_in_all": false } } }, { "decimal_template": { "path_match": "Decimal.*", "mapping": { "type": "double", "include_in_all": false } } }, { "dynamicfield_template": { "path_match": "DynamicField.*", "mapping": { "type": "object", "properties": { "name": { "type": "string", "index": "not_analyzed", "include_in_all": false }, "bool_val": { "type": "boolean", "include_in_all": false }, "date_val": { "type": "date", "include_in_all": false }, "float_val": { "type": "float", "include_in_all": false }, "int_val": { "type": "long", "include_in_all": false }, "string_val": { "type": "string", "analyzer": "english" }, "text_val": { "type": "string", "analyzer": "english" } } } } }, { "float_template": { "path_match": "Float.*", "mapping": { "type": "float", "include_in_all": false } } }, { "geopoint_template": { "path_match": "GeoPoint.*", "mapping": { "type": "geo_point", "include_in_all": false } } }, { "identifier_template": { "path_match": "Identifier.*", "mapping": { "type": "string", "index": "not_analyzed", "include_in_all": false } } }, { "intenum_template": { "path_match": "IntEnum.*", "mapping": { "type": "integer", "include_in_all": false } } }, { "int_template": { "path_match": "Int.*", "mapping": { "type": "long", "include_in_all": false } } }, { "mediumblob_template": { "path_match": "MediumBlob.*", "mapping": { "type": "binary" } } }, { "mediumint_template": { "path_match": "MediumInt.*", "mapping": { "type": "integer", "include_in_all": false } } }, { "mediumtext_template": { "path_match": "MediumText.*", "mapping": { "type": "string", "analyzer": "english" } } }, { "messageref_template": { "path_match": "MessageRef.*", "mapping": { "type": "object", "properties": { "curie": { "type": "string", "index": "not_analyzed", "include_in_all": false }, "id": { "type": "string", "index": "not_analyzed", "include_in_all": false }, "tag": { "type": "string", "index": "not_analyzed", "include_in_all": false } } } } }, { "message_template": { "path_match": "Message.*", "mapping": { "type": "object", "properties": { "_schema": { "type": "string", "index": "not_analyzed", "include_in_all": false }, "test1": { "type": "string", "analyzer": "english" }, "test2": { "type": "long", "include_in_all": false }, "location": { "type": "geo_point", "include_in_all": false }, "refs": { "type": "object", "properties": { "curie": { "type": "string", "index": "not_analyzed", "include_in_all": false }, "id": { "type": "string", "index": "not_analyzed", "include_in_all": false }, "tag": { "type": "string", "index": "not_analyzed", "include_in_all": false } } } } } } }, { "microtime_template": { "path_match": "Microtime.*", "mapping": { "type": "long", "include_in_all": false } } }, { "signedbigint_template": { "path_match": "SignedBigInt.*", "mapping": { "type": "long", "include_in_all": false } } }, { "signedint_template": { "path_match": "SignedInt.*", "mapping": { "type": "integer", "include_in_all": false } } }, { "signedmediumint_template": { "path_match": "SignedMediumInt.*", "mapping": { "type": "long", "include_in_all": false } } }, { "signedsmallint_template": { "path_match": "SignedSmallInt.*", "mapping": { "type": "short", "include_in_all": false } } }, { "signedtinyint_template": { "path_match": "SignedTinyInt.*", "mapping": { "type": "byte", "include_in_all": false } } }, { "smallint_template": { "path_match": "SmallInt.*", "mapping": { "type": "integer", "include_in_all": false } } }, { "stringenum_template": { "path_match": "StringEnum.*", "mapping": { "type": "string", "index": "not_analyzed", "include_in_all": false } } }, { "string_template": { "path_match": "String.*", "mapping": { "type": "string", "analyzer": "english" } } }, { "text_template": { "path_match": "Text.*", "mapping": { "type": "string", "analyzer": "english" } } }, { "timestamp_template": { "path_match": "Timestamp.*", "mapping": { "type": "date", "include_in_all": false } } }, { "timeuuid_template": { "path_match": "TimeUuid.*", "mapping": { "type": "string", "index": "not_analyzed", "include_in_all": false } } }, { "tinyint_template": { "path_match": "TinyInt.*", "mapping": { "type": "short", "include_in_all": false } } }, { "trinary_template": { "path_match": "Trinary.*", "mapping": { "type": "short", "include_in_all": false } } }, { "uuid_template": { "path_match": "Uuid.*", "mapping": { "type": "string", "index": "not_analyzed", "include_in_all": false } } } ] } } JSON; $expected = json_decode($expected, true); $actual = $mapping->toArray(); $expected['pbj_test_type']['dynamic_templates'] = $this->sortDynamicTemplates($expected['pbj_test_type']['dynamic_templates']); $actual['pbj_test_type']['dynamic_templates'] = $this->sortDynamicTemplates($actual['pbj_test_type']['dynamic_templates']); $this->assertSame(json_encode($expected['pbj_test_type']['properties']), json_encode($actual['pbj_test_type']['properties'])); $this->assertSame(json_encode($expected['pbj_test_type']['dynamic_templates']), json_encode($actual['pbj_test_type']['dynamic_templates'])); //echo json_encode($mapping->toArray(), JSON_PRETTY_PRINT); }
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); }