forked from ben-xo/dir2cast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMedia_RSS_Item_SerializationTest.php
76 lines (62 loc) · 2.42 KB
/
Media_RSS_Item_SerializationTest.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
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
class Media_RSS_Item_SerializationTest extends TestCase
{
public static function setUpBeforeClass(): void
{
RSS_File_Item::$FILES_URL = 'http://www.example.com/mp3/';
RSS_File_Item::$FILES_DIR = getcwd();
}
public function setUp(): void
{
file_put_contents('example.mp3', '');
}
public function test_serialize_and_deserialize_yield_the_same_thing()
{
$item = new Media_RSS_Item('example.mp3');
$item->setA('a');
$item->setB('b');
$serialized = serialize($item);
$item2 = unserialize($serialized);
$this->assertEquals('a', $item2->getA());
$this->assertEquals('b', $item2->getB());
}
public function test_unserialize_rejects_invalid_serialization_version()
{
$item = new Media_RSS_Item('example.mp3');
$item->setA('a');
$item->setB('b');
$serialized = serialize($item);
$serialized = preg_replace('/:"serialVersion";i:\d+;/', ':"serialVersion";i:0;', $serialized);
$this->expectException(SerializationException::class);
$item2 = unserialize($serialized);
}
/**
* TODO: I am using serialize/unserialize wrong here. I should unset these properties on load, not save.
* But that would require a version bump in the cache constant.
*/
public function test_unserialize_does_not_overwrite_properties_set_from_fs_metadata()
{
$item = new Media_RSS_Item('example.mp3');
$item->setA('a');
$item->setB('b');
$serialized = $item->serialize($item);
file_put_contents('example2.abc', 'abcde');
$filemtime = time()-100;
touch('example2.abc', $filemtime);
$item2 = new Media_RSS_Item('example2.abc');
$item2->unserialize($serialized);
$this->assertEquals('a', $item2->getA());
$this->assertEquals('b', $item2->getB());
$this->assertEquals(5, $item2->getLength());
$this->assertEquals(date('r', $filemtime), $item2->getPubDate());
$this->assertEquals('example2.abc', $item2->getFilename());
$this->assertEquals('abc', $item2->getExtension());
$this->assertEquals('http://www.example.com/mp3/example2.abc', $item2->getLink());
}
public function tearDown(): void
{
unlink('example.mp3');
file_exists('example2.abc') && unlink('example2.abc');
}
}