Skip to content

Commit 49d1ba0

Browse files
committed
Merge remote-tracking branch 'upstream/develop' into 4.5
Conflicts: user_guide_src/source/testing/overview.rst
2 parents 207c664 + fc500d0 commit 49d1ba0

File tree

14 files changed

+133
-13
lines changed

14 files changed

+133
-13
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"phpunit/phpcov": "^8.2",
3434
"phpunit/phpunit": "^9.1",
3535
"predis/predis": "^1.1 || ^2.0",
36-
"rector/rector": "0.18.10",
36+
"rector/rector": "0.18.11",
3737
"vimeo/psalm": "^5.0"
3838
},
3939
"suggest": {

system/Cache/Handlers/FileHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ public function deleteMatching(string $pattern)
150150
*/
151151
public function increment(string $key, int $offset = 1)
152152
{
153-
$key = static::validateKey($key, $this->prefix);
154-
$tmp = $this->getItem($key);
153+
$prefixedKey = static::validateKey($key, $this->prefix);
154+
$tmp = $this->getItem($prefixedKey);
155155

156156
if ($tmp === false) {
157157
$tmp = ['data' => 0, 'ttl' => 60];

tests/system/Cache/Handlers/FileHandlerTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ protected function tearDown(): void
6767
chmod($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $key, 0777);
6868
unlink($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $key);
6969
}
70+
if (is_file($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $this->config->prefix . $key)) {
71+
chmod($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $this->config->prefix . $key, 0777);
72+
unlink($this->config->file['storePath'] . DIRECTORY_SEPARATOR . $this->config->prefix . $key);
73+
}
7074
}
7175

7276
rmdir($this->config->file['storePath']);
@@ -233,6 +237,22 @@ public function testIncrement(): void
233237
$this->assertSame(10, $this->handler->increment(self::$key3, 10));
234238
}
235239

240+
public function testIncrementWithDefaultPrefix(): void
241+
{
242+
$this->config->prefix = 'test_';
243+
$this->handler = new FileHandler($this->config);
244+
$this->handler->initialize();
245+
246+
$this->handler->save(self::$key1, 1);
247+
$this->handler->save(self::$key2, 'value');
248+
249+
$this->assertSame(11, $this->handler->increment(self::$key1, 10));
250+
$this->assertSame($this->handler->increment(self::$key1, 10), $this->handler->get(self::$key1));
251+
$this->assertFalse($this->handler->increment(self::$key2, 10));
252+
$this->assertSame(10, $this->handler->increment(self::$key3, 10));
253+
$this->assertSame($this->handler->increment(self::$key3, 10), $this->handler->get(self::$key3));
254+
}
255+
236256
public function testDecrement(): void
237257
{
238258
$this->handler->save(self::$key1, 10);
@@ -246,6 +266,21 @@ public function testDecrement(): void
246266
$this->assertSame(-1, $this->handler->decrement(self::$key3, 1));
247267
}
248268

