StringBehaviorTest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace Tools\Test\TestCase\Model\Behavior;
  3. use Cake\ORM\TableRegistry;
  4. use Tools\TestSuite\TestCase;
  5. class StringBehaviorTest extends TestCase {
  6. /**
  7. * @var array
  8. */
  9. public $fixtures = [
  10. 'plugin.tools.string_comments'
  11. ];
  12. /**
  13. * @var \Tools\Model\Table\Table
  14. */
  15. public $Comments;
  16. /**
  17. * @return void
  18. */
  19. public function setUp() {
  20. parent::setUp();
  21. $this->Comments = TableRegistry::get('StringComments');
  22. $this->Comments->addBehavior('Tools.String', ['fields' => ['title'], 'input' => ['ucfirst']]);
  23. }
  24. /**
  25. * @return void
  26. */
  27. public function testBasic() {
  28. $data = [
  29. 'title' => 'some Name',
  30. 'comment' => 'blabla',
  31. 'url' => 'www.dereuromark.de',
  32. ];
  33. $entity = $this->Comments->newEntity($data);
  34. $res = $this->Comments->save($entity);
  35. $this->assertTrue((bool)$res);
  36. $this->assertSame('Some Name', $res['title']);
  37. }
  38. /**
  39. * @return void
  40. */
  41. public function testMultipleFieldsAndMultipleFilters() {
  42. $this->Comments->behaviors()->String->config(['fields' => ['title', 'comment'], 'input' => ['strtolower', 'ucwords']]);
  43. $data = [
  44. 'title' => 'some nAme',
  45. 'comment' => 'blaBla',
  46. 'url' => 'www.dereuromark.de',
  47. ];
  48. $entity = $this->Comments->newEntity($data);
  49. $res = $this->Comments->save($entity);
  50. $this->assertTrue((bool)$res);
  51. $this->assertSame('Some Name', $res['title']);
  52. $this->assertSame('Blabla', $res['comment']);
  53. }
  54. /**
  55. * @return void
  56. */
  57. public function testBasicOutput() {
  58. $this->Comments->removeBehavior('String');
  59. $data = [
  60. 'title' => 'some Name',
  61. 'comment' => 'blabla',
  62. 'url' => ''
  63. ];
  64. $entity = $this->Comments->newEntity($data);
  65. $res = $this->Comments->save($entity);
  66. $this->assertTrue((bool)$res);
  67. $this->Comments->addBehavior('Tools.String', ['fields' => ['title'], 'output' => ['ucfirst']]);
  68. $res = $this->Comments->get($entity->id);
  69. $this->assertSame('Some Name', $res['title']);
  70. }
  71. }