Skip to content

Factory generator - #38

Open
tomasvanrijsse wants to merge 2 commits into
crescat-io:masterfrom
Timatic:factory-generator
Open

Factory generator#38
tomasvanrijsse wants to merge 2 commits into
crescat-io:masterfrom
Timatic:factory-generator

Conversation

@tomasvanrijsse

@tomasvanrijsse tomasvanrijsse commented Jan 29, 2026

Copy link
Copy Markdown
Contributor

I made a factory generator so tests are easier to setup for users of my SDK.

### Testing

When testing code that uses the SDK, you can mock the connector and its responses using factories. The SDK includes factory classes for all DTOs that make it easy to generate test data.

Here's an example of testing the `CustomerController` from the example above:

```php
test('it displays customers', function () {
    // Generate test data using factories
    $customer = CustomerDto::factory()->state([
        'number' => 'CUST001',
        'name' => 'Acme Corporation',
        'status' => 'Active',
    ])->make();

    // Create mock responses using factory-generated data
    $mockClient = MockClient::global([
        CustomerGetAllRequest::class => MockResponse::make([
            'data' => [$customer->toArray()],
        ], 200)
    ]);

    // Make request
    $response = $this->get(route('customers.index'));

    // Assert
    $response->assertOk();
    $response->assertViewHas('customers');
    $response->assertViewHas('defaultCustomer');
});

tomasvanrijsse and others added 2 commits January 29, 2026 21:50
- Add factoryNamespaceSuffix config option (default: 'Factories')
- Create abstract Factory base class with Faker support
- Implement FactoryGenerator as PostProcessor
- Introspect PhpFile for both promoted params and public properties
- Generate smart Faker calls based on property names and types
- Support hookable getPropertiesToSkip() for customization
- Use TaggedOutputFile with 'factories' tag for output control
- Add comprehensive tests for factory generation

All 81 tests pass.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
- Split getDtoPropertiesFromPhpFile into focused single-responsibility methods
- Extract hasAttribute() helper to eliminate duplicate attribute checking
- Replace Laravel collections with native PHP for simpler operations
- Use strict comparisons throughout
- Consolidate duplicate return values
- Simplify constructor method access

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant