BitmaskedBehaviorTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. namespace Tools\Test\TestCase\Model\Behavior;
  3. use RuntimeException;
  4. use Shim\TestSuite\TestCase;
  5. use TestApp\Model\Entity\BitmaskedComment;
  6. class BitmaskedBehaviorTest extends TestCase {
  7. /**
  8. * @var array
  9. */
  10. protected $fixtures = [
  11. 'plugin.Tools.BitmaskedComments',
  12. ];
  13. /**
  14. * @var \Tools\Model\Table\Table|\Tools\Model\Behavior\BitmaskedBehavior
  15. */
  16. protected $Comments;
  17. /**
  18. * @return void
  19. */
  20. public function setUp(): void {
  21. parent::setUp();
  22. $this->Comments = $this->getTableLocator()->get('BitmaskedComments');
  23. $this->Comments->addBehavior('Tools.Bitmasked', ['mappedField' => 'statuses']);
  24. }
  25. /**
  26. * @return void
  27. */
  28. public function testConfig() {
  29. $this->Comments->removeBehavior('Bitmasked');
  30. $this->Comments->addBehavior('Tools.Bitmasked', []);
  31. $bits = $this->Comments->behaviors()->Bitmasked->getConfig('bits');
  32. $expected = BitmaskedComment::statuses();
  33. $this->assertSame($expected, $bits);
  34. }
  35. /**
  36. * @return void
  37. */
  38. public function testFieldMethodMissing() {
  39. $this->Comments->removeBehavior('Bitmasked');
  40. $this->expectException(RuntimeException::class);
  41. $this->expectExceptionMessage('Bits not found for field my_field, expected pluralized static method myFields() on the entity.');
  42. $this->Comments->addBehavior('Tools.Bitmasked', ['field' => 'my_field']);
  43. }
  44. /**
  45. * @return void
  46. */
  47. public function testEncodeBitmask() {
  48. $res = $this->Comments->encodeBitmask([BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED]);
  49. $expected = BitmaskedComment::STATUS_PUBLISHED | BitmaskedComment::STATUS_APPROVED;
  50. $this->assertSame($expected, $res);
  51. }
  52. /**
  53. * @return void
  54. */
  55. public function testDecodeBitmask() {
  56. $res = $this->Comments->decodeBitmask(BitmaskedComment::STATUS_PUBLISHED | BitmaskedComment::STATUS_APPROVED);
  57. $expected = [BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED];
  58. $this->assertSame($expected, $res);
  59. }
  60. /**
  61. * @return void
  62. */
  63. public function testFind() {
  64. $res = $this->Comments->find('all')->toArray();
  65. $this->assertTrue(!empty($res) && is_array($res));
  66. $this->assertTrue(!empty($res[1]['statuses']) && is_array($res[1]['statuses']));
  67. }
  68. /**
  69. * @return void
  70. */
  71. public function testFindBitmasked() {
  72. $res = $this->Comments->find('bits', ['bits' => []])->toArray();
  73. $this->assertCount(1, $res);
  74. $this->assertSame([], $res[0]->statuses);
  75. $res = $this->Comments->find('bits', ['bits' => [BitmaskedComment::STATUS_ACTIVE, BitmaskedComment::STATUS_APPROVED]])->toArray();
  76. $this->assertCount(1, $res);
  77. $this->assertSame([BitmaskedComment::STATUS_ACTIVE, BitmaskedComment::STATUS_APPROVED], $res[0]->statuses);
  78. }
  79. /**
  80. * @return void
  81. */
  82. public function testFindBitmaskedContain() {
  83. $options = [
  84. 'bits' => [],
  85. 'type' => 'contain',
  86. ];
  87. $res = $this->Comments->find('bits', $options)->toArray();
  88. $this->assertCount(1, $res);
  89. $this->assertSame([], $res[0]->statuses);
  90. $options = [
  91. 'bits' => [BitmaskedComment::STATUS_APPROVED],
  92. 'type' => 'contain',
  93. ];
  94. $res = $this->Comments->find('bits', $options)->toArray();
  95. $this->assertCount(3, $res);
  96. $options = [
  97. 'bits' => [BitmaskedComment::STATUS_APPROVED, BitmaskedComment::STATUS_PUBLISHED],
  98. 'type' => 'contain',
  99. ];
  100. $res = $this->Comments->find('bits', $options)->toArray();
  101. $this->assertCount(5, $res);
  102. }
  103. /**
  104. * @return void
  105. */
  106. public function testSaveBasic() {
  107. $data = [
  108. 'comment' => 'test save',
  109. 'statuses' => [],
  110. ];
  111. $entity = $this->Comments->newEntity($data);
  112. $res = $this->Comments->save($entity);
  113. $this->assertTrue((bool)$res);
  114. $this->assertSame(0, $entity->get('status'));
  115. $data = [
  116. 'comment' => 'test save',
  117. 'statuses' => [BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED],
  118. ];
  119. $entity = $this->Comments->newEntity($data);
  120. $res = $this->Comments->save($entity);
  121. $this->assertTrue((bool)$res);
  122. $is = $entity->get('status');
  123. $this->assertSame(BitmaskedComment::STATUS_PUBLISHED | BitmaskedComment::STATUS_APPROVED, $is);
  124. // save + find
  125. $entity = $this->Comments->newEntity($data);
  126. $this->assertEmpty($entity->getErrors());
  127. $res = $this->Comments->save($entity);
  128. $this->assertTrue((bool)$res);
  129. $res = $this->Comments->find()->where(['statuses IN' => $data['statuses']])->first();
  130. $this->assertTrue(!empty($res));
  131. $expected = BitmaskedComment::STATUS_APPROVED | BitmaskedComment::STATUS_PUBLISHED; // 6
  132. $this->assertEquals($expected, $res['status']);
  133. $expected = $data['statuses'];
  134. $this->assertEquals($expected, $res['statuses']);
  135. // model.field syntax
  136. $res = $this->Comments->find()->where(['BitmaskedComments.statuses IN' => $data['statuses']])->first();
  137. $this->assertTrue((bool)$res);
  138. // explicit
  139. $activeApprovedAndPublished = BitmaskedComment::STATUS_ACTIVE | BitmaskedComment::STATUS_APPROVED | BitmaskedComment::STATUS_PUBLISHED;
  140. $data = [
  141. 'comment' => 'another post comment',
  142. 'status' => $activeApprovedAndPublished,
  143. ];
  144. $entity = $this->Comments->newEntity($data);
  145. $res = $this->Comments->save($entity);
  146. $this->assertTrue((bool)$res);
  147. $res = $this->Comments->find()->where(['status' => $activeApprovedAndPublished])->first();
  148. $this->assertTrue((bool)$res);
  149. $this->assertEquals($activeApprovedAndPublished, $res['status']);
  150. $expected = [BitmaskedComment::STATUS_ACTIVE, BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED];
  151. $this->assertEquals($expected, $res['statuses']);
  152. }
  153. /**
  154. * Assert that you can manually trigger "notEmpty" rule with null instead of 0 for "not null" db fields
  155. *
  156. * @return void
  157. */
  158. public function testSaveWithDefaultValue() {
  159. $data = [
  160. 'comment' => 'test save',
  161. 'statuses' => [],
  162. ];
  163. $entity = $this->Comments->newEntity($data);
  164. $res = $this->Comments->save($entity);
  165. $this->assertTrue((bool)$res);
  166. $this->assertSame(0, $entity->get('status'));
  167. $this->skipIf(true, '//FIXME');
  168. // Now let's set the default value
  169. $this->Comments->removeBehavior('Bitmasked');
  170. $this->Comments->addBehavior('Tools.Bitmasked', ['mappedField' => 'statuses', 'defaultValue' => '']);
  171. $data = [
  172. 'comment' => 'test save',
  173. 'statuses' => [],
  174. ];
  175. $entity = $this->Comments->newEntity($data);
  176. $res = $this->Comments->save($entity);
  177. $this->assertFalse($res);
  178. $this->assertSame('', $entity->get('status'));
  179. }
  180. /**
  181. * Assert that it also works with beforeSave event callback.
  182. *
  183. * @return void
  184. */
  185. public function testSaveOnBeforeSave() {
  186. $this->Comments->removeBehavior('Bitmasked');
  187. $this->Comments->addBehavior('Tools.Bitmasked', ['mappedField' => 'statuses', 'on' => 'beforeSave']);
  188. $data = [
  189. 'comment' => 'test save',
  190. 'statuses' => [BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED],
  191. ];
  192. $entity = $this->Comments->newEntity($data);
  193. $this->assertEmpty($entity->getErrors());
  194. $res = $this->Comments->save($entity);
  195. $this->assertTrue((bool)$res);
  196. $this->assertSame(BitmaskedComment::STATUS_PUBLISHED | BitmaskedComment::STATUS_APPROVED, $res['status']);
  197. }
  198. /**
  199. * @return void
  200. */
  201. public function testIs() {
  202. $res = $this->Comments->isBit(BitmaskedComment::STATUS_PUBLISHED);
  203. $expected = ['BitmaskedComments.status' => 2];
  204. $this->assertEquals($expected, $res);
  205. }
  206. /**
  207. * @return void
  208. */
  209. public function testIsNot() {
  210. $res = $this->Comments->isNotBit(BitmaskedComment::STATUS_PUBLISHED);
  211. $expected = ['NOT' => ['BitmaskedComments.status' => 2]];
  212. $this->assertEquals($expected, $res);
  213. }
  214. /**
  215. * @return void
  216. */
  217. public function testContains() {
  218. $config = $this->Comments->getConnection()->config();
  219. $isPostgres = strpos($config['driver'], 'Postgres') !== false;
  220. $res = $this->Comments->containsBit(BitmaskedComment::STATUS_PUBLISHED);
  221. $expected = ['(BitmaskedComments.status & 2 = 2)'];
  222. if ($isPostgres) {
  223. $expected = ['("BitmaskedComments"."status" & 2 = 2)'];
  224. }
  225. $this->assertEquals($expected, $res);
  226. $conditions = $res;
  227. $res = $this->Comments->find('all', ['conditions' => $conditions])->toArray();
  228. $this->assertTrue(!empty($res) && count($res) === 3);
  229. // multiple (AND)
  230. $res = $this->Comments->containsBit([BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_ACTIVE]);
  231. $expected = ['(BitmaskedComments.status & 3 = 3)'];
  232. if ($isPostgres) {
  233. $expected = ['("BitmaskedComments"."status" & 3 = 3)'];
  234. }
  235. $this->assertEquals($expected, $res);
  236. $conditions = $res;
  237. $res = $this->Comments->find('all', ['conditions' => $conditions])->toArray();
  238. $this->assertTrue(!empty($res) && count($res) === 2);
  239. }
  240. /**
  241. * @return void
  242. */
  243. public function testNotContains() {
  244. $config = $this->Comments->getConnection()->config();
  245. $isPostgres = strpos($config['driver'], 'Postgres') !== false;
  246. $res = $this->Comments->containsNotBit(BitmaskedComment::STATUS_PUBLISHED);
  247. $expected = ['(BitmaskedComments.status & 2 != 2)'];
  248. if ($isPostgres) {
  249. $expected = ['("BitmaskedComments"."status" & 2 != 2)'];
  250. }
  251. $this->assertEquals($expected, $res);
  252. $conditions = $res;
  253. $res = $this->Comments->find('all', ['conditions' => $conditions])->toArray();
  254. $this->assertTrue(!empty($res) && count($res) === 4);
  255. // multiple (AND)
  256. $res = $this->Comments->containsNotBit([BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_ACTIVE]);
  257. $expected = ['(BitmaskedComments.status & 3 != 3)'];
  258. if ($isPostgres) {
  259. $expected = ['("BitmaskedComments"."status" & 3 != 3)'];
  260. }
  261. $this->assertEquals($expected, $res);
  262. $conditions = $res;
  263. $res = $this->Comments->find('all', ['conditions' => $conditions])->toArray();
  264. $this->assertTrue(!empty($res) && count($res) === 5);
  265. }
  266. }