Skip to content

Commit 6bea922

Browse files
committed
Merge branch 'release-1.1'
2 parents 6379c89 + 3cee5df commit 6bea922

File tree

10 files changed

+157
-4
lines changed

10 files changed

+157
-4
lines changed

.github/ISSUE_TEMPLATE/bug_report.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Steps to reproduce the behavior, including custom code if needed.
1717
A clear and concise description of what you expected to happen.
1818

1919
**Environment (please complete the following information):**
20-
- OS: [e.g. macOS]
20+
- PHP Version: [e.g. PHP 7.2.30]
2121
- Database: [e.g. mysql, mariadb]
2222
- xPDO and/or MODX Version: [e.g. xPDO 2.7.0-pl, MODX 2.7.2]
2323

CHANGELOG.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [Unreleased]
9+
10+
## [1.1.0] - 2020-05-08
11+
12+
### Added
13+
- This changelog file and notable changes from release [1.0.0].
14+
- Tests to ensure we can use custom column formatters when populate entities.
15+
16+
### Changed
17+
- Improve the metadata to clarify the intention of this project.
18+
19+
### Fixed
20+
- A working usage example in the README.
21+
22+
## [1.0.0] - 2020-04-30
23+
24+
### Added
25+
- Classes to populate xPDO objects with relevant fake data.
26+
- Test Suite (example package and Unit Tests).
27+
28+
[unreleased]: https://github.com/SpringbokAgency/faker-xpdo-orm-adapter/compare/v1.1.0...HEAD
29+
[1.1.0]: https://github.com/SpringbokAgency/faker-xpdo-orm-adapter/releases/tag/v1.0.0...v1.1.0
30+
[1.0.0]: https://github.com/SpringbokAgency/faker-xpdo-orm-adapter/releases/tag/v1.0.0

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ $ composer require --dev springbokagency/faker-xpdo-orm-adapter
1616
```
1717

1818
## Usage
19-
To populate xPDO objects, create a new populator class (using a generator instance as parameter), then list the class and number of all the objects that must be generated. To launch the actual data population, call the execute() method.
19+
To populate xPDO objects, create a new populator class (using a generator instance as first parameter, and a valid xPDO instance as second parameter), then list the class and number of all the objects that must be generated. To launch the actual data population, call the execute() method.
2020

2121
Here is an example showing how to populate 5 `modUser` and 10 `modResource` objects:
2222

2323
```php
2424
<?php
2525
$generator = \Faker\Factory::create();
26-
$populator = new \SpringbokAgency\Faker\ORM\xPDO\Populator($generator);
26+
$populator = new \SpringbokAgency\Faker\ORM\xPDO\Populator($generator, $xpdo);
2727
$populator->addEntity(\modUser::class, 5);
2828
$populator->addEntity(\modResource::class, 10);
2929
$insertedPKs = $populator->execute();

composer.json

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
{
22
"name": "springbokagency/faker-xpdo-orm-adapter",
33
"type": "library",
4-
"description": "xPDO ORM adapter for faker",
4+
"description": "A Faker ORM adapter to populate xPDO objects with fake data",
5+
"keywords": [
6+
"xpdo",
7+
"modx",
8+
"faker"
9+
],
510
"license": "MIT",
611
"authors": [
712
{
@@ -43,5 +48,9 @@
4348
"vendor/modxcms/xpdo2/xpdo/",
4449
"tests/test_package/core/components/faker-xpdo-orm-adapter/model/faker-xpdo-orm-adapter/"
4550
]
51+
},
52+
"support": {
53+
"issues": "https://github.com/SpringbokAgency/faker-xpdo-orm-adapter/issues",
54+
"source": "https://github.com/SpringbokAgency/faker-xpdo-orm-adapter"
4655
}
4756
}

tests/Faker/ORM/xPDO/PopulatorTest.php

+70
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
namespace SpringbokAgency\Tests\Faker\ORM\xPDO;
1111

12+
use CustomColumnFormatterFakerObject;
1213
use Faker\Factory;
14+
use Faker\Generator;
1315
use ParentChildrenFakerObject;
1416
use SimpleFakerObject;
1517
use SpringbokAgency\Faker\ORM\xPDO\Populator;
@@ -20,6 +22,7 @@ class PopulatorTest extends DatabaseTestCase
2022
protected $fixtures = [
2123
SimpleFakerObject::class,
2224
ParentChildrenFakerObject::class,
25+
CustomColumnFormatterFakerObject::class,
2326
];
2427

