/
ArrayTaskRepositoryTest.php
152 lines (118 loc) · 4.44 KB
/
ArrayTaskRepositoryTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/*
* This file is part of php-task library.
*
* (c) php-task
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Task\Tests\Unit\Storage\ArrayStorage;
use Cron\CronExpression;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Prophecy\Argument;
use Task\Storage\ArrayStorage\ArrayTaskRepository;
use Task\Task;
use Task\TaskInterface;
/**
* Tests for ArrayTaskRepository.
*/
class ArrayTaskRepositoryTest extends \PHPUnit_Framework_TestCase
{
public function testFindByUuid()
{
$tasks = [
new Task(\stdClass::class, 'Test 1', '123-123-123'),
new Task(\stdClass::class, 'Test 2'),
new Task(\stdClass::class, 'Test 3'),
];
$repository = new ArrayTaskRepository(new ArrayCollection($tasks));
$this->assertEquals($tasks[0], $repository->findByUuid('123-123-123'));
}
public function testFindByUuidNotExisting()
{
$tasks = [
new Task(\stdClass::class, 'Test 1'),
new Task(\stdClass::class, 'Test 2'),
new Task(\stdClass::class, 'Test 3'),
];
$repository = new ArrayTaskRepository(new ArrayCollection($tasks));
$this->assertNull($repository->findByUuid('123-123-123'));
}
public function testPersist()
{
$collection = $this->prophesize(Collection::class);
$repository = new ArrayTaskRepository($collection->reveal());
$task = $this->prophesize(TaskInterface::class);
$collection->add($task->reveal())->shouldBeCalled();
$this->assertEquals(
$repository,
$repository->persist($task->reveal())
);
}
public function testRemove()
{
$collection = $this->prophesize(Collection::class);
$repository = new ArrayTaskRepository($collection->reveal());
$task = $this->prophesize(TaskInterface::class);
$collection->removeElement($task->reveal())->shouldBeCalled();
$this->assertEquals(
$repository,
$repository->remove($task->reveal())
);
}
public function testFlush()
{
$collection = $this->prophesize(Collection::class);
$repository = new ArrayTaskRepository($collection->reveal());
$collection->add(Argument::any())->shouldNotBeCalled();
$this->assertEquals($repository, $repository->flush());
}
public function testFindAll()
{
$tasks = [
new Task(\stdClass::class, 'Test 1'),
new Task(\stdClass::class, 'Test 2'),
new Task(\stdClass::class, 'Test 3'),
];
$repository = new ArrayTaskRepository(new ArrayCollection($tasks));
$result = $repository->findAll();
$this->assertCount(3, $result);
$this->assertEquals($tasks[0], $result[0]);
$this->assertEquals($tasks[1], $result[1]);
$this->assertEquals($tasks[2], $result[2]);
}
public function testFindAllPaginated()
{
$tasks = [
new Task(\stdClass::class, 'Test 1'),
new Task(\stdClass::class, 'Test 2'),
new Task(\stdClass::class, 'Test 3'),
];
$repository = new ArrayTaskRepository(new ArrayCollection($tasks));
$result = $repository->findAll(1, 2);
$this->assertCount(2, $result);
$this->assertEquals($tasks[0], $result[0]);
$this->assertEquals($tasks[1], $result[1]);
$result = $repository->findAll(2, 2);
$this->assertCount(1, $result);
$this->assertEquals($tasks[2], $result[0]);
}
public function testFindEndBeforeNow()
{
$tasks = [
(new Task(\stdClass::class, 'Test 1'))
->setInterval(CronExpression::factory('@daily'), new \DateTime(), new \DateTime('+1 day')),
(new Task(\stdClass::class, 'Test 2'))
->setInterval(CronExpression::factory('@yearly'), new \DateTime('-2 day'), new \DateTime('-1 day')),
(new Task(\stdClass::class, 'Test 3'))
->setInterval(CronExpression::factory('@monthly'), new \DateTime(), new \DateTime('+1 day')),
];
$repository = new ArrayTaskRepository(new ArrayCollection($tasks));
$result = $repository->findEndBeforeNow();
$this->assertCount(2, $result);
$this->assertEquals($tasks[0], $result[0]);
$this->assertEquals($tasks[2], $result[1]);
}
}