File tree Expand file tree Collapse file tree 8 files changed +303
-0
lines changed Expand file tree Collapse file tree 8 files changed +303
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php namespace Tests \Support \Database \Migrations ;
2+
3+ use CodeIgniter \Database \Migration ;
4+
5+ class ExampleMigration extends Migration
6+ {
7+ protected $ DBGroup = 'tests ' ;
8+
9+ public function up ()
10+ {
11+ $ fields = [
12+ 'name ' => [
13+ 'type ' => 'varchar ' ,
14+ 'constraint ' => 31 ,
15+ ],
16+ 'uid ' => [
17+ 'type ' => 'varchar ' ,
18+ 'constraint ' => 31 ,
19+ ],
20+ 'class ' => [
21+ 'type ' => 'varchar ' ,
22+ 'constraint ' => 63 ,
23+ ],
24+ 'icon ' => [
25+ 'type ' => 'varchar ' ,
26+ 'constraint ' => 31 ,
27+ ],
28+ 'summary ' => [
29+ 'type ' => 'varchar ' ,
30+ 'constraint ' => 255 ,
31+ ],
32+ 'created_at ' => [
33+ 'type ' => 'datetime ' ,
34+ 'null ' => true ,
35+ ],
36+ 'updated_at ' => [
37+ 'type ' => 'datetime ' ,
38+ 'null ' => true ,
39+ ],
40+ 'deleted_at ' => [
41+ 'type ' => 'datetime ' ,
42+ 'null ' => true ,
43+ ],
44+ ];
45+
46+ $ this ->forge ->addField ('id ' );
47+ $ this ->forge ->addField ($ fields );
48+
49+ $ this ->forge ->addKey ('name ' );
50+ $ this ->forge ->addKey ('uid ' );
51+ $ this ->forge ->addKey (['deleted_at ' , 'id ' ]);
52+ $ this ->forge ->addKey ('created_at ' );
53+
54+ $ this ->forge ->createTable ('factories ' );
55+ }
56+
57+ public function down ()
58+ {
59+ $ this ->forge ->dropTable ('factories ' );
60+ }
61+ }
Original file line number Diff line number Diff line change 1+ <?php namespace Tests \Support \Database \Seeds ;
2+
3+ use CodeIgniter \Database \Seeder ;
4+
5+ class ExampleSeeder extends Seeder
6+ {
7+ public function run ()
8+ {
9+ $ factories = [
10+ [
11+ 'name ' => 'Test Factory ' ,
12+ 'uid ' => 'test001 ' ,
13+ 'class ' => 'Factories\Tests\NewFactory ' ,
14+ 'icon ' => 'fas fa-puzzle-piece ' ,
15+ 'summary ' => 'Longer sample text for testing ' ,
16+ ],
17+ [
18+ 'name ' => 'Widget Factory ' ,
19+ 'uid ' => 'widget ' ,
20+ 'class ' => 'Factories\Tests\WidgetPlant ' ,
21+ 'icon ' => 'fas fa-puzzle-piece ' ,
22+ 'summary ' => 'Create widgets in your factory ' ,
23+ ],
24+ [
25+ 'name ' => 'Evil Factory ' ,
26+ 'uid ' => 'evil-maker ' ,
27+ 'class ' => 'Factories\Evil\MyFactory ' ,
28+ 'icon ' => 'fas fa-book-dead ' ,
29+ 'summary ' => 'Abandon all hope, ye who enter here ' ,
30+ ],
31+ ];
32+
33+ $ builder = $ this ->db ->table ('factories ' );
34+
35+ foreach ($ factories as $ factory )
36+ {
37+ $ builder ->insert ($ factory );
38+ }
39+ }
40+ }
Original file line number Diff line number Diff line change 1+ <?php namespace Tests \Support ;
2+
3+ class DatabaseTestCase extends \CodeIgniter \Test \CIDatabaseTestCase
4+ {
5+ /**
6+ * Should the database be refreshed before each test?
7+ *
8+ * @var boolean
9+ */
10+ protected $ refresh = true ;
11+
12+ /**
13+ * The seed file(s) used for all tests within this test case.
14+ * Should be fully-namespaced or relative to $basePath
15+ *
16+ * @var string|array
17+ */
18+ protected $ seed = 'Tests\Support\Database\Seeds\ExampleSeeder ' ;
19+
20+ /**
21+ * The path to the seeds directory.
22+ * Allows overriding the default application directories.
23+ *
24+ * @var string
25+ */
26+ protected $ basePath = SUPPORTPATH . 'Database/ ' ;
27+
28+ /**
29+ * The namespace(s) to help us find the migration classes.
30+ * Empty is equivalent to running `spark migrate -all`.
31+ * Note that running "all" runs migrations in date order,
32+ * but specifying namespaces runs them in namespace order (then date)
33+ *
34+ * @var string|array|null
35+ */
36+ protected $ namespace = 'Tests\Support ' ;
37+
38+ public function setUp (): void
39+ {
40+ parent ::setUp ();
41+
42+ // Extra code to run before each test
43+ }
44+
45+ public function tearDown (): void
46+ {
47+ parent ::tearDown ();
48+
49+ // Extra code to run after each test
50+ }
51+ }
Original file line number Diff line number Diff line change 1+ <?php namespace Tests \Support \Models ;
2+
3+ use CodeIgniter \Model ;
4+
5+ class ExampleModel extends Model
6+ {
7+ protected $ table = 'factories ' ;
8+ protected $ primaryKey = 'id ' ;
9+
10+ protected $ returnType = 'object ' ;
11+ protected $ useSoftDeletes = false ;
12+
13+ protected $ allowedFields = [
14+ 'name ' ,
15+ 'uid ' ,
16+ 'class ' ,
17+ 'icon ' ,
18+ 'summary ' ,
19+ ];
20+
21+ protected $ useTimestamps = true ;
22+
23+ protected $ validationRules = [];
24+ protected $ validationMessages = [];
25+ protected $ skipValidation = false ;
26+ }
Original file line number Diff line number Diff line change 1+ <?php namespace Tests \Support ;
2+
3+ use CodeIgniter \Session \Handlers \ArrayHandler ;
4+ use CodeIgniter \Test \CIUnitTestCase ;
5+ use CodeIgniter \Test \Mock \MockSession ;
6+
7+ class SessionTestCase extends CIUnitTestCase
8+ {
9+ /**
10+ * @var SessionHandler
11+ */
12+ protected $ session ;
13+
14+ public function setUp (): void
15+ {
16+ parent ::setUp ();
17+
18+ $ this ->mockSession ();
19+ }
20+
21+ /**
22+ * Pre-loads the mock session driver into $this->session.
23+ *
24+ * @var string
25+ */
26+ protected function mockSession ()
27+ {
28+ $ config = config ('App ' );
29+ $ this ->session = new MockSession (new ArrayHandler ($ config , '0.0.0.0 ' ), $ config );
30+ \Config \Services::injectMock ('session ' , $ this ->session );
31+ }
32+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ use Tests \Support \Models \ExampleModel ;
4+
5+ class ExampleDatabaseTest extends \Tests \Support \DatabaseTestCase
6+ {
7+ public function setUp (): void
8+ {
9+ parent ::setUp ();
10+
11+ // Extra code to run before each test
12+ }
13+
14+ public function testModelFindAll ()
15+ {
16+ $ model = new ExampleModel ();
17+
18+ // Get every row created by ExampleSeeder
19+ $ objects = $ model ->findAll ();
20+
21+ // Make sure the count is as expected
22+ $ this ->assertCount (3 , $ objects );
23+ }
24+
25+ public function testSoftDeleteLeavesRow ()
26+ {
27+ $ model = new ExampleModel ();
28+ $ this ->setPrivateProperty ($ model , 'useSoftDeletes ' , true );
29+
30+ $ object = $ model ->first ();
31+ $ model ->delete ($ object ->id );
32+
33+ // The model should no longer find it
34+ $ this ->assertNull ($ model ->find ($ object ->id ));
35+
36+ // ... but it should still be in the database
37+ $ result = $ model ->builder ()->where ('id ' , $ object ->id )->get ()->getResult ();
38+
39+ $ this ->assertCount (1 , $ result );
40+ }
41+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ class ExampleSessionTest extends \Tests \Support \SessionTestCase
4+ {
5+ public function setUp (): void
6+ {
7+ parent ::setUp ();
8+ }
9+
10+ public function testSessionSimple ()
11+ {
12+ $ this ->session ->set ('logged_in ' , 123 );
13+
14+ $ value = $ this ->session ->get ('logged_in ' );
15+
16+ $ this ->assertEquals (123 , $ value );
17+ }
18+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ class ExampleTest extends \CodeIgniter \Test \CIUnitTestCase
4+ {
5+ public function setUp (): void
6+ {
7+ parent ::setUp ();
8+ }
9+
10+ public function testIsDefinedAppPath ()
11+ {
12+ $ test = defined ('APPPATH ' );
13+
14+ $ this ->assertTrue ($ test );
15+ }
16+
17+ public function testBaseUrlHasBeenSet ()
18+ {
19+ $ env = $ config = false ;
20+
21+ // First check in .env
22+ $ dotenv = new \CodeIgniter \Config \DotEnv (HOMEPATH );
23+
24+ if ($ dotenv ->load ())
25+ {
26+ // Check any line with "app.baseUrl" to see if it actually has a value set
27+ foreach (preg_grep ('/^app\.baseURL ' , file (HOMEPATH . '.env ' )) as $ line )
28+ {
29+ }
30+ }
31+
32+ $ this ->assertTrue ($ test );
33+ }
34+ }
You can’t perform that action at this time.
0 commit comments