/** * @covers ::get * @covers ::has * @covers ::set * @covers ::clear */ public function testGetterSetter() { $class = 'Harp\\Harp\\Test\\TestModel\\City'; $this->assertFalse(Container::has($class)); $repo = Container::get($class); $this->assertTrue(Container::has($class)); $this->assertInstanceOf('Harp\\Harp\\Repo', $repo); $this->assertEquals($class, $repo->getModelClass()); $this->assertSame($repo, Container::get($class)); $repo2 = new Repo(new Config($class)); Container::set($class, $repo2); $this->assertSame($repo2, Container::get($class)); Container::clear(); $this->assertFalse(Container::has($class)); }
/** * @covers ::loadAllRelsFor */ public function testLoadAllRelsFor() { $repo1 = $this->getMock('Harp\\Harp\\Repo', ['loadRelFor'], [new Config(__NAMESPACE__ . '\\TestModel\\City')]); $repo2 = $this->getMock('Harp\\Harp\\Repo', ['loadRelFor'], [new Config(__NAMESPACE__ . '\\TestModel\\Country')]); $repo3 = $this->getMock('Harp\\Harp\\Repo', ['loadRelFor'], [new Config(__NAMESPACE__ . '\\TestModel\\User')]); Container::set(__NAMESPACE__ . '\\TestModel\\City', $repo1); Container::set(__NAMESPACE__ . '\\TestModel\\Country', $repo2); Container::set(__NAMESPACE__ . '\\TestModel\\User', $repo3); $repo1->getConfig()->addRel(new Rel\BelongsTo('one', $repo1->getConfig(), __NAMESPACE__ . '\\TestModel\\Country')); $repo2->getConfig()->addRel(new Rel\HasMany('many', $repo2->getConfig(), __NAMESPACE__ . '\\TestModel\\User')); $models1 = new Models([new City()]); $models2 = new Models([new Country()]); $models3 = new Models([new User()]); $repo1->expects($this->once())->method('loadRelFor')->with($this->equalTo($models1), $this->equalTo('one'), $this->equalTo(State::DELETED))->will($this->returnValue($models2)); $repo2->expects($this->once())->method('loadRelFor')->with($this->equalTo($models2), $this->equalTo('many'), $this->equalTo(State::DELETED))->will($this->returnValue($models3)); $repo1->loadAllRelsFor($models1, ['one' => 'many'], State::DELETED); }
/** * @covers ::execute */ public function testExecute() { $save = new Save(); $repo1 = $this->getMock('Harp\\Harp\\Repo', ['deleteModels', 'insertModels', 'updateModels', 'get'], [new Config(__NAMESPACE__ . '\\TestModel\\Country')]); $repo1->expects($this->any())->method('get')->will($this->returnValue($repo1)); $repo2 = $this->getMock('Harp\\Harp\\Repo', ['deleteModels', 'insertModels', 'updateModels', 'get'], [new Config(__NAMESPACE__ . '\\TestModel\\User')]); $repo2->expects($this->any())->method('get')->will($this->returnValue($repo2)); Container::set(__NAMESPACE__ . '\\TestModel\\Country', $repo1); Container::set(__NAMESPACE__ . '\\TestModel\\User', $repo2); $models = [1 => (new Country(['id' => 1], State::SAVED))->setProperties(['name' => 'changed']), 2 => new Country(['id' => 2], State::VOID), 3 => new Country(['id' => 3], State::PENDING), 4 => new Country(['id' => 4], State::DELETED), 5 => (new User(['id' => 5, 'name' => 'test'], State::DELETED))->setProperties(['deletedAt' => time()]), 6 => new User(['id' => 6, 'name' => 'test'], State::SAVED), 7 => new User(['id' => 7, 'name' => 'test'], State::PENDING), 8 => new User(['id' => 8, 'name' => 'test'], State::DELETED), 9 => (new User(['id' => 9, 'name' => 'test'], 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(); }