-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMongoDbTest.php
More file actions
95 lines (77 loc) · 3 KB
/
Copy pathMongoDbTest.php
File metadata and controls
95 lines (77 loc) · 3 KB
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
<?php
namespace Detain\SessionSamuraiTest;
use PHPUnit\Framework\TestCase;
use Detain\SessionSamurai\MongoDbSessionHandler;
use MongoDB\Collection;
use MongoDB\Model\BSONDocument;
use MongoDB\UpdateResult;
use MongoDB\DeleteResult;
use MongoDB\Driver\WriteResult;
class MongoDbTest extends TestCase
{
private MongoDbSessionHandler $sessionHandler;
private Collection $collection;
public function setUp(): void
{
$this->collection = $this->createMock(Collection::class);
$this->sessionHandler = new MongoDbSessionHandler($this->collection);
}
public function testOpenReturnsTrue()
{
$this->assertTrue($this->sessionHandler->open('', ''));
}
public function testCloseReturnsTrue()
{
$this->assertTrue($this->sessionHandler->close());
}
public function testReadReturnsData()
{
$data = '{foo:bar}';
$id = $this->sessionHandler->create_sid();
$doc = new BSONDocument(['_id' => $id, 'data' => $data]);
$this->collection->method('findOne')->willReturn($doc);
$this->assertEquals($data, $this->sessionHandler->read($id));
}
public function testReadMissingReturnsEmpty()
{
$this->collection->method('findOne')->willReturn(null);
$this->assertEquals('', $this->sessionHandler->read('nonexistent'));
}
public function testWriteReturnsTrue()
{
$updateResult = $this->createMock(UpdateResult::class);
$updateResult->method('getUpsertedCount')->willReturn(1);
$updateResult->method('getModifiedCount')->willReturn(0);
$this->collection->method('updateOne')->willReturn($updateResult);
$id = $this->sessionHandler->create_sid();
$this->assertTrue($this->sessionHandler->write($id, '{foo:bar}'));
}
public function testDestroyReturnsTrue()
{
$deleteResult = $this->createMock(DeleteResult::class);
$deleteResult->method('getDeletedCount')->willReturn(1);
$this->collection->method('deleteOne')->willReturn($deleteResult);
$id = $this->sessionHandler->create_sid();
$this->assertTrue($this->sessionHandler->destroy($id));
}
public function testCreateSidReturnsString()
{
$sid = $this->sessionHandler->create_sid();
$this->assertIsString($sid);
$this->assertMatchesRegularExpression('/^[a-f0-9]{64}$/', $sid);
}
public function testValidateIdReturnsBoolean()
{
$this->collection->method('countDocuments')->willReturn(1);
$id = $this->sessionHandler->create_sid();
$this->assertIsBool($this->sessionHandler->validateId($id));
}
public function testUpdateTimestampReturnsBoolean()
{
$updateResult = $this->createMock(UpdateResult::class);
$updateResult->method('getModifiedCount')->willReturn(1);
$this->collection->method('updateOne')->willReturn($updateResult);
$id = $this->sessionHandler->create_sid();
$this->assertIsBool($this->sessionHandler->updateTimestamp($id, 'data'));
}
}