Skip to content

Commit

Permalink
AddEntityTest - Convert example FK's from XML-schema to PHP-schema
Browse files Browse the repository at this point in the history
  • Loading branch information
totten committed Jun 27, 2024
1 parent e02fe46 commit 516ed6a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 32 deletions.
23 changes: 23 additions & 0 deletions src/CRM/CivixBundle/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use CRM\CivixBundle\Utils\MixinLibraries;
use CRM\CivixBundle\Utils\Naming;
use CRM\CivixBundle\Utils\Path;
use PhpArrayDocument\Parser;
use PhpArrayDocument\Printer;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
Expand Down Expand Up @@ -162,6 +164,27 @@ public function updatePhpData($path, callable $filter): void {
$phpData->save($ctx, $this->output);
}

/**
* Update a PHP-style data-file. (If the file is new, create it.)
*
* Ex: updatePhpArrayDocument('foobar.mgd.php', fn(PhpArrayDocument $doc) => $doc->setInnerComment("Hello world"))
*
* TIP: updatePhpData() and updatePhpArrayDocument() fill a similar niche.
* - updatePhpArrayDocument reveals and preserves more metadata.
* - updatePhpData has a simpler API.
*
* @param $path
* @param callable $filter
*/
public function updatePhpArrayDocument($path, callable $filter): void {
$file = Path::for($path)->string();
$oldCode = file_get_contents($file);
$doc = (new Parser())->parse($oldCode);
$filter($doc);
$newCode = (new Printer())->print($doc);
$this->writeTextFile($file, $newCode, TRUE);
}

/**
* Update a managed-entity data-file. (If the file is new, create it.)
*
Expand Down
65 changes: 33 additions & 32 deletions tests/e2e/AddEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace E2E;

use PhpArrayDocument\ArrayNode;
use PhpArrayDocument\PhpArrayDocument;
use PhpArrayDocument\ScalarNode;

class AddEntityTest extends \PHPUnit\Framework\TestCase {

use CivixProjectTestTrait;
Expand Down Expand Up @@ -30,13 +34,19 @@ public function setUp(): void {
]);
chdir(static::getKey());

\Civix::ioStack()->push(...$this->createInputOutput());
$this->assertFileGlobs([
'info.xml' => 1,
'civix_addentity.php' => 1,
'civix_addentity.civix.php' => 1,
]);
}

protected function tearDown(): void {
parent::tearDown();
\Civix::ioStack()->reset();
}

public function testAddEntity(): void {
$this->assertFileGlobs([
'sql/auto_install.sql' => 0,
Expand All @@ -48,11 +58,11 @@ public function testAddEntity(): void {
$this->civixGenerateEntity('Sandwich');
$this->civixGenerateEntity('Meal');
$this->civixGenerateEntity('Flour');
$this->addExampleFK($this->getExtPath('xml/schema/CRM/CivixAddentity/Bread.xml'), 'flour', 'civicrm_flour');
$this->addExampleFK($this->getExtPath('xml/schema/CRM/CivixAddentity/Sandwich.xml'), 'bread', 'civicrm_bread');
$this->addExampleFK($this->getExtPath('xml/schema/CRM/CivixAddentity/Meal.xml'), 'sandwich', 'civicrm_sandwich');
$this->addExampleFK($this->getExtPath('schema/Bread.entityType.php'), 'flour', 'Flour','civicrm_flour');
$this->addExampleFK($this->getExtPath('schema/Sandwich.entityType.php'), 'bread', 'Bread', 'civicrm_bread');
$this->addExampleFK($this->getExtPath('schema/Meal.entityType.php'), 'sandwich', 'Sandwich', 'civicrm_sandwich');
// add FK referencing its own table
$this->addExampleFK($this->getExtPath('xml/schema/CRM/CivixAddentity/Meal.xml'), 'next_meal', 'civicrm_meal');
$this->addExampleFK($this->getExtPath('schema/Meal.entityType.php'), 'next_meal', 'Meal', 'civicrm_meal');

$this->assertFileGlobs([
// No longer use static files
Expand All @@ -77,34 +87,25 @@ private function grepLines(string $pattern, string $file): array {
return array_values(preg_grep($pattern, $lines));
}

private function addExampleFK(string $xmlFile, string $field, string $foreignTable) {
$newXmlTpl = '<field>
<name>%%FIELD%%</name>
<title>%%FIELD%% ID</title>
<type>int unsigned</type>
<comment>FK to %%TABLE%% ID</comment>
<html>
<label>%%TABLE%%</label>
</html>
<add>2.0</add>
</field>
<foreignKey>
<name>%%FIELD%%</name>
<table>%%TABLE%%</table>
<key>id</key>
<add>2.0</add>
<onDelete>CASCADE</onDelete>
</foreignKey>';
$newXml = strtr($newXmlTpl, [
'%%FIELD%%' => $field,
'%%TABLE%%' => $foreignTable,
]);

$tail = '</table>';

$raw = file_get_contents($xmlFile);
$raw = str_replace($tail, "{$newXml}\n{$tail}", $raw);
file_put_contents($xmlFile, $raw);
private function addExampleFK(string $schemaFile, string $fieldName, string $foreignEntity, string $foreignTable) {
\Civix::generator()->updatePhpArrayDocument($schemaFile, function (PhpArrayDocument $doc) use ($fieldName, $foreignEntity, $foreignTable) {
$field = ArrayNode::create()->importData([
'title' => ScalarNode::create("$fieldName ID")->setFactory('E::ts'),
'sql_type' => 'int unsigned',
'input_type' => 'EntityRef',
'description' => ScalarNode::create("FK to $foreignTable ID")->setFactory('E::ts'),
'add' => '2.0',
'input_attrs' => [
'label' => ScalarNode::create("$foreignTable")->setFactory('E::ts'), /* weird */
],
'entity_reference' => [
'entity' => $foreignEntity,
'key' => 'id',
'on_delete' => 'CASCADE',
],
]);
$doc->getRoot()['getFields'][$fieldName] = $field;
});
}

}

0 comments on commit 516ed6a

Please sign in to comment.