| 
6 | 6 | use PHPUnit\Framework\Attributes\Group;  | 
7 | 7 | use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;  | 
8 | 8 | use Rappasoft\LaravelLivewireTables\Tests\Models\Pet;  | 
9 |  | -use Rappasoft\LaravelLivewireTables\Tests\TestCase;  | 
10 | 9 | use Rappasoft\LaravelLivewireTables\Tests\Unit\Attributes\AggregateColumnProvider;  | 
11 | 10 | use Rappasoft\LaravelLivewireTables\Views\Columns\AvgColumn;  | 
12 | 11 | 
 
  | 
13 | 12 | #[Group('Columns')]  | 
14 |  | -final class AvgColumnTest extends TestCase  | 
 | 13 | +final class AvgColumnTest extends ColumnTestCase  | 
15 | 14 | {  | 
16 | 15 |     protected function setUp(): void  | 
17 | 16 |     {  | 
18 | 17 |         parent::setUp();  | 
 | 18 | +        self::$columnInstance = AvgColumn::make('Name');  | 
 | 19 | + | 
19 | 20 |         parent::setupSpeciesTable();  | 
20 | 21 |     }  | 
21 | 22 | 
 
  | 
22 |  | -    public function test_can_set_the_column_title(): void  | 
23 |  | -    {  | 
24 |  | -        $column = AvgColumn::make('Average Age');  | 
25 |  | - | 
26 |  | -        $this->assertSame('Average Age', $column->getTitle());  | 
27 |  | -    }  | 
28 | 23 | 
 
  | 
29 | 24 |     #[DataProviderExternal(AggregateColumnProvider::class, 'relationshipProvider')]  | 
30 | 25 |     public function test_can_setup_column_correctly(string $relation_name, string $foreign_field): void  | 
31 | 26 |     {  | 
32 |  | -        $column = AvgColumn::make('Average Age')  | 
 | 27 | +        self::$columnInstance  | 
33 | 28 |             ->setDataSource($relation_name, $foreign_field)  | 
34 | 29 |             ->sortable();  | 
35 | 30 | 
 
  | 
36 |  | -        $this->assertNotEmpty($column);  | 
 | 31 | +        $this->assertNotEmpty(self::$columnInstance);  | 
37 | 32 |     }  | 
38 | 33 | 
 
  | 
39 | 34 |     #[DataProviderExternal(AggregateColumnProvider::class, 'relationshipProvider')]  | 
40 | 35 |     public function test_can_not_skip_set_data_source(string $relation_name, string $foreign_field): void  | 
41 | 36 |     {  | 
42 | 37 |         $this->expectException(DataTableConfigurationException::class);  | 
43 | 38 | 
 
  | 
44 |  | -        $column = AvgColumn::make('Average Age')  | 
 | 39 | +        self::$columnInstance  | 
45 | 40 |             ->sortable();  | 
46 |  | -        $contents = $column->getContents(Pet::find(1));  | 
47 |  | -        $this->assertNull($contents);  | 
 | 41 | +        $contents = self::$columnInstance->getContents(Pet::find(1));  | 
 | 42 | +        $this->assertNull(self::$columnInstance);  | 
48 | 43 | 
 
  | 
49 | 44 |     }  | 
50 | 45 | 
 
  | 
51 | 46 |     #[DataProviderExternal(AggregateColumnProvider::class, 'relationshipProvider')]  | 
52 | 47 |     public function test_can_get_data_source(string $relation_name, string $foreign_field): void  | 
53 | 48 |     {  | 
54 |  | -        $column = AvgColumn::make('Average Age')  | 
 | 49 | +        self::$columnInstance  | 
55 | 50 |             ->setDataSource($relation_name, $foreign_field)  | 
56 | 51 |             ->sortable();  | 
57 |  | -        $this->assertTrue($column->hasDataSource());  | 
58 |  | -        $this->assertSame($relation_name, $column->getDataSource());  | 
 | 52 | +        $this->assertTrue(self::$columnInstance->hasDataSource());  | 
 | 53 | +        $this->assertSame($relation_name, self::$columnInstance->getDataSource());  | 
59 | 54 |     }  | 
60 | 55 | 
 
  | 
61 | 56 |     #[DataProviderExternal(AggregateColumnProvider::class, 'relationshipProvider')]  | 
62 | 57 |     public function test_can_get_foreign_column(string $relation_name, string $foreign_field): void  | 
63 | 58 |     {  | 
64 |  | -        $column = AvgColumn::make('Average Age')  | 
 | 59 | +        self::$columnInstance  | 
65 | 60 |             ->setDataSource($relation_name, $foreign_field)  | 
66 | 61 |             ->sortable();  | 
67 |  | -        $this->assertTrue($column->hasForeignColumn());  | 
68 |  | -        $this->assertSame($foreign_field, $column->getForeignColumn());  | 
 | 62 | +        $this->assertTrue(self::$columnInstance->hasForeignColumn());  | 
 | 63 | +        $this->assertSame($foreign_field, self::$columnInstance->getForeignColumn());  | 
69 | 64 |     }  | 
70 | 65 | 
 
  | 
71 | 66 |     #[DataProviderExternal(AggregateColumnProvider::class, 'relationshipProvider')]  | 
72 | 67 |     public function test_can_set_foreign_column(string $relation_name, string $foreign_field): void  | 
73 | 68 |     {  | 
74 |  | -        $column = AvgColumn::make('Average Age')  | 
 | 69 | +        self::$columnInstance  | 
75 | 70 |             ->setDataSource($relation_name, $foreign_field)  | 
76 | 71 |             ->sortable();  | 
77 |  | -        $this->assertTrue($column->hasForeignColumn());  | 
78 |  | -        $this->assertSame($foreign_field, $column->getForeignColumn());  | 
79 |  | -        $column->setForeignColumn('test');  | 
80 |  | -        $this->assertTrue($column->hasForeignColumn());  | 
81 |  | -        $this->assertSame('test', $column->getForeignColumn());  | 
 | 72 | +        $this->assertTrue(self::$columnInstance->hasForeignColumn());  | 
 | 73 | +        $this->assertSame($foreign_field, self::$columnInstance->getForeignColumn());  | 
 | 74 | +        self::$columnInstance->setForeignColumn('test');  | 
 | 75 | +        $this->assertTrue(self::$columnInstance->hasForeignColumn());  | 
 | 76 | +        $this->assertSame('test', self::$columnInstance->getForeignColumn());  | 
82 | 77 | 
 
  | 
83 | 78 |     }  | 
84 | 79 | 
 
  | 
85 | 80 |     #[DataProviderExternal(AggregateColumnProvider::class, 'relationshipProvider')]  | 
86 | 81 |     public function test_can_get_data_source_fields(string $relation_name, string $foreign_field): void  | 
87 | 82 |     {  | 
88 |  | -        $column = AvgColumn::make('Average Age')  | 
 | 83 | +        self::$columnInstance  | 
89 | 84 |             ->setDataSource($relation_name, $foreign_field)  | 
90 | 85 |             ->sortable();  | 
91 |  | -        $this->assertTrue($column->hasDataSource());  | 
92 |  | -        $this->assertSame($relation_name, $column->getDataSource());  | 
93 |  | -        $this->assertTrue($column->hasForeignColumn());  | 
94 |  | -        $this->assertSame($foreign_field, $column->getForeignColumn());  | 
 | 86 | +        $this->assertTrue(self::$columnInstance->hasDataSource());  | 
 | 87 | +        $this->assertSame($relation_name, self::$columnInstance->getDataSource());  | 
 | 88 | +        $this->assertTrue(self::$columnInstance->hasForeignColumn());  | 
 | 89 | +        $this->assertSame($foreign_field, self::$columnInstance->getForeignColumn());  | 
95 | 90 |     }  | 
96 | 91 | 
 
  | 
97 | 92 |     #[DataProviderExternal(AggregateColumnProvider::class, 'relationshipProvider')]  | 
98 | 93 |     public function test_can_get_aggregate_method(string $relation_name, string $foreign_field): void  | 
99 | 94 |     {  | 
100 |  | -        $column = AvgColumn::make('Average Age')  | 
 | 95 | +        self::$columnInstance  | 
101 | 96 |             ->setDataSource($relation_name, $foreign_field)  | 
102 | 97 |             ->sortable();  | 
103 |  | -        $this->assertSame('avg', $column->getAggregateMethod());  | 
104 |  | -        $column->setAggregateMethod('test_avg');  | 
105 |  | -        $this->assertSame('test_avg', $column->getAggregateMethod());  | 
 | 98 | +        $this->assertSame('avg', self::$columnInstance->getAggregateMethod());  | 
 | 99 | +        self::$columnInstance->setAggregateMethod('test_avg');  | 
 | 100 | +        $this->assertSame('test_avg', self::$columnInstance->getAggregateMethod());  | 
106 | 101 |     }  | 
107 | 102 | 
 
  | 
108 | 103 |     #[DataProviderExternal(AggregateColumnProvider::class, 'relationshipProvider')]  | 
109 | 104 |     public function test_renders_correctly(string $relation_name, string $foreign_field): void  | 
110 | 105 |     {  | 
111 | 106 |         $rows = $this->speciesTable->getRows();  | 
112 |  | -        $column = AvgColumn::make('Average Age')  | 
 | 107 | +        self::$columnInstance  | 
113 | 108 |             ->setDataSource('pets', 'age');  | 
114 |  | -        $contents = $column->getContents($rows->first());  | 
 | 109 | +        $contents = self::$columnInstance->getContents($rows->first());  | 
115 | 110 |         $this->assertSame('15', $contents);  | 
116 |  | -        $contents = $column->getContents($rows[2]);  | 
 | 111 | +        $contents = self::$columnInstance->getContents($rows[2]);  | 
117 | 112 |         $this->assertSame('6', $contents);  | 
118 | 113 |     }  | 
119 | 114 | }  | 
0 commit comments