Skip to content

Commit c848cd3

Browse files
committed
#1 Model all().
Static model method used to retrieve all data models found in the database.
1 parent 705b1d7 commit c848cd3

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

src/Traits/DataModelTrait.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* @author Local Vibes <https://localvibes.co/>
1111
* @copyright Local Vibes
1212
* @package localvibes
13-
* @version 1.0.1
13+
* @version 1.0.7
1414
*/
1515
trait DataModelTrait
1616
{
@@ -136,4 +136,23 @@ public static function builder()
136136
$builder = new QueryBuilder( self::TABLE . '_custom' );
137137
return $builder->from( self::TABLE . ' as `' . self::TABLE . '`' );
138138
}
139+
/**
140+
* Returns a collection with all models found in the database.
141+
* @since 1.0.7
142+
*
143+
* @return array
144+
*/
145+
public static function all()
146+
{
147+
// Build query and retrieve
148+
$builder = new QueryBuilder( self::TABLE . '_all' );
149+
return array_map(
150+
function( $attributes ) {
151+
return new self( $attributes );
152+
},
153+
$builder->select( '*' )
154+
->from( self::TABLE . ' as `' . self::TABLE . '`' )
155+
->get( ARRAY_A )
156+
);
157+
}
139158
}

tests/cases/AbstractModelTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class AbstractModelTest extends TestCase
1717
* Test abstract
1818
* @since 1.0.0
1919
* @group abstract
20+
* @group model
2021
*/
2122
public function testTablenameProperty()
2223
{
@@ -30,6 +31,7 @@ public function testTablenameProperty()
3031
* Test abstract
3132
* @since 1.0.0
3233
* @group abstract
34+
* @group model
3335
*/
3436
public function testSave()
3537
{
@@ -49,6 +51,7 @@ public function testSave()
4951
* Test abstract
5052
* @since 1.0.1
5153
* @group abstract
54+
* @group model
5255
*/
5356
public function testSaveUpdate()
5457
{
@@ -69,6 +72,7 @@ public function testSaveUpdate()
6972
* Test abstract
7073
* @since 1.0.1
7174
* @group abstract
75+
* @group model
7276
*/
7377
public function testSaveForceInsert()
7478
{
@@ -88,6 +92,7 @@ public function testSaveForceInsert()
8892
* Test abstract
8993
* @since 1.0.0
9094
* @group abstract
95+
* @group model
9196
*/
9297
public function testDelete()
9398
{
@@ -106,6 +111,7 @@ public function testDelete()
106111
* Test abstract
107112
* @since 1.0.0
108113
* @group abstract
114+
* @group model
109115
*/
110116
public function testDeleteEmpty()
111117
{
@@ -120,6 +126,7 @@ public function testDeleteEmpty()
120126
* Test abstract
121127
* @since 1.0.0
122128
* @group abstract
129+
* @group model
123130
*/
124131
public function testLoad()
125132
{
@@ -142,6 +149,7 @@ public function testLoad()
142149
* Test abstract
143150
* @since 1.0.7
144151
* @group abstract
152+
* @group model
145153
*/
146154
public function testSaveTimestamps()
147155
{

tests/cases/SanitizationTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,7 @@ public function testSanitizeNegativeInt()
223223
// Preapre
224224
global $wpdb;
225225
$builder = QueryBuilder::create( 'test' );
226-
// Prepare
227-
// Prepare
226+
// Run
228227
$builder->select( '*' )
229228
->from( 'test_table' )
230229
->where( [

tests/cases/TraitModelTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,20 @@ public function testCount()
9797
$this->assertInternalType( 'int', $count );
9898
$this->assertEquals( 1, $count );
9999
}
100+
/**
101+
* Test abstract
102+
* @since 1.0.7
103+
* @group model
104+
* @group trait
105+
*/
106+
public function testAll()
107+
{
108+
// Preapre
109+
global $wpdb;
110+
// Exec
111+
$collection = Model::all();
112+
// Assert
113+
$this->assertInternalType( 'array', $collection );
114+
$this->assertInstanceOf( 'Model', $collection[0] );
115+
}
100116
}

0 commit comments

Comments
 (0)