Skip to content

Commit

Permalink
Merge branch 'release-15.8.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
shpran committed Nov 4, 2021
2 parents dfe0d8c + bc8424f commit dbc67fb
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions test/IteratorMockTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2021 (original work) Open Assessment Technologies SA;
*/

declare(strict_types=1);

namespace oat\generis\test;

use stdClass;
use PHPUnit\Framework\MockObject\MockObject;

trait IteratorMockTrait
{
public function createIteratorMock(string $originalClassName, array $items = []): MockObject
{
$iteratorData = new stdClass();
$iteratorData->array = $items;
$iteratorData->position = 0;

$iteratorMock = $this->createMock($originalClassName);

$iteratorMock
->expects($this->any())
->method('rewind')
->will(
$this->returnCallback(
static function() use ($iteratorData): void {
$iteratorData->position = 0;
}
)
);

$iteratorMock
->expects($this->any())
->method('current')
->will(
$this->returnCallback(
static function() use ($iteratorData) {
return $iteratorData->array[$iteratorData->position];
}
)
);

$iteratorMock
->expects($this->any())
->method('key')
->will(
$this->returnCallback(
static function() use ($iteratorData) {
return $iteratorData->position;
}
)
);

$iteratorMock
->expects($this->any())
->method('next')
->will(
$this->returnCallback(
static function() use ($iteratorData): void {
++$iteratorData->position;
}
)
);

$iteratorMock
->expects($this->any())
->method('valid')
->will(
$this->returnCallback(
static function() use ($iteratorData): bool {
return isset($iteratorData->array[$iteratorData->position]);
}
)
);

$iteratorMock
->expects($this->any())
->method('count')
->will(
$this->returnCallback(
static function() use ($iteratorData): int {
return count($iteratorData->array);
}
)
);

return $iteratorMock;
}
}

0 comments on commit dbc67fb

Please sign in to comment.