You are here

class MockSerializer in Config Entity Revisions 8

Same name and namespace in other branches
  1. 8.2 tests/src/Unit/ConfigEntityRevisionsRevertFormBaseTest.php \Drupal\Tests\config_entity_revisions\Unit\MockSerializer
  2. 1.x tests/src/Unit/ConfigEntityRevisionsRevertFormBaseTest.php \Drupal\Tests\config_entity_revisions\Unit\MockSerializer

The serializer (sic) we'd normally mock above has deserialize as a final method. Instead of using it, define our own class here.

Hierarchy

Expanded class hierarchy of MockSerializer

File

tests/src/Unit/ConfigEntityRevisionsRevertFormBaseTest.php, line 650

Namespace

Drupal\Tests\config_entity_revisions\Unit
View source
class MockSerializer {

  /**
   * @var ConfigEntityRevisionsRevertFormBaseTest
   */
  public $testClass = NULL;

  /**
   * @var bool
   */
  public $wasCalled = FALSE;

  /**
   * @var array
   */
  public $keyValuePairs = [];

  /**
   * Constructor. Store a copy of the test class so we can use its prophecy.
   *
   * @param ConfigEntityRevisionsRevertFormBaseTest $testClass
   *   The test class instance.
   */
  public function __construct(ConfigEntityRevisionsRevertFormBaseTest $testClass) {
    $this->testClass = $testClass;
  }

  /**
   * @param $data
   * @param $type
   * @param $format
   * @param array $context
   *
   * @return string
   * @throws \Exception
   */
  public function deserialize($data, $type, $format, array $context = []) {
    if ($data !== 'serialisedConfiguration') {
      throw new \Exception("Mock Serializer class's deserialize method should be called with data = 'serialisedConfiguration'");
    }
    $this->wasCalled = TRUE;
    $configEntity = $this->testClass
      ->prophesizeProxy(ConfigEntityRevisionsInterface::CLASS);
    $configEntity
      ->enforceIsNew(Argument::type('bool'))
      ->shouldBeCalled()
      ->will(function ($args) {
      if (!$args || $args[0]) {
        throw new \Exception("enforceIsNew wasn't set to FALSE.");
      }
    });
    $configEntity
      ->get(Argument::type('string'))
      ->shouldBeCalledTimes(1)
      ->willReturn('originalSettings');
    $keyValuePairs =& $this->keyValuePairs;
    $configEntity
      ->set(Argument::type('string'), Argument::any())
      ->will(function ($args) use (&$keyValuePairs) {
      $keyValuePairs[$args[0]] = $args[1];
    });
    $configEntity
      ->save()
      ->shouldBeCalled();
    return $configEntity
      ->reveal();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MockSerializer::$keyValuePairs public property
MockSerializer::$testClass public property
MockSerializer::$wasCalled public property
MockSerializer::deserialize public function
MockSerializer::__construct public function Constructor. Store a copy of the test class so we can use its prophecy.