|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Sebastienheyd\Boilerplate\Tests\Datatables; |
| 4 | + |
| 5 | +use Sebastienheyd\Boilerplate\Datatables\Button; |
| 6 | +use Sebastienheyd\Boilerplate\Tests\TestCase; |
| 7 | + |
| 8 | +class ButtonTest extends TestCase |
| 9 | +{ |
| 10 | + public function testTooltipMethod() |
| 11 | + { |
| 12 | + $button = Button::add('Test Button') |
| 13 | + ->link('/test') |
| 14 | + ->tooltip('This is a tooltip') |
| 15 | + ->make(); |
| 16 | + |
| 17 | + $this->assertStringContainsString('title="This is a tooltip"', $button); |
| 18 | + } |
| 19 | + |
| 20 | + public function testTooltipNotAddedWhenEmpty() |
| 21 | + { |
| 22 | + $button = Button::add('Test Button') |
| 23 | + ->link('/test') |
| 24 | + ->make(); |
| 25 | + |
| 26 | + $this->assertStringNotContainsString('title=', $button); |
| 27 | + } |
| 28 | + |
| 29 | + public function testTooltipMethodChaining() |
| 30 | + { |
| 31 | + $button = Button::add('Test') |
| 32 | + ->link('/test') |
| 33 | + ->tooltip('Tooltip text') |
| 34 | + ->color('primary') |
| 35 | + ->icon('star') |
| 36 | + ->make(); |
| 37 | + |
| 38 | + $this->assertStringContainsString('title="Tooltip text"', $button); |
| 39 | + $this->assertStringContainsString('btn-primary', $button); |
| 40 | + $this->assertStringContainsString('fa-star', $button); |
| 41 | + } |
| 42 | + |
| 43 | + public function testButtonHtmlStructure() |
| 44 | + { |
| 45 | + $button = Button::add('Click me') |
| 46 | + ->link('/action') |
| 47 | + ->tooltip('Button tooltip') |
| 48 | + ->color('success') |
| 49 | + ->make(); |
| 50 | + |
| 51 | + $this->assertMatchesRegularExpression('/<a href="\/action" title="Button tooltip" class="btn btn-sm btn-success/', $button); |
| 52 | + } |
| 53 | + |
| 54 | + public function testCustomButtonWithRoute() |
| 55 | + { |
| 56 | + // Define a test route |
| 57 | + $this->app['router']->get('/test/{id}', function () { |
| 58 | + return 'test'; |
| 59 | + })->name('test.route'); |
| 60 | + |
| 61 | + $button = Button::custom( |
| 62 | + 'test.route', |
| 63 | + ['id' => 1], |
| 64 | + 'star', |
| 65 | + 'Custom tooltip', |
| 66 | + 'success' |
| 67 | + ); |
| 68 | + |
| 69 | + $this->assertStringContainsString('title="Custom tooltip"', $button); |
| 70 | + $this->assertStringContainsString('btn-success', $button); |
| 71 | + $this->assertStringContainsString('fa-star', $button); |
| 72 | + $this->assertStringContainsString('/test/1', $button); |
| 73 | + } |
| 74 | + |
| 75 | + public function testCustomButtonWithAttributes() |
| 76 | + { |
| 77 | + $this->app['router']->get('/test', function () { |
| 78 | + return 'test'; |
| 79 | + })->name('test.route2'); |
| 80 | + |
| 81 | + $button = Button::custom( |
| 82 | + 'test.route2', |
| 83 | + [], |
| 84 | + 'heart', |
| 85 | + 'Custom tooltip', |
| 86 | + 'primary', |
| 87 | + ['data-action' => 'custom-action', 'data-id' => '123'] |
| 88 | + ); |
| 89 | + |
| 90 | + $this->assertStringContainsString('data-action="custom-action"', $button); |
| 91 | + $this->assertStringContainsString('data-id="123"', $button); |
| 92 | + $this->assertStringContainsString('btn-primary', $button); |
| 93 | + $this->assertStringContainsString('fa-heart', $button); |
| 94 | + } |
| 95 | + |
| 96 | + public function testCustomButtonWithDefaults() |
| 97 | + { |
| 98 | + $this->app['router']->get('/default', function () { |
| 99 | + return 'test'; |
| 100 | + })->name('test.default'); |
| 101 | + |
| 102 | + $button = Button::custom('test.default'); |
| 103 | + |
| 104 | + $this->assertStringNotContainsString('title=', $button); |
| 105 | + $this->assertStringContainsString('btn-default', $button); |
| 106 | + $this->assertStringNotContainsString('fa-', $button); // No icon by default |
| 107 | + } |
| 108 | + |
| 109 | + public function testCustomButtonArgumentOrder() |
| 110 | + { |
| 111 | + // Test that $icon comes before $tooltip in the method signature |
| 112 | + $this->app['router']->get('/order', function () { |
| 113 | + return 'test'; |
| 114 | + })->name('test.order'); |
| 115 | + |
| 116 | + $button = Button::custom( |
| 117 | + 'test.order', |
| 118 | + [], |
| 119 | + 'cog', // $icon (3rd parameter) |
| 120 | + 'Tooltip', // $tooltip (4th parameter) |
| 121 | + 'warning' // $color (5th parameter) |
| 122 | + ); |
| 123 | + |
| 124 | + $this->assertStringContainsString('fa-cog', $button); |
| 125 | + $this->assertStringContainsString('title="Tooltip"', $button); |
| 126 | + $this->assertStringContainsString('btn-warning', $button); |
| 127 | + } |
| 128 | +} |
0 commit comments