Open Journal Systems  3.3.0
testfetch.php
1 <?php
2 
3 require_once 'lib/bagit_fetch.php';
4 require_once 'lib/bagit_utils.php';
5 
6 class BagItFetchTest extends PHPUnit_Framework_TestCase
7 {
8  var $tmpdir;
9  var $fetch;
10 
11  public function setUp()
12  {
13  $this->tmpdir = tmpdir();
14  mkdir($this->tmpdir);
15 
16  file_put_contents(
17  "{$this->tmpdir}/fetch.txt",
18  "http://www.google.com - data/google/index.html\n" .
19  "http://www.yahoo.com - data/yahoo/index.html\n"
20  );
21 
22  $this->fetch = new BagItFetch("{$this->tmpdir}/fetch.txt");
23  }
24 
25  public function tearDown()
26  {
27  rrmdir($this->tmpdir);
28  }
29 
30  public function testFileName()
31  {
32  $this->assertEquals(
33  "{$this->tmpdir}/fetch.txt",
34  $this->fetch->fileName
35  );
36  }
37 
38  public function testData()
39  {
40  $data = $this->fetch->data;
41 
42  $this->assertEquals(2, count($data));
43  $this->assertEquals("http://www.google.com", $data[0]['url']);
44  $this->assertEquals("http://www.yahoo.com", $data[1]['url']);
45  }
46 
47  public function testRead()
48  {
49  file_put_contents(
50  "{$this->tmpdir}/fetch.txt",
51  "http://www.scholarslab.org/ - data/scholarslab/index.html"
52  );
53 
54  $this->fetch->read();
55 
56  $this->assertFalse(
57  array_key_exists('data/google/index.html', $this->fetch->data)
58  );
59  $this->assertFalse(
60  array_key_exists('data/yahoo/index.html', $this->fetch->data)
61  );
62  $this->assertEquals(
63  'data/scholarslab/index.html',
64  $this->fetch->data[0]['filename']
65  );
66  }
67 
68  public function testWrite()
69  {
70  array_push(
71  $this->fetch->data,
72  array('url' => 'http://www.scholarslab.org/', 'length' => '-', 'filename' => 'data/scholarslab/index.html')
73  );
74  $this->fetch->write();
75  $this->assertEquals(
76  "http://www.google.com - data/google/index.html\n" .
77  "http://www.yahoo.com - data/yahoo/index.html\n" .
78  "http://www.scholarslab.org/ - data/scholarslab/index.html\n",
79  file_get_contents("{$this->tmpdir}/fetch.txt")
80  );
81  }
82 
83  public function testGetData()
84  {
85  $data = $this->fetch->getData();
86 
87  $this->assertEquals(2, count($data));
88  $this->assertEquals("http://www.google.com", $data[0]['url']);
89  $this->assertEquals("http://www.yahoo.com", $data[1]['url']);
90  }
91 
92  public function testDownload()
93  {
94  $tmp = $this->tmpdir;
95 
96  $this->assertFalse(is_file("$tmp/data/google/index.html"));
97  $this->assertFalse(is_file("$tmp/data/yahoo/index.html"));
98 
99  $errors = array();
100  $this->fetch->download();
101 
102  $this->assertFileExists("$tmp/data/google/index.html");
103  $this->assertFileExists("$tmp/data/yahoo/index.html");
104  }
105 
106  public function testAdd()
107  {
108  $this->assertEquals(2, count($this->fetch->data));
109 
110  $this->fetch->add(
111  'http://www.scholarslab.org/',
112  'data/scholarslab/index.html'
113  );
114 
115  $this->assertEquals(3, count($this->fetch->data));
116  $this->assertEquals(
117  'data/scholarslab/index.html',
118  $this->fetch->data[2]['filename']
119  );
120 
121  $this->assertEquals(
122  "http://www.google.com - data/google/index.html\n" .
123  "http://www.yahoo.com - data/yahoo/index.html\n" .
124  "http://www.scholarslab.org/ - data/scholarslab/index.html\n",
125  file_get_contents("{$this->tmpdir}/fetch.txt")
126  );
127  }
128 
129  public function testClear()
130  {
131  $this->assertEquals(2, count($this->fetch->data));
132 
133  $this->fetch->clear();
134 
135  $this->assertEquals(0, count($this->fetch->data));
136  $this->assertFalse(
137  array_key_exists('data/google/index.html', $this->fetch->data)
138  );
139  $this->assertFalse(
140  array_key_exists('data/yahoo/index.html', $this->fetch->data)
141  );
142  }
143 
144  public function testEmptyWrite()
145  {
146  $this->fetch->clear();
147  $this->fetch->write();
148  $this->assertFileNotExists("{$this->tmpdir}/fetch.txt");
149  }
150 
151  public function testNewBagEmpty()
152  {
153  $bagdir = "{$this->tmpdir}/_bag";
154 
155  $bag = new BagIt($bagdir);
156  $this->assertFileNotExists("$bagdir/fetch.txt");
157 
158  $bag->update();
159  $this->assertFileNotExists("$bagdir/fetch.txt");
160  }
161 
162 }
163 
164 ?>
BagItFetchTest\testNewBagEmpty
testNewBagEmpty()
Definition: testfetch.php:151
BagItFetchTest
Definition: testfetch.php:6
BagItFetchTest\testData
testData()
Definition: testfetch.php:38
BagItFetchTest\testGetData
testGetData()
Definition: testfetch.php:83
BagItFetchTest\testEmptyWrite
testEmptyWrite()
Definition: testfetch.php:144
BagItFetch
Definition: bagit_fetch.php:45
BagItFetchTest\testWrite
testWrite()
Definition: testfetch.php:68
BagItFetchTest\testRead
testRead()
Definition: testfetch.php:47
BagItFetchTest\setUp
setUp()
Definition: testfetch.php:11
BagItFetchTest\testAdd
testAdd()
Definition: testfetch.php:106
BagItFetchTest\$fetch
$fetch
Definition: testfetch.php:9
BagItFetchTest\testFileName
testFileName()
Definition: testfetch.php:30
BagItFetchTest\testClear
testClear()
Definition: testfetch.php:129
BagItFetchTest\tearDown
tearDown()
Definition: testfetch.php:25
BagItFetchTest\$tmpdir
$tmpdir
Definition: testfetch.php:8
BagItFetchTest\testDownload
testDownload()
Definition: testfetch.php:92
BagIt
Definition: bagit.php:69