b2pweb/bdf-phpunit
Laravel/PHPUnit helper package for working with BDF/SEPA bank files in tests. Provides utilities and test assertions to validate BDF structures and parsing results, making it easier to write reliable unit tests around banking file imports.
Installation
composer require --dev b2pweb/bdf-phpunit
Add the trait to your test class:
use B2PWeb\BDF\PHPUnit\BDFTestCase;
class ExampleTest extends BDFTestCase
{
// Your tests
}
First Use Case Use the built-in assertions for BDF (Business Data Framework) validation:
public function testValidation()
{
$this->assertBDFValid($this->createBDFModel(), 'model');
}
Key Files to Explore
src/BDFTestCase.php (Core trait with assertions)src/Assertions/BDFAssertions.php (Custom assertions)tests/ (Example test cases in the package)Model Validation Testing
public function testModelValidation()
{
$model = $this->createBDFModel(['invalid_field' => 'value']);
$this->assertBDFInvalid($model, 'model', [
'invalid_field' => 'The invalid field is required.'
]);
}
BDF Rule Testing
public function testCustomRule()
{
$this->assertBDFRuleValid('custom_rule', ['input' => 'value']);
$this->assertBDFRuleInvalid('custom_rule', ['input' => 'invalid']);
}
Integration with Laravel
Use the trait in Laravel’s TestCase:
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use B2PWeb\BDF\PHPUnit\BDFTestCase;
class BDFTest extends BaseTestCase
{
use BDFTestCase;
}
Mocking BDF Services
public function testServiceInteraction()
{
$mock = $this->mockBDFService('MyService');
$mock->shouldReceive('doSomething')->once();
$this->assertBDFServiceResponse($mock, 'doSomething');
}
BDFAssertions to add domain-specific checks.config('bdf') in tests if the package relies on BDF’s config.DatabaseTransactions for BDF-backed models.Assertion Naming Conflicts
Avoid naming custom assertions assertBDF* to prevent collisions with the package’s methods.
Missing BDF Context
Ensure BDF’s service container is bootstrapped before running tests (e.g., in setUp()):
public function setUp(): void
{
parent::setUp();
$this->app->make('bdf');
}
Overly Strict Validation
The package defaults to strict validation. Use assertBDFValidRelaxed() for partial checks:
$this->assertBDFValidRelaxed($model, ['field1', 'field2']);
Enable BDF Logging
Add to config/logging.php:
'channels' => [
'bdf' => [
'driver' => 'single',
'path' => storage_path('logs/bdf.log'),
'level' => 'debug',
],
],
Then use:
$this->enableBDFLogging();
Inspect Validation Rules Dump rules for a model:
$this->dumpBDFRules($model);
Custom Assertions
Add to BDFTestCase:
use B2PWeb\BDF\PHPUnit\Assertions\BDFAssertions;
class CustomBDFTest extends BDFTestCase
{
use BDFAssertions;
protected function assertCustomBDFRule($rule, $data, $message = '')
{
// Custom logic
}
}
Override Default Assertions
Extend BDFAssertions and bind it in setUp():
$this->assertions = new CustomBDFAssertions();
BDF Event Testing Listen for BDF events in tests:
$this->assertBDFEventDispatched('bdf.model.validated', function ($event) {
$this->assertEquals('model_id', $event->model->id);
});
How can I help you explore Laravel packages today?