Completed
Push — master ( fbbea6...24edcd )
by Christian
01:44
created

testMessageConsumptionOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\NotificationBundle\Tests\Iterator;
15
16
use Doctrine\Common\Persistence\ManagerRegistry;
17
use PHPUnit\Framework\TestCase;
18
use Sonata\NotificationBundle\Iterator\MessageManagerMessageIterator as MessageManagerMessageIteratorObject;
19
use Sonata\NotificationBundle\Model\Message;
20
use Sonata\NotificationBundle\Model\MessageManagerInterface;
21
22
/**
23
 * @author Kevin Nedelec <[email protected]>
24
 */
25
class MessageManagerMessageIteratorTest extends TestCase
26
{
27
    /**
28
     * @var ManagerRegistry
29
     */
30
    private $registry;
31
32
    protected function setUp(): void
33
    {
34
        $this->registry = $this->createMock(ManagerRegistry::class);
35
    }
36
37
    public function testBufferize(): void
38
    {
39
        $iterator = new MessageManagerMessageIterator($this->registry, 0);
40
41
        $iterator->_bufferize();
42
43
        $this->assertCount(10, $iterator->getBuffer());
44
    }
45
46
    public function testIterations(): void
47
    {
48
        $size = 10;
49
50
        $iterator = new MessageManagerMessageIterator($this->registry, 0);
51
52
        $iterator->rewind();
53
        $this->assertTrue($iterator->valid());
54
        $this->assertNotNull($iterator->current());
55
56
        $iterator->next();
57
        $this->assertTrue($iterator->valid());
58
        $this->assertNotNull($iterator->current());
59
60
        --$size;
61
        while (--$size >= 1) {
62
            $iterator->next();
63
        }
64
65
        $this->assertTrue($iterator->valid());
66
        $this->assertNotNull($iterator->current());
67
    }
68
69
    public function testLongForeach(): void
70
    {
71
        $iterator = new MessageManagerMessageIterator($this->registry, 500000, 2);
72
73
        $count = 0;
74
75
        foreach ($iterator as $message) {
76
            ++$count;
77
            $this->assertNotNull($message);
78
            if ($count > 20) {
79
                return;
80
            }
81
        }
82
    }
83
84
    public function testMessageConsumptionOrder(): void
85
    {
86
        $now = new \DateTime();
87
        $olderDate = (clone $now)->modify('-1 hour');
88
        $oldMessage = new Message();
89
        $oldMessage->setCreatedAt($olderDate);
90
        $oldMessage->setType('older.message');
91
92
        $newMessage = new Message();
93
        $newMessage->setCreatedAt($now);
94
        $newMessage->setType('newer.message');
95
96
        $messageManager = $this->createMock(MessageManagerInterface::class);
97
        $messageManager->expects($this->once())->method('findByTypes')->willReturn([$oldMessage, $newMessage]);
98
99
        $iterator = new MessageManagerMessageIteratorObject($messageManager, [], 500000, 2);
100
101
        $iterator->next();
102
        $this->assertSame($oldMessage, $iterator->current());
103
        $iterator->next();
104
        $this->assertSame($newMessage, $iterator->current());
105
    }
106
}
107