Filtering a Doctrine collection that implements Selectable interface

Code
<?php

use PHPSnippets\Db\Entity\Test;
use 
Doctrine\ORM\Query;
use 
Doctrine\Common\Collections\Collection;
use 
Doctrine\Common\Collections\Selectable;
use 
Doctrine\Common\Collections\Criteria;
use 
Doctrine\Common\Collections\ArrayCollection;

// function for collection output
$print = function(Collection $result) {
    
$output = [];
    foreach (
$result as $entity) {
        
$output[] = $entity->name;
    }
    echo 
implode(', '$output) .
        
str_repeat(PHP_EOL2);
};

$repo $entityManager->getRepository(Test::CLASS);

// similar to findAll() but returns result as Collection
$collection $repo->matching(Criteria::create());
$print($collection);

if (
$collection instanceOf Selectable) {
    
// expression to filter collection
    
$expr Criteria::expr()->eq('name''foo');

    
$criteria Criteria::create();
    
$criteria->where($expr);

    
// This works directly with the collection
    
$collection2 $collection->matching($criteria);
    
$print($collection2);
}
Result
foo, test, bar

foo
Used Versions
PHP 7.1, Zend 3.0, Symfony 3.2, Laravel 5.2, PHPUnit 5.6, Doctrine 2.6