3 namespace Shaarli\Api\Controllers
;
7 use Slim\Http\Environment
;
9 use Slim\Http\Response
;
14 * Test getLink by ID API service.
16 * @see http://shaarli.github.io/api-documentation/#links-link-get
18 * @package Shaarli\Api\Controllers
20 class GetLinkIdTest
extends \PHPUnit_Framework_TestCase
23 * @var string datastore to test write operations
25 protected static $testDatastore = 'sandbox/datastore.php';
28 * @var \ConfigManager instance
33 * @var \ReferenceLinkDB instance.
35 protected $refDB = null;
38 * @var Container instance.
43 * @var Links controller instance.
45 protected $controller;
48 * Number of JSON fields per link.
50 const NB_FIELDS_LINK
= 9;
53 * Before each test, instantiate a new Api with its config, plugins and links.
55 public function setUp()
57 $this->conf
= new \
ConfigManager('tests/utils/config/configJson');
58 $this->refDB
= new \
ReferenceLinkDB();
59 $this->refDB
->write(self
::$testDatastore);
61 $this->container
= new Container();
62 $this->container
['conf'] = $this->conf
;
63 $this->container
['db'] = new \
LinkDB(self
::$testDatastore, true, false);
65 $this->controller
= new Links($this->container
);
69 * After each test, remove the test datastore.
71 public function tearDown()
73 @unlink(self
::$testDatastore);
77 * Test basic getLink service: return link ID=41.
79 public function testGetLinkId()
81 // Used by index_url().
82 $_SERVER['SERVER_NAME'] = 'domain.tld';
83 $_SERVER['SERVER_PORT'] = 80;
84 $_SERVER['SCRIPT_NAME'] = '/';
87 $env = Environment
::mock([
88 'REQUEST_METHOD' => 'GET',
90 $request = Request
::createFromEnvironment($env);
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']);
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']);
103 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag',
106 $this->assertEquals('sTuff', $data['tags'][0]);
107 $this->assertEquals(false, $data['private']);
109 \DateTime
::createFromFormat(\LinkDB
::LINK_DATE_FORMAT
, '20150310_114651')->format(\DateTime
::ATOM
),
112 $this->assertEmpty($data['updated']);
116 * Test basic getLink service: get non existent link => ApiLinkNotFoundException.
118 * @expectedException Shaarli\Api\Exceptions\ApiLinkNotFoundException
119 * @expectedExceptionMessage Link not found
121 public function testGetLink404()
123 $env = Environment
::mock([
124 'REQUEST_METHOD' => 'GET',
126 $request = Request
::createFromEnvironment($env);
128 $this->controller
->getLink($request, new Response(), ['id' => -1]);