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