3 namespace Shaarli\Api\Controllers
;
5 use Shaarli\Config\ConfigManager
;
8 use Slim\Http\Environment
;
10 use Slim\Http\Response
;
15 * Test getLink by ID API service.
17 * @see http://shaarli.github.io/api-documentation/#links-link-get
19 * @package Shaarli\Api\Controllers
21 class GetLinkIdTest
extends \PHPUnit_Framework_TestCase
24 * @var string datastore to test write operations
26 protected static $testDatastore = 'sandbox/datastore.php';
29 * @var ConfigManager instance
34 * @var \ReferenceLinkDB instance.
36 protected $refDB = null;
39 * @var Container instance.
44 * @var Links controller instance.
46 protected $controller;
49 * Number of JSON fields per link.
51 const NB_FIELDS_LINK
= 9;
54 * Before each test, instantiate a new Api with its config, plugins and links.
56 public function setUp()
58 $this->conf
= new ConfigManager('tests/utils/config/configJson');
59 $this->refDB
= new \
ReferenceLinkDB();
60 $this->refDB
->write(self
::$testDatastore);
62 $this->container
= new Container();
63 $this->container
['conf'] = $this->conf
;
64 $this->container
['db'] = new \
LinkDB(self
::$testDatastore, true, false);
65 $this->container
['history'] = null;
67 $this->controller
= new Links($this->container
);
71 * After each test, remove the test datastore.
73 public function tearDown()
75 @unlink(self
::$testDatastore);
79 * Test basic getLink service: return link ID=41.
81 public function testGetLinkId()
83 // Used by index_url().
84 $_SERVER['SERVER_NAME'] = 'domain.tld';
85 $_SERVER['SERVER_PORT'] = 80;
86 $_SERVER['SCRIPT_NAME'] = '/';
89 $env = Environment
::mock([
90 'REQUEST_METHOD' => 'GET',
92 $request = Request
::createFromEnvironment($env);
94 $response = $this->controller
->getLink($request, new Response(), ['id' => $id]);
95 $this->assertEquals(200, $response->getStatusCode());
96 $data = json_decode((string) $response->getBody(), true);
97 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
98 $this->assertEquals($id, $data['id']);
100 // Check link elements
101 $this->assertEquals('http://domain.tld/?WDWyig', $data['url']);
102 $this->assertEquals('WDWyig', $data['shorturl']);
103 $this->assertEquals('Link title: @website', $data['title']);
105 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag',
108 $this->assertEquals('sTuff', $data['tags'][0]);
109 $this->assertEquals(false, $data['private']);
111 \DateTime
::createFromFormat(\LinkDB
::LINK_DATE_FORMAT
, '20150310_114651')->format(\DateTime
::ATOM
),
114 $this->assertEmpty($data['updated']);
118 * Test basic getLink service: get non existent link => ApiLinkNotFoundException.
120 * @expectedException Shaarli\Api\Exceptions\ApiLinkNotFoundException
121 * @expectedExceptionMessage Link not found
123 public function testGetLink404()
125 $env = Environment
::mock([
126 'REQUEST_METHOD' => 'GET',
128 $request = Request
::createFromEnvironment($env);
130 $this->controller
->getLink($request, new Response(), ['id' => -1]);