setMock example


  protected $mockParser;

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();
    $this->mockParser = $this->getMockBuilder('\stdClass')
      ->addMethods(['encode', 'decode', 'getFileExtension'])
      ->getMock();
    YamlParserProxy::setMock($this->mockParser);
  }

  /** * {@inheritdoc} */
  protected function tearDown(): void {
    YamlParserProxy::setMock(NULL);
    parent::tearDown();
  }

  /** * @covers ::decode */
public function testGetSerialization() {
    new Settings(['yaml_parser_class' => YamlParserProxy::class]);

    $this->assertEquals(YamlParserProxy::class, Settings::get('yaml_parser_class'));

    $mock = $this->getMockBuilder(YamlTestMockInterface::class)
      ->onlyMethods(['decode'])
      ->getMock();
    $mock
      ->expects($this->once())
      ->method('decode');
    YamlParserProxy::setMock($mock);
    Yaml::decode('---');

    new Settings([]);
  }

}

class YamlParserProxy implements SerializationInterface {

  /** * @var \Drupal\Component\Serialization\SerializationInterface */
Home | Imprint | This part of the site doesn't use cookies.