diff options
author | ArthurHoaro <arthur@hoa.ro> | 2018-07-10 18:06:26 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-07-10 18:06:26 +0200 |
commit | c2c2338f9adecf971d01d7abcd09bab6275a9706 (patch) | |
tree | 9c8fe4d6c1ca1d5667b6bce786d872471322fac1 /tests/api/controllers/links/GetLinkIdTest.php | |
parent | edb4a4d9c9fbc01d67ee8d095fe26648714e2285 (diff) | |
parent | 6410bf96707e381685b7dafbefcccd83ea762934 (diff) | |
download | Shaarli-c2c2338f9adecf971d01d7abcd09bab6275a9706.tar.gz Shaarli-c2c2338f9adecf971d01d7abcd09bab6275a9706.tar.zst Shaarli-c2c2338f9adecf971d01d7abcd09bab6275a9706.zip |
Merge pull request #1141 from ArthurHoaro/api/tags
Implements Tags endpoints for Shaarli's REST API
Diffstat (limited to 'tests/api/controllers/links/GetLinkIdTest.php')
-rw-r--r-- | tests/api/controllers/links/GetLinkIdTest.php | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/tests/api/controllers/links/GetLinkIdTest.php b/tests/api/controllers/links/GetLinkIdTest.php new file mode 100644 index 00000000..57528d5a --- /dev/null +++ b/tests/api/controllers/links/GetLinkIdTest.php | |||
@@ -0,0 +1,132 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Shaarli\Api\Controllers; | ||
4 | |||
5 | use Shaarli\Config\ConfigManager; | ||
6 | |||
7 | use Slim\Container; | ||
8 | use Slim\Http\Environment; | ||
9 | use Slim\Http\Request; | ||
10 | use Slim\Http\Response; | ||
11 | |||
12 | /** | ||
13 | * Class GetLinkIdTest | ||
14 | * | ||
15 | * Test getLink by ID API service. | ||
16 | * | ||
17 | * @see http://shaarli.github.io/api-documentation/#links-link-get | ||
18 | * | ||
19 | * @package Shaarli\Api\Controllers | ||
20 | */ | ||
21 | class GetLinkIdTest extends \PHPUnit_Framework_TestCase | ||
22 | { | ||
23 | /** | ||
24 | * @var string datastore to test write operations | ||
25 | */ | ||
26 | protected static $testDatastore = 'sandbox/datastore.php'; | ||
27 | |||
28 | /** | ||
29 | * @var ConfigManager instance | ||
30 | */ | ||
31 | protected $conf; | ||
32 | |||
33 | /** | ||
34 | * @var \ReferenceLinkDB instance. | ||
35 | */ | ||
36 | protected $refDB = null; | ||
37 | |||
38 | /** | ||
39 | * @var Container instance. | ||
40 | */ | ||
41 | protected $container; | ||
42 | |||
43 | /** | ||
44 | * @var Links controller instance. | ||
45 | */ | ||
46 | protected $controller; | ||
47 | |||
48 | /** | ||
49 | * Number of JSON fields per link. | ||
50 | */ | ||
51 | const NB_FIELDS_LINK = 9; | ||
52 | |||
53 | /** | ||
54 | * Before each test, instantiate a new Api with its config, plugins and links. | ||
55 | */ | ||
56 | public function setUp() | ||
57 | { | ||
58 | $this->conf = new ConfigManager('tests/utils/config/configJson'); | ||
59 | $this->refDB = new \ReferenceLinkDB(); | ||
60 | $this->refDB->write(self::$testDatastore); | ||
61 | |||
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; | ||
66 | |||
67 | $this->controller = new Links($this->container); | ||
68 | } | ||
69 | |||
70 | /** | ||
71 | * After each test, remove the test datastore. | ||
72 | */ | ||
73 | public function tearDown() | ||
74 | { | ||
75 | @unlink(self::$testDatastore); | ||
76 | } | ||
77 | |||
78 | /** | ||
79 | * Test basic getLink service: return link ID=41. | ||
80 | */ | ||
81 | public function testGetLinkId() | ||
82 | { | ||
83 | // Used by index_url(). | ||
84 | $_SERVER['SERVER_NAME'] = 'domain.tld'; | ||
85 | $_SERVER['SERVER_PORT'] = 80; | ||
86 | $_SERVER['SCRIPT_NAME'] = '/'; | ||
87 | |||
88 | $id = 41; | ||
89 | $env = Environment::mock([ | ||
90 | 'REQUEST_METHOD' => 'GET', | ||
91 | ]); | ||
92 | $request = Request::createFromEnvironment($env); | ||
93 | |||
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']); | ||
99 | |||
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']); | ||
104 | $this->assertEquals( | ||
105 | 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag', | ||
106 | $data['description'] | ||
107 | ); | ||
108 | $this->assertEquals('sTuff', $data['tags'][0]); | ||
109 | $this->assertEquals(false, $data['private']); | ||
110 | $this->assertEquals( | ||
111 | \DateTime::createFromFormat(\LinkDB::LINK_DATE_FORMAT, '20150310_114651')->format(\DateTime::ATOM), | ||
112 | $data['created'] | ||
113 | ); | ||
114 | $this->assertEmpty($data['updated']); | ||
115 | } | ||
116 | |||
117 | /** | ||
118 | * Test basic getLink service: get non existent link => ApiLinkNotFoundException. | ||
119 | * | ||
120 | * @expectedException Shaarli\Api\Exceptions\ApiLinkNotFoundException | ||
121 | * @expectedExceptionMessage Link not found | ||
122 | */ | ||
123 | public function testGetLink404() | ||
124 | { | ||
125 | $env = Environment::mock([ | ||
126 | 'REQUEST_METHOD' => 'GET', | ||
127 | ]); | ||
128 | $request = Request::createFromEnvironment($env); | ||
129 | |||
130 | $this->controller->getLink($request, new Response(), ['id' => -1]); | ||
131 | } | ||
132 | } | ||