PHP code example of opctim / bruno-lang

1. Go to this page and download the library: Download opctim/bruno-lang library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

opctim / bruno-lang example snippets


 declare(strict_types=1);

use Opctim\BrunoLang\V1\Block\Entry\DictionaryBlockEntry;
use Opctim\BrunoLang\V1\BruFile;
use Opctim\BrunoLang\V1\Collection;
use Opctim\BrunoLang\V1\Tag\Schema\BodyJsonTag;
use Opctim\BrunoLang\V1\Tag\Schema\HeadersTag;
use Opctim\BrunoLang\V1\Tag\Schema\MetaTag;
use Opctim\BrunoLang\V1\Tag\Schema\PostTag;
use Opctim\BrunoLang\V1\Tag\Schema\ScriptPostResponseTag;
use Opctim\BrunoLang\V1\Tag\Schema\VarsTag;

$collection = new Collection(
    // Metadata, will be written to bruno.json
    [
        'version' => '1',
        'name' => 'my_awesome_collection',
        'type' => 'collection',
        'ignore' => [
            'node_modules',
            '.git'
        ]
    ],
    // Request definitions
    [
        new BruFile('Login request', [
            new MetaTag([
                new DictionaryBlockEntry('name', 'Login request'), // should be the same as the .bru file name
                new DictionaryBlockEntry('type', 'http'),
                new DictionaryBlockEntry('seq', '1') // Used to order the requests in Bruno
            ]),
            new PostTag([
                new DictionaryBlockEntry('url', '{{baseUrl}}/api/login'),
                new DictionaryBlockEntry('body', 'json'),
                new DictionaryBlockEntry('auth', 'none'),
            ]),
            new HeadersTag([
                new DictionaryBlockEntry('Content-Type', 'application/json'),
                new DictionaryBlockEntry('X-Custom', '1234', false),
            ]),
            new BodyJsonTag(
'{
  "username": "[email protected]",
  "password": "1234"
}'
            ),
            new ScriptPostResponseTag("bru.setVar('auth_token', res.body.token)")
        ])
    ],
    // Environments, as bruno files. The BruFile::name is the environment name.
    [
        new BruFile('local', [
            new VarsTag([
                new DictionaryBlockEntry('baseUrl', 'https://localhost')
            ])
        ])
    ]
);

// Write the changes to disk
$collection->write('/path/to/my/collection');

new Collection(
    $metadata,
    [
        new BruFile('root_request', [
            // ...
        ]),
        'my' => [
            'nested' => [
                new BruFile('request', [
                    // ...
                ]),
            ]
        ]
    ],
    $environments
);

 declare(strict_types=1);

use Opctim\BrunoLang\V1\Block\Entry\DictionaryBlockEntry;
use Opctim\BrunoLang\V1\Collection;
use Opctim\BrunoLang\V1\Tag\Schema\MetaTag;

$path = '/path/to/my/collection';

$collection = Collection::parse($path);

// Change all request types to 'http'
foreach ($collection->getRequests() as $request) {
    $meta = $request->findOneBlockByName('meta');
    
    if ($meta instanceof MetaTag) {
        $entry = $meta->findOneBlockEntryByName('type');
        
        if ($entry instanceof DictionaryBlockEntry) {
            $entry->setValue('http');
        }
    }
}

// Write the changes to disk
$collection->write($path);