diff options
author | ArthurHoaro <arthur@hoa.ro> | 2016-12-15 10:13:00 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2016-12-15 10:36:00 +0100 |
commit | 18e6796726d73d7dc90ecdd16c181493941f5487 (patch) | |
tree | 17159284be5072b505eead31efdc064b6d5a35d0 /tests/api/controllers/InfoTest.php | |
parent | 423ab02846286f94276d21e38ca1e296646618bf (diff) | |
download | Shaarli-18e6796726d73d7dc90ecdd16c181493941f5487.tar.gz Shaarli-18e6796726d73d7dc90ecdd16c181493941f5487.tar.zst Shaarli-18e6796726d73d7dc90ecdd16c181493941f5487.zip |
REST API structure using Slim framework
* REST API routes are handle by Slim.
* Every API controller go through ApiMiddleware which handles security.
* First service implemented `/info`, for tests purpose.
Diffstat (limited to 'tests/api/controllers/InfoTest.php')
-rw-r--r-- | tests/api/controllers/InfoTest.php | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/tests/api/controllers/InfoTest.php b/tests/api/controllers/InfoTest.php new file mode 100644 index 00000000..2916eed8 --- /dev/null +++ b/tests/api/controllers/InfoTest.php | |||
@@ -0,0 +1,113 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Shaarli\Api\Controllers; | ||
4 | |||
5 | use Slim\Container; | ||
6 | use Slim\Http\Environment; | ||
7 | use Slim\Http\Request; | ||
8 | use Slim\Http\Response; | ||
9 | |||
10 | /** | ||
11 | * Class InfoTest | ||
12 | * | ||
13 | * Test REST API controller Info. | ||
14 | * | ||
15 | * @package Api\Controllers | ||
16 | */ | ||
17 | class InfoTest extends \PHPUnit_Framework_TestCase | ||
18 | { | ||
19 | /** | ||
20 | * @var string datastore to test write operations | ||
21 | */ | ||
22 | protected static $testDatastore = 'sandbox/datastore.php'; | ||
23 | |||
24 | /** | ||
25 | * @var \ConfigManager instance | ||
26 | */ | ||
27 | protected $conf; | ||
28 | |||
29 | /** | ||
30 | * @var \ReferenceLinkDB instance. | ||
31 | */ | ||
32 | protected $refDB = null; | ||
33 | |||
34 | /** | ||
35 | * @var Container instance. | ||
36 | */ | ||
37 | protected $container; | ||
38 | |||
39 | /** | ||
40 | * @var Info controller instance. | ||
41 | */ | ||
42 | protected $controller; | ||
43 | |||
44 | /** | ||
45 | * Before every test, instantiate a new Api with its config, plugins and links. | ||
46 | */ | ||
47 | public function setUp() | ||
48 | { | ||
49 | $this->conf = new \ConfigManager('tests/utils/config/configJson.json.php'); | ||
50 | $this->refDB = new \ReferenceLinkDB(); | ||
51 | $this->refDB->write(self::$testDatastore); | ||
52 | |||
53 | $this->container = new Container(); | ||
54 | $this->container['conf'] = $this->conf; | ||
55 | $this->container['db'] = new \LinkDB(self::$testDatastore, true, false); | ||
56 | |||
57 | $this->controller = new Info($this->container); | ||
58 | } | ||
59 | |||
60 | /** | ||
61 | * After every test, remove the test datastore. | ||
62 | */ | ||
63 | public function tearDown() | ||
64 | { | ||
65 | @unlink(self::$testDatastore); | ||
66 | } | ||
67 | |||
68 | /** | ||
69 | * Test /info service. | ||
70 | */ | ||
71 | public function testGetInfo() | ||
72 | { | ||
73 | $env = Environment::mock([ | ||
74 | 'REQUEST_METHOD' => 'GET', | ||
75 | ]); | ||
76 | $request = Request::createFromEnvironment($env); | ||
77 | |||
78 | $response = $this->controller->getInfo($request, new Response()); | ||
79 | $this->assertEquals(200, $response->getStatusCode()); | ||
80 | $data = json_decode((string) $response->getBody(), true); | ||
81 | |||
82 | $this->assertEquals(8, $data['global_counter']); | ||
83 | $this->assertEquals(2, $data['private_counter']); | ||
84 | $this->assertEquals('Shaarli', $data['settings']['title']); | ||
85 | $this->assertEquals('?', $data['settings']['header_link']); | ||
86 | $this->assertEquals('UTC', $data['settings']['timezone']); | ||
87 | $this->assertEquals(\ConfigManager::$DEFAULT_PLUGINS, $data['settings']['enabled_plugins']); | ||
88 | $this->assertEquals(false, $data['settings']['default_private_links']); | ||
89 | |||
90 | $title = 'My links'; | ||
91 | $headerLink = 'http://shaarli.tld'; | ||
92 | $timezone = 'Europe/Paris'; | ||
93 | $enabledPlugins = array('foo', 'bar'); | ||
94 | $defaultPrivateLinks = true; | ||
95 | $this->conf->set('general.title', $title); | ||
96 | $this->conf->set('general.header_link', $headerLink); | ||
97 | $this->conf->set('general.timezone', $timezone); | ||
98 | $this->conf->set('general.enabled_plugins', $enabledPlugins); | ||
99 | $this->conf->set('privacy.default_private_links', $defaultPrivateLinks); | ||
100 | |||
101 | $response = $this->controller->getInfo($request, new Response()); | ||
102 | $this->assertEquals(200, $response->getStatusCode()); | ||
103 | $data = json_decode((string) $response->getBody(), true); | ||
104 | |||
105 | $this->assertEquals(8, $data['global_counter']); | ||
106 | $this->assertEquals(2, $data['private_counter']); | ||
107 | $this->assertEquals($title, $data['settings']['title']); | ||
108 | $this->assertEquals($headerLink, $data['settings']['header_link']); | ||
109 | $this->assertEquals($timezone, $data['settings']['timezone']); | ||
110 | $this->assertEquals($enabledPlugins, $data['settings']['enabled_plugins']); | ||
111 | $this->assertEquals($defaultPrivateLinks, $data['settings']['default_private_links']); | ||
112 | } | ||
113 | } | ||