StringBehaviorTest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. public function setUp() {
  17. parent::setUp();
  18. $this->Comments = TableRegistry::get('StringComments');
  19. $this->Comments->addBehavior('Tools.String', ['fields' => ['title'], 'input' => ['ucfirst']]);
  20. }
  21. /**
  22. * @return void
  23. */
  24. public function testBasic() {
  25. $data = [
  26. 'title' => 'some Name',
  27. 'comment' => 'blabla',
  28. 'url' => 'www.dereuromark.de',
  29. ];
  30. $entity = $this->Comments->newEntity($data);
  31. $res = $this->Comments->save($entity);
  32. $this->assertTrue((bool)$res);
  33. $this->assertSame('Some Name', $res['title']);
  34. }
  35. /**
  36. * @return void
  37. */
  38. public function testMultipleFieldsAndMultipleFilters() {
  39. $this->Comments->behaviors()->String->config(['fields' => ['title', 'comment'], 'input' => ['strtolower', 'ucwords']]);
  40. $data = [
  41. 'title' => 'some nAme',
  42. 'comment' => 'blaBla',
  43. 'url' => 'www.dereuromark.de',
  44. ];
  45. $entity = $this->Comments->newEntity($data);
  46. $res = $this->Comments->save($entity);
  47. $this->assertTrue((bool)$res);
  48. $this->assertSame('Some Name', $res['title']);
  49. $this->assertSame('Blabla', $res['comment']);
  50. }
  51. /**
  52. * @return void
  53. */
  54. public function testBasicOutput() {
  55. $this->Comments->removeBehavior('String');
  56. $data = [
  57. 'title' => 'some Name',
  58. 'comment' => 'blabla',
  59. 'url' => ''
  60. ];
  61. $entity = $this->Comments->newEntity($data);
  62. $res = $this->Comments->save($entity);
  63. $this->assertTrue((bool)$res);
  64. $this->Comments->addBehavior('Tools.String', ['fields' => ['title'], 'output' => ['ucfirst']]);
  65. $res = $this->Comments->get($entity->id);
  66. $this->assertSame('Some Name', $res['title']);
  67. }
  68. }