Using jQuery Select2 with Codeception

It took me some time to figure out how to fill out a jQuery Select2 dropdown during acceptance testing. The search box is not connected to the input field but appended to the DOM. This will do the trick:

/**
* Fill out select2 option field
*
* @param \Codeception\Actor $I
* @param string $selector
* @param string $value
*
* @return void
*/
public function fillOutSelect2OptionField(Actor $I, $selector, $value)
{
$element = '#select2-' . $selector . '-container';
    $I->click($element);
    $searchField = '.select2-search__field';
    $I->waitForElementVisible($searchField);
    $I->fillField($searchField, $value);
    $I->pressKey($searchField, WebDriverKeys::ENTER);
}