diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-10-13 12:05:08 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-10-13 12:05:08 +0200 |
commit | b6f678a5a1d15acf284ebcec16c905e976671ce1 (patch) | |
tree | 33c7da831482ed79c44896ef19c73c72ada84f2e /tests/api/controllers/links/PostLinkTest.php | |
parent | b14687036b9b800681197f51fdc47e62f0c88e2e (diff) | |
parent | 1c1520b6b98ab20201bfe15577782a52320339df (diff) | |
download | Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.tar.gz Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.tar.zst Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.zip |
Merge branch 'v0.12' into latest
Diffstat (limited to 'tests/api/controllers/links/PostLinkTest.php')
-rw-r--r-- | tests/api/controllers/links/PostLinkTest.php | 40 |
1 files changed, 24 insertions, 16 deletions
diff --git a/tests/api/controllers/links/PostLinkTest.php b/tests/api/controllers/links/PostLinkTest.php index d683a984..fe3de66f 100644 --- a/tests/api/controllers/links/PostLinkTest.php +++ b/tests/api/controllers/links/PostLinkTest.php | |||
@@ -2,9 +2,11 @@ | |||
2 | 2 | ||
3 | namespace Shaarli\Api\Controllers; | 3 | namespace Shaarli\Api\Controllers; |
4 | 4 | ||
5 | use PHPUnit\Framework\TestCase; | 5 | use Shaarli\Bookmark\Bookmark; |
6 | use Shaarli\Bookmark\BookmarkFileService; | ||
6 | use Shaarli\Config\ConfigManager; | 7 | use Shaarli\Config\ConfigManager; |
7 | use Shaarli\History; | 8 | use Shaarli\History; |
9 | use Shaarli\TestCase; | ||
8 | use Slim\Container; | 10 | use Slim\Container; |
9 | use Slim\Http\Environment; | 11 | use Slim\Http\Environment; |
10 | use Slim\Http\Request; | 12 | use Slim\Http\Request; |
@@ -41,6 +43,11 @@ class PostLinkTest extends TestCase | |||
41 | protected $refDB = null; | 43 | protected $refDB = null; |
42 | 44 | ||
43 | /** | 45 | /** |
46 | * @var BookmarkFileService instance. | ||
47 | */ | ||
48 | protected $bookmarkService; | ||
49 | |||
50 | /** | ||
44 | * @var HistoryController instance. | 51 | * @var HistoryController instance. |
45 | */ | 52 | */ |
46 | protected $history; | 53 | protected $history; |
@@ -61,29 +68,30 @@ class PostLinkTest extends TestCase | |||
61 | const NB_FIELDS_LINK = 9; | 68 | const NB_FIELDS_LINK = 9; |
62 | 69 | ||
63 | /** | 70 | /** |
64 | * Before every test, instantiate a new Api with its config, plugins and links. | 71 | * Before every test, instantiate a new Api with its config, plugins and bookmarks. |
65 | */ | 72 | */ |
66 | public function setUp() | 73 | protected function setUp(): void |
67 | { | 74 | { |
68 | $this->conf = new ConfigManager('tests/utils/config/configJson.json.php'); | 75 | $this->conf = new ConfigManager('tests/utils/config/configJson'); |
76 | $this->conf->set('resource.datastore', self::$testDatastore); | ||
69 | $this->refDB = new \ReferenceLinkDB(); | 77 | $this->refDB = new \ReferenceLinkDB(); |
70 | $this->refDB->write(self::$testDatastore); | 78 | $this->refDB->write(self::$testDatastore); |
71 | |||
72 | $refHistory = new \ReferenceHistory(); | 79 | $refHistory = new \ReferenceHistory(); |
73 | $refHistory->write(self::$testHistory); | 80 | $refHistory->write(self::$testHistory); |
74 | $this->history = new History(self::$testHistory); | 81 | $this->history = new History(self::$testHistory); |
82 | $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true); | ||
75 | 83 | ||
76 | $this->container = new Container(); | 84 | $this->container = new Container(); |
77 | $this->container['conf'] = $this->conf; | 85 | $this->container['conf'] = $this->conf; |
78 | $this->container['db'] = new \Shaarli\Bookmark\LinkDB(self::$testDatastore, true, false); | 86 | $this->container['db'] = $this->bookmarkService; |
79 | $this->container['history'] = new History(self::$testHistory); | 87 | $this->container['history'] = $this->history; |
80 | 88 | ||
81 | $this->controller = new Links($this->container); | 89 | $this->controller = new Links($this->container); |
82 | 90 | ||
83 | $mock = $this->createMock(Router::class); | 91 | $mock = $this->createMock(Router::class); |
84 | $mock->expects($this->any()) | 92 | $mock->expects($this->any()) |
85 | ->method('relativePathFor') | 93 | ->method('relativePathFor') |
86 | ->willReturn('api/v1/links/1'); | 94 | ->willReturn('api/v1/bookmarks/1'); |
87 | 95 | ||
88 | // affect @property-read... seems to work | 96 | // affect @property-read... seems to work |
89 | $this->controller->getCi()->router = $mock; | 97 | $this->controller->getCi()->router = $mock; |
@@ -99,7 +107,7 @@ class PostLinkTest extends TestCase | |||
99 | /** | 107 | /** |
100 | * After every test, remove the test datastore. | 108 | * After every test, remove the test datastore. |
101 | */ | 109 | */ |
102 | public function tearDown() | 110 | protected function tearDown(): void |
103 | { | 111 | { |
104 | @unlink(self::$testDatastore); | 112 | @unlink(self::$testDatastore); |
105 | @unlink(self::$testHistory); | 113 | @unlink(self::$testHistory); |
@@ -118,16 +126,16 @@ class PostLinkTest extends TestCase | |||
118 | 126 | ||
119 | $response = $this->controller->postLink($request, new Response()); | 127 | $response = $this->controller->postLink($request, new Response()); |
120 | $this->assertEquals(201, $response->getStatusCode()); | 128 | $this->assertEquals(201, $response->getStatusCode()); |
121 | $this->assertEquals('api/v1/links/1', $response->getHeader('Location')[0]); | 129 | $this->assertEquals('api/v1/bookmarks/1', $response->getHeader('Location')[0]); |
122 | $data = json_decode((string) $response->getBody(), true); | 130 | $data = json_decode((string) $response->getBody(), true); |
123 | $this->assertEquals(self::NB_FIELDS_LINK, count($data)); | 131 | $this->assertEquals(self::NB_FIELDS_LINK, count($data)); |
124 | $this->assertEquals(43, $data['id']); | 132 | $this->assertEquals(43, $data['id']); |
125 | $this->assertRegExp('/[\w_-]{6}/', $data['shorturl']); | 133 | $this->assertRegExp('/[\w_-]{6}/', $data['shorturl']); |
126 | $this->assertEquals('http://domain.tld/?' . $data['shorturl'], $data['url']); | 134 | $this->assertEquals('http://domain.tld/shaare/' . $data['shorturl'], $data['url']); |
127 | $this->assertEquals('?' . $data['shorturl'], $data['title']); | 135 | $this->assertEquals('/shaare/' . $data['shorturl'], $data['title']); |
128 | $this->assertEquals('', $data['description']); | 136 | $this->assertEquals('', $data['description']); |
129 | $this->assertEquals([], $data['tags']); | 137 | $this->assertEquals([], $data['tags']); |
130 | $this->assertEquals(false, $data['private']); | 138 | $this->assertEquals(true, $data['private']); |
131 | $this->assertTrue( | 139 | $this->assertTrue( |
132 | new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) | 140 | new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) |
133 | ); | 141 | ); |
@@ -163,7 +171,7 @@ class PostLinkTest extends TestCase | |||
163 | $response = $this->controller->postLink($request, new Response()); | 171 | $response = $this->controller->postLink($request, new Response()); |
164 | 172 | ||
165 | $this->assertEquals(201, $response->getStatusCode()); | 173 | $this->assertEquals(201, $response->getStatusCode()); |
166 | $this->assertEquals('api/v1/links/1', $response->getHeader('Location')[0]); | 174 | $this->assertEquals('api/v1/bookmarks/1', $response->getHeader('Location')[0]); |
167 | $data = json_decode((string) $response->getBody(), true); | 175 | $data = json_decode((string) $response->getBody(), true); |
168 | $this->assertEquals(self::NB_FIELDS_LINK, count($data)); | 176 | $this->assertEquals(self::NB_FIELDS_LINK, count($data)); |
169 | $this->assertEquals(43, $data['id']); | 177 | $this->assertEquals(43, $data['id']); |
@@ -211,11 +219,11 @@ class PostLinkTest extends TestCase | |||
211 | $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']); | 219 | $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']); |
212 | $this->assertEquals(false, $data['private']); | 220 | $this->assertEquals(false, $data['private']); |
213 | $this->assertEquals( | 221 | $this->assertEquals( |
214 | \DateTime::createFromFormat(\Shaarli\Bookmark\LinkDB::LINK_DATE_FORMAT, '20130614_184135'), | 222 | \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20130614_184135'), |
215 | \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) | 223 | \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) |
216 | ); | 224 | ); |
217 | $this->assertEquals( | 225 | $this->assertEquals( |
218 | \DateTime::createFromFormat(\Shaarli\Bookmark\LinkDB::LINK_DATE_FORMAT, '20130615_184230'), | 226 | \DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20130615_184230'), |
219 | \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']) | 227 | \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']) |
220 | ); | 228 | ); |
221 | } | 229 | } |