Skip to content
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

Bump to PHP8 #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
}
],
"require": {
"php": ">=7.1.0"
"php": ">=8.0"
},
"require-dev": {
"illuminate/database": ">=5.4",
"mockery/mockery" : "0.9.*|^1.0",
"phpunit/phpunit" : ">=7.0"
"phpunit/phpunit" : "^9.3|^10.0"
},
"autoload": {
"psr-4": {
Expand Down
13 changes: 7 additions & 6 deletions src/EloquentSuite.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ trait EloquentSuite
{
use MocksMixins;

protected static $eloquent_relations = [
protected static array $eloquent_relations = [
'hasOne' => Relations\HasOne::class,
'hasMany' => Relations\HasMany::class,
'morphTo' => Relations\MorphTo::class,
Expand Down Expand Up @@ -133,10 +133,11 @@ public function createRelationChainMock(string $model, string $relation, string
*
* @param string $relation
* @param \Illuminate\Database\Eloquent\Relations\Relation $actual
* @param string $message
* @param string $message
*
* @return void
*/
public static function assertRelation(string $relation, Relations\Relation $actual, $message = '')
public static function assertRelation(string $relation, Relations\Relation $actual, string $message = ''): void
{
if (array_key_exists($relation, self::$eloquent_relations)) {
$relation = self::$eloquent_relations[$relation];
Expand All @@ -150,9 +151,9 @@ public static function assertRelation(string $relation, Relations\Relation $actu
$relation,
$actual,
$message ?: 'Possible reasons:' .
' relation not defined on the model,' .
' unexpected query methods chained on relation object,' .
' missing return statement.'
' relation not defined on the model,' .
' unexpected query methods chained on relation object,' .
' missing return statement.'
);
}

Expand Down
28 changes: 19 additions & 9 deletions src/MocksMixins.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Mockery\MockInterface;
use PHPUnit\Framework\Constraint\Callback;
use PHPUnit\Framework\MockObject\Generator;
use PHPUnit\Framework\MockObject\MockBuilder;
use PHPUnit\Framework\MockObject\MockObject;
Expand Down Expand Up @@ -42,15 +44,22 @@ protected function mockEloquentBuilder(): MockObject
*/
protected function createMixinMock(string $mocked_class, string ...$mixins): MockObject
{
return $this->getMockBuilder($mocked_class)
$mockBuilder = $this->getMockBuilder($mocked_class)
->disableOriginalConstructor()
->disableOriginalClone()
->disableArgumentCloning()
->allowMockingUnknownTypes()
->setMethods($this->getMixinMockableMethods(array_merge([$mocked_class], $mixins)))
->allowMockingUnknownTypes();

$mixinMockableMethods = $this->getMixinMockableMethods(array_merge([$mocked_class], $mixins));
$onlyExistMethods = array_intersect(get_class_methods($mocked_class), $mixinMockableMethods);
$otherMethods = array_diff($mixinMockableMethods, $onlyExistMethods);

return $mockBuilder->onlyMethods($onlyExistMethods)
->addMethods($otherMethods)
->getMock();
}


protected function getMixinMockableMethods($classnames): array
{
$methods = array_map(function ($classname) {
Expand All @@ -66,7 +75,7 @@ protected function getMixinMockableMethods($classnames): array
}, $classnames);

return array_filter(array_unique(array_merge(...$methods)), function ($method) {
return substr($method, 0, 2) !== '__';
return !str_starts_with($method, '__');
});
}

Expand Down Expand Up @@ -99,10 +108,11 @@ protected function getMixinMockableMethods($classnames): array
* // act
* $some_class->something($query);
*
* @param \PHPUnit\Framework\MockObject\MockObject|\Mockery\MockInterface $query
* @return \PHPUnit\Framework\Constraint\Callback|\Mockery\Matcher\Closure
* @param Closure $expectations
*
* @return Callback|\Mockery\Matcher\Closure
*/
protected function assertQueryCallback(Closure $expectations)
protected function assertQueryCallback(Closure $expectations): \Mockery\Matcher\Closure|Callback
{
$query = $this->buildExpectedQueryMock($expectations);
call_user_func($expectations, $query);
Expand All @@ -122,9 +132,9 @@ protected function assertQueryCallback(Closure $expectations)

/**
* @param Closure $expectations
* @return \Mockery\MockInterface|MockObject
* @return MockInterface|MockObject
*/
protected function buildExpectedQueryMock(Closure $expectations)
protected function buildExpectedQueryMock(Closure $expectations): MockInterface|MockObject
{
$ref = new ReflectionFunction($expectations);

Expand Down