StringBehaviorTest.php 1.9 KB

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