You are here

protected function CivicrmEntityTestBase::mockCiviCrmApi in CiviCRM Entity 8.3

Mocks the CiviCRM API.

1 call to CivicrmEntityTestBase::mockCiviCrmApi()
CivicrmEntityTestBase::setUp in tests/src/Kernel/CivicrmEntityTestBase.php

File

tests/src/Kernel/CivicrmEntityTestBase.php, line 48

Class

CivicrmEntityTestBase
Test base to aid in mocking the CiviCRM API.

Namespace

Drupal\Tests\civicrm_entity\Kernel

Code

protected function mockCiviCrmApi() {
  $civicrm_api_mock = $this
    ->prophesize(CiviCrmApiInterface::class);
  $civicrm_api_mock
    ->get('event', [
    'id' => 1,
    'return' => array_keys($this
      ->sampleEventsGetFields()),
  ])
    ->willReturn($this
    ->sampleEventsData());
  $civicrm_api_mock
    ->get('contact', [
    'id' => 10,
    'return' => array_keys($this
      ->sampleContactGetFields()),
  ])
    ->willReturn($this
    ->sampleContactData());
  $civicrm_api_mock
    ->getFields('event')
    ->willReturn($this
    ->sampleEventsGetFields());
  $civicrm_api_mock
    ->getFields('event', 'create')
    ->willReturn($this
    ->sampleEventsGetFields());
  $civicrm_api_mock
    ->getFields('contact')
    ->willReturn($this
    ->sampleContactGetFields());
  $civicrm_api_mock
    ->getFields('contact', 'create')
    ->willReturn($this
    ->sampleContactGetFields());
  $civicrm_api_mock
    ->getFields('address')
    ->willReturn($this
    ->sampleAddressGetFields());
  $civicrm_api_mock
    ->getFields('address', 'create')
    ->willReturn($this
    ->sampleAddressGetFields());
  $civicrm_api_mock
    ->getFields(Argument::type('string'), 'create')
    ->willReturn([
    'id' => [
      'name' => 'id',
      'type' => 1,
      'title' => 'Fake ID',
      'description' => 'Unique Contact ID',
      'required' => TRUE,
    ],
  ]);
  $supported_entities = SupportedEntities::getInfo();
  foreach ($supported_entities as $civicrm_entity_info) {
    $civicrm_entity_name = $civicrm_entity_info['civicrm entity name'];
    if (in_array($civicrm_entity_name, [
      'event',
      'contact',
    ])) {
      continue;
    }
    $civicrm_api_mock
      ->getFields($civicrm_entity_name)
      ->willReturn($this
      ->minimalGenericGetFields());
  }
  $civicrm_api_mock
    ->save('event', Argument::type('array'))
    ->willReturn(TRUE);
  $civicrm_api_mock
    ->delete('event', Argument::type('array'))
    ->willReturn(TRUE);
  $this->container
    ->set('civicrm_entity.api', $civicrm_api_mock
    ->reveal());
}