movemoveapp/laravel-postmangen
Laravel package that generates a Postman collection (JSON) from HTTP requests executed during PHPUnit tests. Configure via .env and phpunit.xml, install the PHPUnit extension and middleware to capture requests and export collections automatically.
composer require movemoveapp/laravel-postmangen
php artisan vendor:publish --provider="MoveMoveIo\Postmangen\PostmangenServiceProvider"
.env:
POSTMANGEN_TMP=postman/
phpunit.xml:
Add the extension under <extensions>:
<extensions>
<bootstrap class="MoveMoveIo\Postmangen\PostmangenPhpunitExtension">
<parameter name="outputDir" value="postman/"/>
</bootstrap>
</extensions>
app/Http/Kernel.php):
protected $middleware = [
\MoveMoveIo\Postmangen\Phpunit\Middleware\PostmangenMiddleware::class,
// ... other middleware
];
Run PHPUnit with the config file:
./vendor/bin/phpunit -c phpunit.xml
A JSON Postman collection (<APP_NAME>.postman_collection.json) will be generated in postman/.
Test-Driven API Capture:
GET /api/users).Grouping Logic:
method + URI).@postmangenMustCapture annotation to force-capture requests that wouldn’t group (e.g., dynamic routes):
/** @postmangenMustCapture */
public function test_dynamic_route() { ... }
Output Customization:
outputDir in phpunit.xml to change the output path.PostmangenPhpunitExtension or overriding the PostmangenMiddleware.phpunit --filter="TestClass" to avoid generating collections for subsets of tests (disabled by default in v1.1.2+).PostmangenMiddleware before auth/middleware that might alter requests (e.g., VerifyCsrfToken).git add postman/<APP_NAME>.postman_collection.json && git commit -m "Update Postman collection"
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class UserApiTest extends TestCase
{
use RefreshDatabase;
/** @postmangenMustCapture */ // Forces capture for dynamic route
public function test_create_user()
{
$response = $this->post('/api/users', ['name' => 'John']);
$response->assertCreated();
}
public function test_list_users()
{
$this->get('/api/users')->assertOk(); // Auto-captured (GET /api/users)
}
}
Failed Tests:
--stop-on-failure to bypass this.--verbose to debug failures before regenerating.Dynamic Routes:
/users/1 vs /users/2) may merge in the collection.@postmangenMustCapture or customize the grouping logic in the middleware.Middleware Conflicts:
VerifyCsrfToken or auth may alter requests before capture.PostmangenMiddleware first in Kernel.php to capture raw requests.Non-Standard Responses:
statusCode()/statusText() (e.g., PSR-7 implementations) may break (fixed in v1.1.6).UndefinedMethodException if collection generation fails.Date Headers:
filterHeaders logic.dd($this->app->make('postmangen')->getRequests()) in a test to inspect captured requests.PostmangenMiddleware to confirm requests are being intercepted:
\Log::debug('Postmangen captured:', ['uri' => $request->fullUrl()]);
POSTMANGEN_TMP in .env is writable and doesn’t conflict with other paths.Custom Collection Format:
Override the PostmangenPhpunitExtension to modify the JSON structure:
class CustomPostmangenExtension extends \MoveMoveIo\Postmangen\PostmangenPhpunitExtension
{
protected function transformRequest($request) { ... }
protected function transformResponse($response) { ... }
}
Register it in phpunit.xml:
<bootstrap class="App\Tests\CustomPostmangenExtension"/>
Filter Requests:
Extend PostmangenMiddleware to exclude specific routes or methods:
public function handle($request, Closure $next)
{
if ($request->is('admin/*')) return $next($request);
// ... rest of logic
}
Post-Processing:
Use Laravel’s commands to process the generated JSON (e.g., validate schema or upload to Postman):
Artisan::command('postmangen:validate', function () {
$collection = file_get_contents('postman/collection.json');
// Add validation logic
});
outputDir in phpunit.xml must match the .env POSTMANGEN_TMP path exactly (including trailing slashes)..env.testing to override POSTMANGEN_TMP for test environments:
POSTMANGEN_TMP=storage/postman/
How can I help you explore Laravel packages today?