aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/api/controllers/GetLinkIdTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/api/controllers/GetLinkIdTest.php')
-rw-r--r--tests/api/controllers/GetLinkIdTest.php130
1 files changed, 130 insertions, 0 deletions
diff --git a/tests/api/controllers/GetLinkIdTest.php b/tests/api/controllers/GetLinkIdTest.php
new file mode 100644
index 00000000..1b020505
--- /dev/null
+++ b/tests/api/controllers/GetLinkIdTest.php
@@ -0,0 +1,130 @@
1<?php
2
3namespace Shaarli\Api\Controllers;
4
5
6use Slim\Container;
7use Slim\Http\Environment;
8use Slim\Http\Request;
9use Slim\Http\Response;
10
11/**
12 * Class GetLinkIdTest
13 *
14 * Test getLink by ID API service.
15 *
16 * @see http://shaarli.github.io/api-documentation/#links-link-get
17 *
18 * @package Shaarli\Api\Controllers
19 */
20class GetLinkIdTest extends \PHPUnit_Framework_TestCase
21{
22 /**
23 * @var string datastore to test write operations
24 */
25 protected static $testDatastore = 'sandbox/datastore.php';
26
27 /**
28 * @var \ConfigManager instance
29 */
30 protected $conf;
31
32 /**
33 * @var \ReferenceLinkDB instance.
34 */
35 protected $refDB = null;
36
37 /**
38 * @var Container instance.
39 */
40 protected $container;
41
42 /**
43 * @var Links controller instance.
44 */
45 protected $controller;
46
47 /**
48 * Number of JSON fields per link.
49 */
50 const NB_FIELDS_LINK = 9;
51
52 /**
53 * Before each test, instantiate a new Api with its config, plugins and links.
54 */
55 public function setUp()
56 {
57 $this->conf = new \ConfigManager('tests/utils/config/configJson');
58 $this->refDB = new \ReferenceLinkDB();
59 $this->refDB->write(self::$testDatastore);
60
61 $this->container = new Container();
62 $this->container['conf'] = $this->conf;
63 $this->container['db'] = new \LinkDB(self::$testDatastore, true, false);
64
65 $this->controller = new Links($this->container);
66 }
67
68 /**
69 * After each test, remove the test datastore.
70 */
71 public function tearDown()
72 {
73 @unlink(self::$testDatastore);
74 }
75
76 /**
77 * Test basic getLink service: return link ID=41.
78 */
79 public function testGetLinkId()
80 {
81 // Used by index_url().
82 $_SERVER['SERVER_NAME'] = 'domain.tld';
83 $_SERVER['SERVER_PORT'] = 80;
84 $_SERVER['SCRIPT_NAME'] = '/';
85
86 $id = 41;
87 $env = Environment::mock([
88 'REQUEST_METHOD' => 'GET',
89 ]);
90 $request = Request::createFromEnvironment($env);
91
92 $response = $this->controller->getLink($request, new Response(), ['id' => $id]);
93 $this->assertEquals(200, $response->getStatusCode());
94 $data = json_decode((string) $response->getBody(), true);
95 $this->assertEquals(self::NB_FIELDS_LINK, count($data));
96 $this->assertEquals($id, $data['id']);
97
98 // Check link elements
99 $this->assertEquals('http://domain.tld/?WDWyig', $data['url']);
100 $this->assertEquals('WDWyig', $data['shorturl']);
101 $this->assertEquals('Link title: @website', $data['title']);
102 $this->assertEquals(
103 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag',
104 $data['description']
105 );
106 $this->assertEquals('sTuff', $data['tags'][0]);
107 $this->assertEquals(false, $data['private']);
108 $this->assertEquals(
109 \DateTime::createFromFormat(\LinkDB::LINK_DATE_FORMAT, '20150310_114651')->format(\DateTime::ATOM),
110 $data['created']
111 );
112 $this->assertEmpty($data['updated']);
113 }
114
115 /**
116 * Test basic getLink service: get non existent link => ApiLinkNotFoundException.
117 *
118 * @expectedException Shaarli\Api\Exceptions\ApiLinkNotFoundException
119 * @expectedExceptionMessage Link not found
120 */
121 public function testGetLink404()
122 {
123 $env = Environment::mock([
124 'REQUEST_METHOD' => 'GET',
125 ]);
126 $request = Request::createFromEnvironment($env);
127
128 $this->controller->getLink($request, new Response(), ['id' => -1]);
129 }
130}