2528
/**
@@ -44,4 +47,71 @@ public function addEntityDataProvider()
4447
[ParentChildrenFakerObject::class, 10, 10],
4548
];
4649
}
50+
51+
/**
52+
* @depends testAddEntityAndExecute
53+
* @dataProvider customColumnFormattersProvider
54+
*/
55+
public function testAddEntityAndExecuteWithCustomColumnFormatters($className, $formatters)
56+
{
57+
$generator = Factory::create();
58+
$generator->seed(1234);
59+
60+
$customFormatters = [];
61+
$expected = [];
62+
foreach ($formatters as $column => $formatter) {
63+
if ($formatter === null) {
64+
$customFormatters[$column] = null;
65+
66+
$classFields = self::$xpdo->getFields($className);
67+
if (!array_key_exists($column, $classFields)) {
68+
continue;
69+
}
70+
71+
$expected[$column] = $classFields[$column];
72+
73+
continue;
74+
}
75+
76+
$customFormatters[$column] = $formatter($generator);
77+
$expected[$column] = $formatter($generator)();
78+
}
79+
80+
$populator = new Populator($generator, self::$xpdo);
81+
82+
$populator->addEntity($className, 1, $customFormatters);
83+
$populatedEntities = $populator->execute();
84+
85+
$primarKeyField = self::$xpdo->getPK($className);
86+
foreach ($populatedEntities[$className] as $primaryKey) {
87+
$entity = self::$xpdo->getObject(
88+
$className,
89+
array_merge(
90+
[
91+
$primarKeyField => $primaryKey,
92+
],
93+
$expected
94+
)
95+
);
96+
97+
$this->assertNotNull($entity);
98+
}
99+
}
100+
101+
public function customColumnFormattersProvider()
102+
{
103+
return [
104+
[
105+
CustomColumnFormatterFakerObject::class,
106+
[
107+
'class_key' => function (Generator $generator) {
108+
return function () use ($generator) {
109+
return CustomColumnFormatterFakerObject::class;
110+
};
111+
},
112+
'do_not_populate' => null,
113+
],
114+
],
115+
];
116+
}
47117
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php
2+
class CustomColumnFormatterFakerObject extends xPDOSimpleObject {}

tests/test_package/core/components/faker-xpdo-orm-adapter/model/faker-xpdo-orm-adapter/metadata.mysql.php

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
array (
66
0 => 'SimpleFakerObject',
77
1 => 'ColumnTypeGuesserFakerObject',
8+
2 => 'CustomColumnFormatterFakerObject',
89
),
910
'SimpleFakerObject' =>
1011
array (
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
require_once (dirname(dirname(__FILE__)) . '/customcolumnformatterfakerobject.class.php');
3+
class CustomColumnFormatterFakerObject_mysql extends CustomColumnFormatterFakerObject {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
$xpdo_meta_map['CustomColumnFormatterFakerObject']= array (
3+
'package' => 'faker-xpdo-orm-adapter',
4+
'version' => '1.1',
5+
'table' => 'custom_column_formatter_faker_objects',
6+
'extends' => 'xPDOSimpleObject',
7+
'tableMeta' =>
8+
array (
9+
'engine' => 'InnoDB',
10+
),
11+
'fields' =>
12+
array (
13+
'class_key' => NULL,
14+
'do_not_populate' => 'not populated with fake data',
15+
),
16+
'fieldMeta' =>
17+
array (
18+
'class_key' =>
19+
array (
20+
'dbtype' => 'varchar',
21+
'precision' => '255',
22+
'phptype' => 'string',
23+
'null' => true,
24+
),
25+
'do_not_populate' =>
26+
array (
27+
'dbtype' => 'text',
28+
'phptype' => 'string',
29+
'null' => false,
30+
'default' => 'not populated with fake data',
31+
),
32+
),
33+
);

tests/test_package/core/components/faker-xpdo-orm-adapter/model/schema/faker-xpdo-orm-adapter.mysql.schema.xml

+5
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,9 @@
2626

2727
<field key="object" dbtype="text" phptype="object" null="false" default="not_sure"/>
2828
</object>
29+
30+
<object class="CustomColumnFormatterFakerObject" table="custom_column_formatter_faker_objects" extends="xPDOSimpleObject">
31+
<field key="class_key" dbtype="varchar" precision="255" phptype="string" null="true"/>
32+
<field key="do_not_populate" dbtype="text" phptype="string" null="false" default="not populated with fake data"/>
33+
</object>
2934
</model>

0 commit comments

Comments
 (0)