3 namespace Shaarli\Api\Controllers
;
6 use Slim\Http\Environment
;
8 use Slim\Http\Response
;
13 * Test REST API controller Info.
15 * @package Api\Controllers
17 class InfoTest
extends \PHPUnit_Framework_TestCase
20 * @var string datastore to test write operations
22 protected static $testDatastore = 'sandbox/datastore.php';
25 * @var \ConfigManager instance
30 * @var \ReferenceLinkDB instance.
32 protected $refDB = null;
35 * @var Container instance.
40 * @var Info controller instance.
42 protected $controller;
45 * Before every test, instantiate a new Api with its config, plugins and links.
47 public function setUp()
49 $this->conf
= new \
ConfigManager('tests/utils/config/configJson.json.php');
50 $this->refDB
= new \
ReferenceLinkDB();
51 $this->refDB
->write(self
::$testDatastore);
53 $this->container
= new Container();
54 $this->container
['conf'] = $this->conf
;
55 $this->container
['db'] = new \
LinkDB(self
::$testDatastore, true, false);
57 $this->controller
= new Info($this->container
);
61 * After every test, remove the test datastore.
63 public function tearDown()
65 @unlink(self
::$testDatastore);
71 public function testGetInfo()
73 $env = Environment
::mock([
74 'REQUEST_METHOD' => 'GET',
76 $request = Request
::createFromEnvironment($env);
78 $response = $this->controller
->getInfo($request, new Response());
79 $this->assertEquals(200, $response->getStatusCode());
80 $data = json_decode((string) $response->getBody(), true);
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']);
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);
101 $response = $this->controller
->getInfo($request, new Response());
102 $this->assertEquals(200, $response->getStatusCode());
103 $data = json_decode((string) $response->getBody(), true);
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']);