269+
public function testDecrementWithDefaultPrefix(): void
270+
{
271+
$this->handler->save(self::$key1, 10);
272+
$this->handler->save(self::$key2, 'value');
273+
274+
// Line following commented out to force the cache to add a zero entry for key3
275+
// $this->fileHandler->save(self::$key3, 0);
276+
277+
$this->assertSame(9, $this->handler->decrement(self::$key1, 1));
278+
$this->assertSame($this->handler->decrement(self::$key1, 1), $this->handler->get(self::$key1));
279+
$this->assertFalse($this->handler->decrement(self::$key2, 1));
280+
$this->assertSame(-1, $this->handler->decrement(self::$key3, 1));
281+
$this->assertSame($this->handler->decrement(self::$key3, 1), $this->handler->get(self::$key3));
282+
}
283+
249284
public function testClean(): void
250285
{
251286
$this->handler->save(self::$key1, 1);

user_guide_src/source/incoming/restful.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ the controller that should be used:
5252

5353
.. literalinclude:: restful/003.php
5454

55+
.. literalinclude:: restful/017.php
56+
57+
.. literalinclude:: restful/018.php
58+
59+
See also :ref:`controllers-namespace`.
60+
5561
Change the Placeholder Used
5662
===========================
5763

@@ -122,6 +128,12 @@ the controller that should be used:
122128

123129
.. literalinclude:: restful/011.php
124130

131+
.. literalinclude:: restful/019.php
132+
133+
.. literalinclude:: restful/020.php
134+
135+
See also :ref:`controllers-namespace`.
136+
125137
Change the Placeholder Used
126138
===========================
127139

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
$routes->resource('photos', ['controller' => 'App\Gallery']);
4-
3+
$routes->resource('photos', ['controller' => 'Gallery']);
54
// Would create routes like:
6-
$routes->get('photos', 'App\Gallery::index');
5+
$routes->get('photos', '\App\Controllers\Gallery::index');
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
$routes->presenter('photos', ['controller' => 'App\Gallery']);
4-
3+
$routes->presenter('photos', ['controller' => 'Gallery']);
54
// Would create routes like:
6-
$routes->get('photos', 'App\Gallery::index');
5+
$routes->get('photos', '\App\Controllers\Gallery::index');
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
$routes->resource('photos', ['controller' => '\App\Gallery']);
4+
// Would create routes like:
5+
$routes->get('photos', '\App\Gallery::index');
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
use App\Controllers\Gallery;
4+
5+
$routes->resource('photos', ['namespace' => '', 'controller' => Gallery::class]);
6+
// Would create routes like:
7+
$routes->get('photos', '\App\Controllers\Gallery::index');
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
$routes->presenter('photos', ['controller' => '\App\Gallery']);
4+
// Would create routes like:
5+
$routes->get('photos', '\App\Gallery::index');
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
use App\Controllers\Gallery;
4+
5+
$routes->presenter('photos', ['namespace' => '', 'controller' => Gallery::class]);
6+
// Would create routes like:
7+
$routes->get('photos', '\App\Controllers\Gallery::index');

user_guide_src/source/incoming/routing.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ You can supply multiple verbs that a route should match by passing them in as an
8383
Specifying Route Handlers
8484
=========================
8585

86+
.. _controllers-namespace:
87+
8688
Controller's Namespace
8789
----------------------
8890

user_guide_src/source/models/entities.rst

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Using Entity Classes
33
#####################
44

5-
CodeIgniter supports Entity classes as a first-class citizen in it's database layer, while keeping
5+
CodeIgniter supports Entity classes as a first-class citizen, while keeping
66
them completely optional to use. They are commonly used as part of the Repository pattern, but can
77
be used directly with the :doc:`Model </models/model>` if that fits your needs better.
88

@@ -16,9 +16,17 @@ Entity Usage
1616

1717
At its core, an Entity class is simply a class that represents a single database row. It has class properties
1818
to represent the database columns, and provides any additional methods to implement the business logic for
19-
that row. The core feature, though, is that it doesn't know anything about how to persist itself. That's the
19+
that row.
20+
21+
.. note:: For ease of understanding, the explanation here is based on the case of
22+
using a database. However, Entity can also be used for data that does not come
23+
from a database.
24+
25+
The core feature, though, is that it doesn't know anything about how to persist itself. That's the
2026
responsibility of the model or the repository class. That way, if anything changes on how you need to save the
21-
object, you don't have to change how that object is used throughout the application. This makes it possible to
27+
object, you don't have to change how that object is used throughout the application.
28+
29+
This makes it possible to
2230
use JSON or XML files to store the objects during a rapid prototyping stage, and then easily switch to a
2331
database when you've proven the concept works.
2432

@@ -70,7 +78,7 @@ access them as if they were public properties. The base class, ``CodeIgniter\Ent
7078
well as providing the ability to check the properties with ``isset()``, or ``unset()`` the property, and keep track
7179
of what columns have changed since the object was created or pulled from the database.
7280

73-
.. note:: The Entity class stores the data in the property ``$attributes``.
81+
.. note:: The Entity class stores the data in the class property ``$attributes`` internally.
7482

7583
When the User is passed to the model's ``save()`` method, it automatically takes care of reading the properties
7684
and saving any changes to columns listed in the model's ``$allowedFields`` property. It also knows whether to create

user_guide_src/source/testing/overview.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,20 @@ component name:
266266
.. literalinclude:: overview/017.php
267267

268268
.. note:: All component Factories are reset by default between each test. Modify your test case's ``$setUpMethods`` if you need instances to persist.
269+
270+
Testing and Time
271+
================
272+
273+
Testing time-dependent code can be challenging. However, when using the
274+
:doc:`Time <../libraries/time>` class, the current time can be fixed or changed
275+
at will during testing.
276+
277+
Below is a sample test code that fixes the current time:
278+
279+
.. literalinclude:: overview/021.php
280+
281+
You can fix the current time with the ``Time::setTestNow()`` method.
282+
Optionally, you can specify a locale as the second parameter.
283+
284+
Don't forget to reset the current time after the test with calling it without
285+
parameters.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
use CodeIgniter\I18n\Time;
4+
use CodeIgniter\Test\CIUnitTestCase;
5+
6+
final class TimeDependentCodeTest extends CIUnitTestCase
7+
{
8+
protected function tearDown(): void
9+
{
10+
parent::tearDown();
11+
12+
// Reset the current time.
13+
Time::setTestNow();
14+
}
15+
16+
public function testFixTime(): void
17+
{
18+
// Fix the current time to "2023-11-25 12:00:00".
19+
Time::setTestNow('2023-11-25 12:00:00');
20+
21+
// This assertion always passes.
22+
$this->assertSame('2023-11-25 12:00:00', (string) Time::now());
23+
}
24+
}

0 commit comments

Comments
 (0)