public function setUp() { $t = new Seriplater(new Rule()); $this->idResolver = new IdResolver(); $this->idFactory = new IdFactory(); $this->hier = new HierarchicalTemplate($this->idResolver); $genSerializer = new GenericSerializer($this->idFactory); $genDeserializer = new GenericDeserializer($this->idResolver); $preExDeserializer = new PreExistingEntityDeserializer($this->idResolver); $preservingSerializer = new PreservingEntityIdSerializer($this->idFactory); // Site $this->siteRepo = Mockery::mock("Prewk\\Seriplating\\Contracts\\RepositoryInterface"); $this->site = new SeriplatingTemplate($genSerializer, $genDeserializer, $this->siteRepo, ["id" => $t->id("sites"), "name" => $t->value(), "primary_menu_id" => $t->references("menus"), "landing_page_id" => $t->references("pages"), "font_preset" => $t->value(), "font_variables" => $t->value(), "color_swatches" => $t->hasMany("color_swatches"), "sections" => $t->hasMany("sections"), "tweaks" => $t->hasMany("tweaks"), "pages" => $t->hasMany("pages"), "menus" => $t->hasMany("menus"), "aliases" => $t->hasMany("aliases")]); // Color swatch $this->colorSwatchRepo = Mockery::mock("Prewk\\Seriplating\\Contracts\\RepositoryInterface"); $this->colorSwatch = new SeriplatingTemplate($genSerializer, $genDeserializer, $this->colorSwatchRepo, ["id" => $t->id("color_swatches"), "site_id" => $t->inherits("id"), "value" => $t->value()]); // Tweak $this->tweakRepo = Mockery::mock("Prewk\\Seriplating\\Contracts\\RepositoryInterface"); $this->tweak = new SeriplatingTemplate($genSerializer, $genDeserializer, $this->tweakRepo, ["id" => $t->id("tweaks"), "site_id" => $t->inherits("site_id", "id"), "tweakable_type" => $t->value(), "tweakable_id" => $t->inherits("id"), "definition" => $t->value(), "data" => $t->deep(["/^color_swatch_id\$/" => $t->references("color_swatches")])]); // Menu $this->menuRepo = Mockery::mock("Prewk\\Seriplating\\Contracts\\RepositoryInterface"); $this->menu = new SeriplatingTemplate($genSerializer, $genDeserializer, $this->menuRepo, ["id" => $t->id("menus"), "site_id" => $t->inherits("id"), "locale" => $t->value(), "menu_items" => $t->hasMany("menu_items")]); // Menu item $this->menuItemRepo = Mockery::mock("Prewk\\Seriplating\\Contracts\\RepositoryInterface"); $this->menuItem = new SeriplatingTemplate($genSerializer, $genDeserializer, $this->menuItemRepo, ["id" => $t->id("menu_items"), "site_id" => $t->inherits("site_id"), "alias_id" => $t->references("aliases"), "menu_id" => $t->inherits("menu_id", "id"), "parent_id" => $t->references("menu_items", 0), "parent_id" => 0, "sort_order" => $t->value()]); // Alias $this->aliasRepo = Mockery::mock("Prewk\\Seriplating\\Contracts\\RepositoryInterface"); $this->alias = new SeriplatingTemplate($genSerializer, $genDeserializer, $this->aliasRepo, ["id" => $t->id("aliases"), "site_id" => $t->inherits("id"), "aliasable_type" => $t->value(), "aliasable_id" => $t->conditions("aliasable_type", ["Page" => $t->references("pages"), "Section" => $t->references("sections"), "Resource" => $t->references("resources")]), "alias" => $t->value()]); // Page $this->pageRepo = Mockery::mock("Prewk\\Seriplating\\Contracts\\RepositoryInterface"); $this->page = new SeriplatingTemplate($genSerializer, $genDeserializer, $this->pageRepo, ["id" => $t->id("pages"), "site_id" => $t->inherits("id"), "name" => $t->value(), "sections" => $t->hasMany("sections")]); // Section $this->sectionRepo = Mockery::mock("Prewk\\Seriplating\\Contracts\\RepositoryInterface"); $this->section = new SeriplatingTemplate($genSerializer, $genDeserializer, $this->sectionRepo, ["id" => $t->id("sections"), "site_id" => $t->inherits("site_id", "id"), "sectionable_type" => $t->value(), "sectionable_id" => $t->inherits("id"), "type" => $t->value(), "name" => $t->value(), "position" => $t->value(), "sort_order" => $t->increments(), "data" => $t->conditions("type", ["block" => $t->deep(["/\\.blocks\\.\\d+.id\$/" => $t->references("blocks"), "/^resources\\.[\\d]+\\.id\$/" => $t->references("resources")]), "menu" => ["menu_id" => $t->references("menus")]], $t->value()), "resources" => $t->hasMany("resources"), "blocks" => $t->hasMany("blocks")]); // Block $this->blockRepo = Mockery::mock("Prewk\\Seriplating\\Contracts\\RepositoryInterface"); $this->block = new SeriplatingTemplate($genSerializer, $genDeserializer, $this->blockRepo, ["id" => $t->id("blocks"), "site_id" => $t->inherits("site_id"), "type" => $t->value(), "resources" => $t->hasMany("resources"), "data" => $t->conditions("type", ["image" => $t->deep(["/^resources\\.[\\d]+\\.id\$/" => $t->references("resources"), "/^current_resource_id\$/" => $t->references("resources")])], $t->value())]); // Resource $this->resourceRepo = Mockery::mock("Prewk\\Seriplating\\Contracts\\RepositoryInterface"); $this->resource = new SeriplatingTemplate($preservingSerializer, $preExDeserializer, $this->resourceRepo, ["id" => $t->id("resources")]); $this->hier->register($this->site)->register($this->colorSwatch)->register($this->tweak)->register($this->section)->register($this->page)->register($this->block)->register($this->resource)->register($this->menu)->register($this->menuItem)->register($this->alias); }
public function test_hierarchical_deserialization_with_container_template() { $t = new Seriplater(new Rule()); $idResolver = new IdResolver(); $idFactory = new IdFactory(); $hier = new HierarchicalTemplate($idResolver); $serializer = new GenericSerializer($idFactory); $deserializer = new GenericDeserializer($idResolver); $topTemplate = new ContainerTemplate($idFactory, $idResolver, ["id" => $t->id("tops"), "foos" => $t->hasMany("foos"), "bars" => $t->hasMany("bars")], 1); $fooRepository = Mockery::mock("Prewk\\Seriplating\\Contracts\\RepositoryInterface"); $fooTemplate = new SeriplatingTemplate($serializer, $deserializer, $fooRepository, ["id" => $t->id("foos"), "top_id" => $t->inherits("id"), "val" => $t->value(), "sort_order" => $t->increments()]); $barRepository = Mockery::mock("Prewk\\Seriplating\\Contracts\\RepositoryInterface"); $barTemplate = new SeriplatingTemplate($serializer, $deserializer, $barRepository, ["id" => $t->id("bars"), "val" => $t->value(), "bazes" => $t->hasMany("bazes"), "top_id" => $t->conditions("val", ["bar" => $t->inherits("id")])]); $bazRepository = Mockery::mock("Prewk\\Seriplating\\Contracts\\RepositoryInterface"); $bazTemplate = new SeriplatingTemplate($serializer, $deserializer, $bazRepository, ["id" => $t->id("bazes"), "val" => $t->value(), "bar_id" => $t->inherits("id"), "top_id" => $t->inherits("top_id"), "foo_id" => $t->references("foos")]); $serialization = ["_id" => $idFactory->get("tops", 1), "foos" => [["_id" => $idFactory->get("foos", 2), "val" => "ipsum"], ["_id" => $idFactory->get("foos", 3), "val" => "foo"]], "bars" => [["_id" => $idFactory->get("bars", 4), "val" => "bar", "bazes" => [["_id" => $idFactory->get("bazes", 5), "val" => "baz", "foo_id" => ["_ref" => $idFactory->get("foos", 2)]]]]]]; $expectedCreatedEntityData = ["id" => 1, "foos" => [["id" => 2, "val" => "ipsum", "top_id" => 1, "sort_order" => 0], ["id" => 3, "val" => "foo", "top_id" => 1, "sort_order" => 1]], "bars" => [["id" => 4, "val" => "bar", "bazes" => [["id" => 5, "val" => "baz", "top_id" => 1, "bar_id" => 4, "foo_id" => 0]], "top_id" => 1]]]; $fooRepository->shouldReceive("create")->once()->with(["val" => "ipsum", "top_id" => 1, "sort_order" => 0])->andReturn(["id" => 2, "val" => "ipsum", "top_id" => 1, "sort_order" => 0])->shouldReceive("create")->once()->with(["val" => "foo", "top_id" => 1, "sort_order" => 1])->andReturn(["id" => 3, "val" => "foo", "top_id" => 1, "sort_order" => 1]); $barRepository->shouldReceive("create")->once()->with(["val" => "bar", "top_id" => 1])->andReturn(["id" => 4, "val" => "bar", "top_id" => 1]); $bazRepository->shouldReceive("create")->once()->with(["val" => "baz", "top_id" => 1, "bar_id" => 4, "foo_id" => 0])->andReturn(["id" => 5, "val" => "baz", "top_id" => 1, "bar_id" => 4, "foo_id" => 0])->shouldReceive("update")->once()->with(5, ["foo_id" => 2]); $hier->register($topTemplate)->register($fooTemplate)->register($barTemplate)->register($bazTemplate); $entityData = $hier->deserialize("tops", $serialization); $this->assertEquals($expectedCreatedEntityData, $entityData); }