<?php

/*
 +--------------------------------------------------------------------+
 | Copyright CiviCRM LLC. All rights reserved.                        |
 |                                                                    |
 | This work is published under the GNU AGPLv3 license with some      |
 | permitted exceptions and without any warranty. For full license    |
 | and copyright information, see https://civicrm.org/licensing       |
 +--------------------------------------------------------------------+
 */

/**
 *
 * @package CRM
 * @copyright CiviCRM LLC https://civicrm.org/licensing
 */


namespace api\v4\Entity;

use api\v4\Api4TestBase;
use Civi\Api4\ExampleData;

/**
 * @group headless
 */
class ExampleDataTest extends Api4TestBase {

  /**
   * Basic canary test fetching a specific example.
   *
   * @throws \CRM_Core_Exception
   */
  public function testGet(): void {
    $file = \Civi::paths()->getPath('[civicrm.root]/Civi/WorkflowMessage/GenericWorkflowMessage/Alex.php');
    $name = 'workflow/generic/Alex';

    $this->assertFileExists($file, "Expect find canary file ($file)");

    $get = ExampleData::get()
      ->addWhere('name', '=', $name)
      ->execute()
      ->single();
    $this->assertEquals($name, $get['name']);
    $this->assertTrue(!isset($get['data']), 'Default "get" should not return "data"');
    $this->assertTrue(!isset($get['asserts']), 'Default "get" should not return "asserts"');

    $get = ExampleData::get()
      ->addWhere('name', 'LIKE', 'workflow/generic/%')
      ->execute();
    $this->assertTrue($get->count() > 0);
    foreach ($get as $gotten) {
      $this->assertStringStartsWith('workflow/generic/', $gotten['name']);
    }

    $get = ExampleData::get()
      ->addWhere('name', '=', $name)
      ->addSelect('workflow', 'data')
      ->execute()
      ->single();
    $this->assertEquals($name, $get['name']);
    $this->assertEquals(0, $get['data']['modelProps']['contact']['id']);
  }

}
