Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Xml Faker Laravel Package

prewk/xml-faker

Generate fake XML easily for tests and demos with prewk/xml-faker. Define elements and attributes, create realistic sample documents fast, and avoid hand-writing fixtures. Lightweight helper for PHP projects that need quick XML payloads.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation

    composer require prewk/xml-faker
    

    Ensure fzaninotto/Faker is also installed (this package depends on it).

  2. Basic Usage

    use Prewk\XmlFaker\XmlFaker;
    use Faker\Factory as Faker;
    
    $faker = Faker::create();
    $xmlFaker = new XmlFaker($faker);
    
    $xml = $xmlFaker->fake('<?xml version="1.0"?><root><user>{$name}</user></root>');
    echo $xml;
    

    Outputs:

    <?xml version="1.0"?><root><user>John Doe</user></root>
    
  3. First Use Case Generate fake XML for testing API responses or database seeding:

    $fakeXml = $xmlFaker->fake('<response><id>{$id}</id><name>{$name}</name></response>');
    

Implementation Patterns

Core Workflows

  1. Dynamic XML Generation Use placeholders ({placeholder}) to inject Faker data:

    $xmlFaker->fake('<book><title>{$sentence}</title><author>{$name}</author></book>');
    
  2. Reusable Templates Store XML templates in a config file (e.g., config/fake_xml.php) and load them dynamically:

    $template = config('fake_xml.books');
    $xmlFaker->fake($template);
    
  3. Nested Structures Handle nested XML with recursive placeholders:

    $xmlFaker->fake('<catalog><book><title>{$sentence}</title><pages>{$numberBetween(100,500)}</pages></book></catalog>');
    
  4. Integration with Laravel Bind the service in AppServiceProvider:

    public function register()
    {
        $this->app->singleton(XmlFaker::class, function ($app) {
            return new XmlFaker(Faker::create());
        });
    }
    

    Inject via constructor or resolve via app():

    $xml = app(XmlFaker::class)->fake('<user>{$name}</user>');
    
  5. Batch Generation Loop to generate multiple fake XML entries:

    $users = [];
    for ($i = 0; $i < 5; $i++) {
        $users[] = $xmlFaker->fake('<user><id>{$unique->number(100)}</id><email>{$email}</email></user>');
    }
    

Gotchas and Tips

Pitfalls

  1. Placeholder Escaping If your XML contains literal { or }, escape them or use alternative delimiters (e.g., {$name} vs. {name}). The package uses {} by default. Fix: Use {$literal} for literal braces or adjust delimiters via:

    $xmlFaker->setDelimiters('{{', '}}');
    
  2. Faker Locale Mismatch Ensure Faker’s locale matches your expected output (e.g., en_US for US addresses). Set it explicitly:

    $faker = Faker::create('en_US');
    
  3. XML Validation Generated XML may not validate against strict schemas. Test with SimpleXMLElement or DOMDocument:

    $xml = simplexml_load_string($fakeXml);
    if ($xml === false) {
        throw new \RuntimeException("Invalid XML generated");
    }
    
  4. Performance with Large Data Avoid generating massive XML in memory. Stream or chunk data for large datasets:

    foreach (range(1, 1000) as $i) {
        $xmlFaker->fake('<item>{$loop->index}</item>'); // Uses Faker's loop context
    }
    
  5. Custom Faker Providers Extend Faker’s providers for domain-specific data. Register them before initializing XmlFaker:

    Faker::addProvider(new CustomFakerProvider($faker));
    

Tips

  1. Debugging Placeholders Use {$debug} to inspect unresolved placeholders:

    $xmlFaker->fake('<data>{$debug}</data>');
    
  2. Contextual Data Pass custom data to Faker for consistency:

    $faker->seed(1234);
    $xmlFaker->fake('<user><name>{$name}</name><age>{$numberBetween(20,40)}</age></user>');
    
  3. Laravel Blade Integration Create a Blade directive for reusable fake XML:

    Blade::directive('fakeXml', function ($expression) {
        return "<?php echo app(\Prewk\XmlFaker\XmlFaker::class)->fake($expression); ?>";
    });
    

    Usage:

    @fakeXml('<user>{$name}</user>')
    
  4. Testing Mock XmlFaker in tests for predictable outputs:

    $this->partialMock(XmlFaker::class, function ($mock) {
        $mock->shouldReceive('fake')->andReturn('<mocked></mocked>');
    });
    
  5. Configuration Override default settings in config/xml_faker.php:

    'delimiters' => ['{{', '}}'],
    'default_locale' => 'en_GB',
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
besmartand-pro/php-quality-config
sentix/ai-chatbot
codifyo/ts-generator-bundle
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor