コード例 #1
0
ファイル: ResultTest.php プロジェクト: reliv/rcm-user
 /**
  * testSetGet
  *
  * @covers \RcmUser\Result
  *
  * @return void
  */
 public function testSetGet()
 {
     $result = new Result(null, Result::CODE_SUCCESS, 'DEFAULT_MESSAGE');
     $data = 'SOMEDATA';
     $result->setCode(Result::CODE_SUCCESS);
     $result->setData($data);
     $messages = ['message 1'];
     $result->setMessages($messages);
     $result->setMessage('message 2');
     $this->assertTrue($result->getCode() === Result::CODE_SUCCESS, 'Data not returned.');
     $this->assertTrue($result->getData() === $data, 'Data not returned.');
     $returnedMessages = $result->getMessages();
     $this->assertTrue(is_array($returnedMessages), 'Messages should be array.');
     $this->assertTrue($returnedMessages[0] === $messages[0], 'Message 1 not returned.');
     $this->assertTrue($result->getMessage(1) === 'message 2', 'Message 2 not returned.');
     $this->assertTrue($result->getMessage('nope', 'not_found') === 'not_found', 'Message unset default not returned.');
     $this->assertTrue(is_string($result->getMessagesString()), 'Massages not returned as string');
     $this->assertJson(json_encode($result), 'Json not returned');
     $result->setCode(Result::CODE_SUCCESS);
     try {
         // this should NOT throw
         $result->throwFailure();
     } catch (RcmUserResultException $e) {
         $this->fail("Exception thrown incorrectly");
         return;
     }
     $result->setCode(Result::CODE_FAIL);
     try {
         $result->throwFailure();
     } catch (RcmUserResultException $e) {
         $this->assertInstanceOf('\\RcmUser\\Exception\\RcmUserResultException', $e);
         return;
     }
     $this->fail("Expected exception not thrown");
 }
コード例 #2
0
 /**
  * delete
  *
  * @param User  $user  user
  * @param array $roles roles
  *
  * @return Result
  */
 public function delete(User $user, $roles = [])
 {
     $failed = [];
     $result = new Result($failed, Result::CODE_SUCCESS);
     foreach ($roles as $key => $roleId) {
         $removeResult = $this->remove($user, $roleId);
         if (!$removeResult->isSuccess()) {
             $failed[] = $roleId;
             $result->setCode(Result::CODE_FAIL);
             $result->setMessage("Failed to remove role {$roleId} with error: " . $removeResult->getMessage());
             $result->setData($failed);
         } else {
             $result->setMessage("Removed role {$roleId}");
         }
     }
     return $result;
 }