public function testSaveWithException() { $exceptionMessage = 'Some Message'; $this->bookmarkResourceMock->expects($this->once())->method('save')->with($this->bookmarkMock)->willThrowException(new \Exception($exceptionMessage)); $this->setExpectedException('Magento\\Framework\\Exception\\CouldNotSaveException', __($exceptionMessage)); $this->bookmarkRepository->save($this->bookmarkMock); }
public function testSaveWithExtra() { $log = $this->subject->create(); $log->setExtra(['job_ticket']); $this->entityManager->expects($this->once())->method('persist')->with($this->callback(function (LogInterface $value) use($log) { $extra = $value->getExtra(); return $log === $value && !isset($extra['job_ticket']); })); $this->subject->save($log); }
/** * Tests required validation. * * @covers ::validate * @covers ::isValidationRequired * @covers ::setValidationRequired * @covers ::save * @covers ::preSave * * @expectedException \LogicException * @expectedExceptionMessage Entity validation was skipped. */ public function testRequiredValidation() { $validator = $this->getMock('\\Symfony\\Component\\Validator\\ValidatorInterface'); /** @var \Symfony\Component\Validator\ConstraintViolationList|\PHPUnit_Framework_MockObject_MockObject $empty_violation_list */ $empty_violation_list = $this->getMockBuilder('\\Symfony\\Component\\Validator\\ConstraintViolationList')->setMethods(NULL)->getMock(); $validator->expects($this->at(0))->method('validate')->with($this->entity->getTypedData())->will($this->returnValue($empty_violation_list)); $this->typedDataManager->expects($this->any())->method('getValidator')->will($this->returnValue($validator)); /** @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject $storage */ $storage = $this->getMock('\\Drupal\\Core\\Entity\\EntityStorageInterface'); $storage->expects($this->any())->method('save')->willReturnCallback(function (ContentEntityInterface $entity) use($storage) { $entity->preSave($storage); }); $this->entityManager->expects($this->any())->method('getStorage')->with($this->entityTypeId)->will($this->returnValue($storage)); // Check that entities can be saved normally when validation is not // required. $this->assertFalse($this->entity->isValidationRequired()); $this->entity->save(); // Make validation required and check that if the entity is validated, it // can be saved normally. $this->entity->setValidationRequired(TRUE); $this->assertTrue($this->entity->isValidationRequired()); $this->entity->validate(); $this->entity->save(); // Check that the "validated" status is reset after saving the entity and // that trying to save a non-validated entity when validation is required // results in an exception. $this->assertTrue($this->entity->isValidationRequired()); $this->entity->save(); }
/** * @param array $badStoreData * * @dataProvider saveValidationDataProvider * @magentoAppIsolation enabled * @magentoAppArea adminhtml * @magentoDbIsolation enabled * @expectedException \Magento\Framework\Exception\LocalizedException */ public function testSaveValidation($badStoreData) { $normalStoreData = ['code' => 'test', 'website_id' => 1, 'group_id' => 1, 'name' => 'test name', 'sort_order' => 0, 'is_active' => 1]; $data = array_merge($normalStoreData, $badStoreData); $this->model->setData($data); $this->model->save(); }
/** * @covers ::save */ public function testSave() { $storage = $this->getMock('\\Drupal\\Core\\Entity\\EntityStorageInterface'); $storage->expects($this->once())->method('save')->with($this->entity); $this->entityManager->expects($this->once())->method('getStorage')->with($this->entityTypeId)->will($this->returnValue($storage)); $this->entity->save(); }