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

Phpspec Data Provider Extension Laravel Package

coduo/phpspec-data-provider-extension

PHPSpec extension adding data providers to run the same specification with multiple input/output datasets. Define examples in your specs for concise, parameterized tests and cleaner coverage of edge cases without duplicating scenarios.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the package via Composer in your Laravel project:

    composer require --dev coduo/phpspec-data-provider-extension
    
  2. Enable Extension In your phpspec.yml (or phpspec.yml.dist), add the extension under extensions:

    extensions:
        Coduo\PhpSpec\DataProviderExtension\DataProviderExtension:
            format: 'dsl' # or 'array'
    
  3. First Use Case Define a data provider in your spec file:

    // src/MyClassSpec.php
    namespace spec\App;
    
    use PhpSpec\ObjectBehavior;
    use Coduo\PhpSpec\DataProviderExtension\DataProvider;
    
    class MyClassSpec extends ObjectBehavior
    {
        public function it_handles_different_inputs(DataProvider $data)
        {
            $data->provide([
                'string input' => ['input' => 'test'],
                'integer input' => ['input' => 123],
            ])->then(function ($input) {
                // Your spec logic here
            });
        }
    }
    

Implementation Patterns

Workflow Integration

  1. Data-Driven Specs Use data providers to avoid repetitive it() blocks for similar test cases:

    public function it_processes_various_values(DataProvider $data)
    {
        $data->provide([
            'zero' => [0],
            'positive' => [5],
            'negative' => [-3],
        ])->then(function ($value) {
            $this->shouldReturnGreaterThanZero($value);
        });
    }
    
  2. External Data Sources Load test data from external files (e.g., data.yml):

    # data.yml
    users:
        - { id: 1, name: 'Alice' }
        - { id: 2, name: 'Bob' }
    
    $data->fromYaml('data.yml')->provide('users')->then(function ($user) {
        // Test user-related logic
    });
    
  3. Combining with Laravel Use the extension alongside Laravel’s testing helpers:

    public function it_validates_requests(DataProvider $data)
    {
        $data->provide([
            'valid' => [['name' => 'Valid']],
            'invalid' => [['name' => '']],
        ])->then(function ($input) {
            $response = $this->post('/api/users', $input);
            // Assertions...
        });
    }
    
  4. Shared Providers Define reusable providers in a base spec class:

    abstract class BaseSpec extends ObjectBehavior
    {
        protected function getTestData()
        {
            return [
                'edge case' => [null],
                'normal case' => ['valid'],
            ];
        }
    }
    

Gotchas and Tips

Pitfalls

  1. Archived Package The package is unmaintained (last release: 2015). Consider alternatives like:

    • phpspec/prophecy for mocking.
    • Custom data providers using PHP 8.1’s array union types or generators.
  2. DSL vs. Array Format

    • DSL: More readable but less flexible.
      $data->provide('name', 'age')->then(function ($name, $age) { ... });
      
    • Array: More control but verbose.
      $data->provide([['name' => 'Alice', 'age' => 30]])->then(function ($data) { ... });
      
  3. Laravel-Specific Quirks

    • Avoid mixing with Laravel’s DatabaseMigrations or RefreshDatabase traits, as data providers may conflict with test isolation.
    • Use beforeEach() to reset state between provider iterations.
  4. Debugging

    • Provider Mismatch: Ensure the number of then() arguments matches the provider keys.
      // Fails: $data->provide(['a'])->then(function ($a, $b)) { ... }
      
    • File Loading: Verify paths for external data (e.g., fromYaml('data.yml') expects the file in the project root).

Tips

  1. Combine with phpspec/extension Extend the extension for custom logic:

    use Coduo\PhpSpec\DataProviderExtension\DataProviderExtension;
    
    class CustomExtension extends DataProviderExtension
    {
        public function getConfig()
        {
            return [
                'format' => 'array',
                'custom_key' => 'value',
            ];
        }
    }
    
  2. Laravel Artisan Integration Run specs with Laravel’s test environment:

    phpspec run --config=phpspec.yml --env=testing
    
  3. Performance For large datasets, use ->skip() to exclude specific cases:

    $data->provide([...])->skip('edge case')->then(...);
    
  4. Legacy Code If stuck with this package, wrap providers in a service class to abstract away quirks:

    class SpecDataProvider
    {
        public function provide(array $data): DataProvider
        {
            return new DataProvider($data);
        }
    }
    
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
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
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