PHP code example of ajant / db-mock-library

1. Go to this page and download the library: Download ajant/db-mock-library 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/ */

    

ajant / db-mock-library example snippets


use DbMockLibrary/DbMockLibrary

...
// 2 tables, 2 rows each
$data = [
    'table_1' => [
        -1 => ['foo' => 20, 'id' => -1],
        -2 => ['foo' => 50, 'id' => -2]
    ],
    'table_2' => [
        -1 => ['bar' => 30, 'id' => -1, 'table_1_id' => -1],
        -2 => ['bar' => 10, 'id' => -2, 'table_1_id' => -2]
    ]
];
// table_1_id is foreign key, referencing id column
$dependencies = [
    [
        DependencyHandler::DEPENDENT => ['table_2' => 'table_1_id'],
        DependencyHandler::ON        => ['table_1' => 'id']
    ]
];
// initialize MySQL
MySQL::initMySQL($data, 'localhost', 'DbMockLibraryTest', 'root', '', $dependencies);
...

...
// inserts both rows of table_2 and both rows of table_1, because
MySQL::getInstance()->setUp(['table_2' => [-1, -2]]);
...

...
// removes all rows inserted during set up phase
MySQL::getInstance()->cleanUp();
...

...
// 4 indexes, 2 rows each
// 4th index is percolator index
$data = [
    'index_1' => [
        0 => ['foo' => 20, 'id' => -1],
        1 => ['foo' => 50, 'id' => -2],
    ],
    'index_2' => [
        0 => ['bar' => 30, 'id' => -1, 'index_1_id' => -1],
        1 => ['bar' => 10, 'id' => -2, 'index_1_id' => -2],
    ],
    'index_3' => [
        'record1' => ['field1' => 'value11', 'field2' => 'value12'],
        'record2' => ['field1' => 'value21', 'field2' => 'value22'],
    ],
    'index_4' => [
        'percolatorRecord1' => [
            'routing' => 0,
            'body' => [
                'query' => [
                    'bool' => [
                        'minimum_number_should_match' => 1,
                        'should' => [
                            [
                                'match' => [
                                    'someField' => [
                                        'query' => 'foo',
                                        'operator' => 'and'
                                    ],
                                ],
                            ],
                        ],
                    ],
                ],
            ],
        ],
        'percolatorRecord2' => [
            'routing' => 1,
            'body' => [
                'query' => [
                    'terms' => [
                        'someField' => [
                            'someValue'
                        ],
                    ],
                ],
            ],
        ],
    ],
];
// index_1_id is foreign key, referencing id column
$dependencies = [
    [
        DependencyHandler::DEPENDENT => ['index_2' => 'index_1_id'],
        DependencyHandler::ON        => ['index_1' => 'id'],
    ],
];
$indexTypes = [
    'index_1' => 'someType',
    'index_2' => 'someType',
    'index_3' => 'someType',
    'index_4' => '.percolator',
];
// initialize Elasticsearch
$client = \Elasticsearch\ClientBuilder::create()->setHosts(['http://localhost:9200'])->build();
Elasticsearch::initElasticsearch($client, $data, $dependencies, $indexTypes);
...

...
// inserts both rows of index_2 and both rows of index_1, because
Elasticsearch::getInstance()->setUp(['index_2' => [0, 1]]);
// inserts all indexes
Elasticsearch::getInstance()->setUp();
// inserts only index_1
Elasticsearch::getInstance()->setUp(['index_1']);
...

...
// removes all rows inserted during set up phase, including dependencies
Elasticsearch::getInstance()->cleanUp();
// removes all indexes
Elasticsearch::getInstance()->tearDown();
// removes only index_1
Elasticsearch::getInstance()->tearDown(['index_1']);
...