Skip to content

Commit

Permalink
APIv4 - Simplify entity creation in test suite
Browse files Browse the repository at this point in the history
Removes the old v3 creation and hardcoded sample data in favor of
new `createTestRecord` and `saveTestRecords` methods which automatically
handle cleanup during tearDown.
  • Loading branch information
colemanw committed May 11, 2022
1 parent adc1253 commit d3095ee
Show file tree
Hide file tree
Showing 28 changed files with 506 additions and 1,155 deletions.
2 changes: 2 additions & 0 deletions CRM/Core/BAO/CustomGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public static function self_hook_civicrm_post(\Civi\Core\Event\PostEvent $e): vo
* @throws \Exception
*/
public static function create(&$params) {
// This is the database default
$params += ['extends' => 'Contact'];
// create custom group dao, populate fields and then save.
$group = new CRM_Core_DAO_CustomGroup();
if (isset($params['title'])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public function modifySpec(RequestSpec $spec) {
$action = $spec->getAction();

$spec->getFieldByName('extends')
->setRequired($action === 'create')
->setSuffixes(['name', 'label', 'grouping']);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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 |
+--------------------------------------------------------------------+
*/

namespace Civi\Api4\Service\Spec\Provider;

use Civi\Api4\Service\Spec\RequestSpec;

class MembershipTypeCreationSpecProvider implements Generic\SpecProviderInterface {

/**
* @param \Civi\Api4\Service\Spec\RequestSpec $spec
*/
public function modifySpec(RequestSpec $spec): void {
$spec->getFieldByName('duration_interval')->setRequired(TRUE);
}

/**
* When does this apply.
*
* @param string $entity
* @param string $action
*
* @return bool
*/
public function applies($entity, $action): bool {
return $entity === 'MembershipType' && $action === 'create';
}

}
129 changes: 0 additions & 129 deletions tests/phpunit/api/v4/Action/ComplexQueryTest.php

This file was deleted.

28 changes: 25 additions & 3 deletions tests/phpunit/api/v4/Action/ContactGetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,7 @@ public function testAge(): void {
['first_name' => 'abc', 'last_name' => $lastName, 'birth_date' => 'now - 1 year - 1 month'],
['first_name' => 'def', 'last_name' => $lastName, 'birth_date' => 'now - 21 year - 6 month'],
];
Contact::save(FALSE)
->setRecords($sampleData)
->execute();
$this->saveTestRecords('Contact', ['records' => $sampleData]);

$result = Contact::get(FALSE)
->addWhere('last_name', '=', $lastName)
Expand All @@ -347,4 +345,28 @@ public function testAge(): void {
->execute()->single();
}

/**
*
*/
public function testGetWithCount() {
$myName = uniqid('count');
for ($i = 1; $i <= 20; ++$i) {
$this->createTestRecord('Contact', [
'first_name' => "Contact $i",
'last_name' => $myName,
]);
}

$get1 = Contact::get(FALSE)
->addWhere('last_name', '=', $myName)
->selectRowCount()
->addSelect('first_name')
->setLimit(10)
->execute();

$this->assertEquals(20, $get1->count());
$this->assertCount(10, (array) $get1);

}

}
Loading

0 comments on commit d3095ee

Please sign in to comment.