Skip to content

docs: improve testing/overview.rst #8492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions user_guide_src/source/testing/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,31 @@ Testing Your Application
PHPUnit Configuration
=====================

The framework has a ``phpunit.xml.dist`` file in the project root. This controls unit
testing of the framework itself. If you provide your own ``phpunit.xml``, it will
over-ride this.
In your CodeIgniter project root, there is the ``phpunit.xml.dist`` file. This
controls unit testing of your application. If you provide your own ``phpunit.xml``,
it will over-ride this.

Your ``phpunit.xml`` should exclude the ``system`` folder, as well as any ``vendor`` or
``ThirdParty`` folders, if you are unit testing your application.
By default, test files are placed under the **tests** directory in the project root.

The Test Class
==============

In order to take advantage of the additional tools provided, your tests must extend ``CIUnitTestCase``. All tests
are expected to be located in the **tests/app** directory by default.
In order to take advantage of the additional tools provided, your tests must extend
``CodeIgniter\Test\CIUnitTestCase``.

To test a new library, **Foo**, you would create a new file at **tests/app/Libraries/FooTest.php**:
There are no rules for how test files must be placed. However, we recommend that
you establish placement rules in advance so that you can quickly understand where
the test files are located.

In this document, we will place the test files corresponding to the classes in
the **app** directory in the **tests/app** directory. To test a new library,
**app/Libraries/Foo.php**, you would create a new file at
**tests/app/Libraries/FooTest.php**:

.. literalinclude:: overview/001.php

To test one of your models, you might end up with something like this in **tests/app/Models/OneOfMyModelsTest.php**:
To test one of your models, **app/Models/UserMode.php**, you might end up with
something like this in **tests/app/Models/UserModelTest.php**:

.. literalinclude:: overview/002.php

Expand All @@ -104,7 +111,7 @@ to help with staging and clean up::
The static methods ``setUpBeforeClass()`` and ``tearDownAfterClass()`` run before and after the entire test case, whereas the protected methods ``setUp()`` and ``tearDown()`` run
between each test.

If you implement any of these special functions make sure you run their
If you implement any of these special functions, make sure you run their
parent as well so extended test cases do not interfere with staging:

.. literalinclude:: overview/003.php
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/testing/overview/002.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use CodeIgniter\Test\CIUnitTestCase;

class OneOfMyModelsTest extends CIUnitTestCase
class UserModelTest extends CIUnitTestCase
{
public function testFooNotBar()
{
Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/testing/overview/003.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use CodeIgniter\Test\CIUnitTestCase;

final class OneOfMyModelsTest extends CIUnitTestCase
final class UserModelTest extends CIUnitTestCase
{
protected function setUp(): void
{
Expand Down
7 changes: 5 additions & 2 deletions user_guide_src/source/testing/overview/016.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<?php

namespace Tests;

use CodeIgniter\HTTP\CURLRequest;
use CodeIgniter\Test\CIUnitTestCase;
use Config\Services;

final class SomeTest extends CIUnitTestCase
{
public function testSomething()
{
$curlrequest = $this->getMockBuilder('CodeIgniter\HTTP\CURLRequest')
->setMethods(['request'])
$curlrequest = $this->getMockBuilder(CURLRequest::class)
->onlyMethods(['request'])
->getMock();
Services::injectMock('curlrequest', $curlrequest);

Expand Down
3 changes: 2 additions & 1 deletion user_guide_src/source/testing/overview/017.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests;

use App\Models\UserModel;
use CodeIgniter\Config\Factories;
use CodeIgniter\Test\CIUnitTestCase;
use Tests\Support\Mock\MockUserModel;
Expand All @@ -13,6 +14,6 @@ protected function setUp(): void
parent::setUp();

$model = new MockUserModel();
Factories::injectMock('models', 'App\Models\UserModel', $model);
Factories::injectMock('models', UserModel::class, $model);
}
}
2 changes: 2 additions & 0 deletions user_guide_src/source/testing/overview/018.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Tests;

use CodeIgniter\CLI\CLI;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\StreamFilterTrait;
Expand Down
2 changes: 2 additions & 0 deletions user_guide_src/source/testing/overview/019.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Tests;

use CodeIgniter\CLI\CLI;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\PhpStreamWrapper;
Expand Down
2 changes: 2 additions & 0 deletions user_guide_src/source/testing/overview/020.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Tests;

use CodeIgniter\CLI\CLI;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Filters\CITestStreamFilter;
Expand Down