/** * @covers ::loadRelFor */ public function testLoadRelFor() { $repo = new Repo(__NAMESPACE__ . '\\Model'); Model::$repo = $repo; $modelsSource = [new Model(), new Model()]; $foreignSource = [new Model(), new Model()]; $models = new Models($modelsSource); $foreign = new Models($foreignSource); $rel = $this->getMock(__NAMESPACE__ . '\\RelOne', ['loadForeignModels', 'areLinked'], ['test', $repo, $repo]); $repo->addRels([$rel]); $rel->expects($this->once())->method('loadForeignModels')->with($this->identicalTo($models), $this->equalTo(State::DELETED))->will($this->returnValue($foreign)); $map = [[$modelsSource[0], $foreignSource[0], true], [$modelsSource[1], $foreignSource[0], false], [$modelsSource[0], $foreignSource[1], false], [$modelsSource[1], $foreignSource[1], true]]; $rel->expects($this->exactly(4))->method('areLinked')->will($this->returnValueMap($map)); $result = $repo->loadRelFor($models, 'test', State::DELETED); $this->assertSame($foreign, $result); $link1 = $repo->loadLink($modelsSource[0], 'test'); $link2 = $repo->loadLink($modelsSource[1], 'test'); $this->assertSame($foreignSource[0], $link1->get()); $this->assertSame($foreignSource[1], $link2->get()); Model::$repo = null; }
public static function initialize(AbstractRepo $repo) { SoftDeleteTrait::initialize($repo); $repo->addRels([new RelOne('one', $repo, Model::getRepo()), new RelMany('many', $repo, Model::getRepo())]); }
/** * @covers ::loadCount */ public function testLoadCount() { $find = $this->getMock(__NAMESPACE__ . '\\Find', ['loadRaw'], [Model::getRepo()]); $models = [new Model(['id' => 4]), new Model(['id' => 98]), new Model(['id' => 100])]; $find->expects($this->once())->method('loadRaw')->will($this->returnValue($models)); $result = $find->loadCount(); $this->assertEquals(3, $result); }
/** * @covers ::execute */ public function testExecute() { $save = new Save(); $repo1 = $this->getMock(__NAMESPACE__ . '\\Repo', ['deleteModels', 'insertModels', 'updateModels', 'get'], [__NAMESPACE__ . '\\Model']); $repo1->expects($this->any())->method('get')->will($this->returnValue($repo1)); $repo2 = $this->getMock(__NAMESPACE__ . '\\Repo', ['deleteModels', 'insertModels', 'updateModels', 'get'], [__NAMESPACE__ . '\\SoftDeleteModel']); $repo2->expects($this->any())->method('get')->will($this->returnValue($repo2)); Model::$repo = $repo1; SoftDeleteModel::$repo = $repo2; $models = [1 => (new Model(['id' => 1], State::SAVED))->setProperties(['name' => 'changed']), 2 => new Model(['id' => 2], State::VOID), 3 => new Model(['id' => 3], State::PENDING), 4 => new Model(['id' => 4], State::DELETED), 5 => (new SoftDeleteModel(['id' => 5], State::DELETED))->setProperties(['deletedAt' => time()]), 6 => new SoftDeleteModel(['id' => 6], State::SAVED), 7 => new SoftDeleteModel(['id' => 7], State::PENDING), 8 => new SoftDeleteModel(['id' => 8], State::DELETED), 9 => (new SoftDeleteModel(['id' => 9], State::SAVED))->setProperties(['name' => 'changed'])]; $save = new Save(); $save->addArray($models); $expected = ['deleteModels' => [$models[4]], 'insertModels' => [$models[3]], 'updateModels' => [$models[1]]]; foreach ($expected as $method => $values) { $repo1->expects($this->once())->method($method)->with($this->callback(function (Models $models) use($values) { $this->assertSame($values, $models->toArray()); return true; })); } $expected = ['deleteModels' => [$models[8]], 'insertModels' => [$models[7]], 'updateModels' => [$models[5], $models[9]]]; foreach ($expected as $method => $values) { $repo2->expects($this->once())->method($method)->with($this->callback(function (Models $models) use($values) { $this->assertSame($values, $models->toArray()); return true; })); } $save->execute(); }