DataParserPluginBaseTest.php 2.36 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php

namespace Drupal\Tests\migrate_plus\Unit;

use Drupal\migrate_plus\DataParserPluginBase;
use Drupal\Tests\migrate\Unit\MigrateTestCase;

/**
 * @coversDefaultClass \Drupal\migrate_plus\DataParserPluginBase
 *
 * @group migrate_plus
 */
class DataParserPluginBaseTest extends MigrateTestCase {

  /**
   * @covers ::nextSource
   */
18
  public function testNextSourceWithOneUrl(): void {
19
20
21
22
23
24
25
26
27
28
    $parser = $this->getMockedDataParser();
    $parser->expects($this->once())
      ->method('openSourceUrl')
      ->willReturn(TRUE);
    $this->assertTrue($parser->nextSource());
  }

  /**
   * @covers ::nextSource
   */
29
  public function testNextSourceWithoutUrls(): void {
30
31
32
33
34
35
36
37
38
39
40
41
42
    $config = [
      'urls' => [],
    ];

    $parser = $this->getMockedDataParser($config);
    $parser->expects($this->never())
      ->method('openSourceUrl');
    $this->assertFalse($parser->nextSource());
  }

  /**
   * @covers ::count
   */
43
  public function testCountWithoutUrls(): void {
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
    $config = [
      'urls' => [],
    ];

    $parser = $this->getMockedDataParser($config);
    $parser->expects($this->never())
      ->method('openSourceUrl');
    $this->assertEquals(0, $parser->count());
  }

  /**
   * Returns a mocked data parser.
   *
   * @param array $configuration
   *   The configuration to pass to the data parser.
   *
   * @return \PHPUnit\Framework\MockObject\MockObject|\Drupal\Tests\migrate_plus\Unit\DataParserPluginBaseMock
   *   An mock instance of DataParserPluginBase.
   */
  protected function getMockedDataParser(array $configuration = []) {
    // Set constructor arguments.
    $configuration += [
      'urls' => ['http://example.org/data_parser_test'],
      'item_selector' => 0,
    ];
    $plugin_id = 'foo';
    $plugin_definition = [
      'id' => 'foo',
      'title' => 'Foo',
    ];

    return $this->getMockBuilder(DataParserPluginBaseMock::class)
      ->setConstructorArgs([$configuration, $plugin_id, $plugin_definition])
      ->setMethods(['openSourceUrl'])
      ->getMockForAbstractClass();
  }

}

/**
 * Mock for abstract class DataParserPluginBase.
 *
 * This mock is used to make certain methods publicly accessible.
 */
abstract class DataParserPluginBaseMock extends DataParserPluginBase {

  /**
   * {@inheritdoc}
92
93
   *
   * phpcs:disable Generic.CodeAnalysis.UselessOverridingMethod.Found
94
95
96
97
98
99
   */
  public function nextSource() {
    return parent::nextSource();
  }

}