summaryrefslogtreecommitdiffstats
path: root/application/test/tests/test_service_multipaste_queue.php
blob: 6bf078d97b6705e1d1a35f89809d61c1ade8770e (plain)
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
<?php
/*
 * Copyright 2016 Florian "Bluewind" Pritz <bluewind@server-speed.net>
 *
 * Licensed under AGPLv3
 * (see COPYING for full license text)
 *
 */

namespace test\tests;

class test_service_multipaste_queue extends \test\Test {

	private $session;
	private $mfile;
	private $mmultipaste;
	private $m;

	public function __construct()
	{
		parent::__construct();
	}

	public function init()
	{
		$this->session = \Mockery::mock("Session");
		$this->session->shouldReceive("userdata")->never()->byDefault();
		$this->session->shouldReceive("set_userdata")->never()->byDefault();

		$this->mfile = \Mockery::mock("Mfile");
		$this->mfile->shouldReceive("valid_id")->never()->byDefault();

		$this->mmultipaste = \Mockery::mock("Mmultipaste");
		$this->mmultipaste->shouldReceive("valid_id")->never()->byDefault();

		$this->m = new \service\multipaste_queue($this->session, $this->mfile, $this->mmultipaste);
	}

	public function cleanup()
	{
		\Mockery::close();
	}

	public function test_get()
	{
		$this->session->shouldReceive('userdata')->with("multipaste_queue")->once()->andReturn(null);
		$this->t->is_deeply($this->m->get(), [], "Fresh queue is empty");
	}

	public function test_set()
	{
		$this->session->shouldReceive('set_userdata')->with("multipaste_queue", ['abc', '123'])->once();

		$this->mfile->shouldReceive('valid_id')->with('abc')->once()->andReturn(true);
		$this->mfile->shouldReceive('valid_id')->with('123')->once()->andReturn(true);

		$this->t->is($this->m->set(['abc', '123']), null, "set() should succeed");
	}

	public function test_append()
	{
		$this->session->shouldReceive('userdata')->with("multipaste_queue")->once()->andReturn(null);
		$this->mfile->shouldReceive('valid_id')->with('abc')->times(2)->andReturn(true);
		$this->session->shouldReceive('set_userdata')->with("multipaste_queue", ['abc'])->once();
		$this->t->is($this->m->append(['abc']), null, "append([abc]) should succeed");

		$this->session->shouldReceive('userdata')->with("multipaste_queue")->once()->andReturn(['abc']);
		$this->mfile->shouldReceive('valid_id')->with('123')->once()->andReturn(true);
		$this->session->shouldReceive('set_userdata')->with("multipaste_queue", ['abc', '123'])->once();
		$this->t->is($this->m->append(['123']), null, "append([123]) should succeed");
	}

	public function test_append_itemAlreadyInQueue()
	{
		$this->session->shouldReceive('userdata')->with("multipaste_queue")->once()->andReturn(['abc', '123']);
		$this->mfile->shouldReceive('valid_id')->with('abc')->once()->andReturn(true);
		$this->mfile->shouldReceive('valid_id')->with('123')->once()->andReturn(true);
		$this->session->shouldReceive('set_userdata')->with("multipaste_queue", ['abc', '123'])->once();
		$this->t->is($this->m->append(['abc']), null, "append([abc]) should succeed");
	}

	public function test_append_multipaste()
	{
		$this->session->shouldReceive('userdata')->with("multipaste_queue")->once()->andReturn([]);
		$this->mmultipaste->shouldReceive('valid_id')->with('m-abc')->once()->andReturn(true);
		$this->mmultipaste->shouldReceive('get_files')->with('m-abc')->once()->andReturn([
			['id' => 'abc'],
			['id' => '123'],
		]);
		$this->mfile->shouldReceive('valid_id')->with('abc')->once()->andReturn(true);
		$this->mfile->shouldReceive('valid_id')->with('123')->once()->andReturn(true);
		$this->session->shouldReceive('set_userdata')->with("multipaste_queue", ['abc', '123'])->once();
		$this->t->is($this->m->append(['m-abc']), null, "append([m-abc]) should succeed");
	}


}