Laravel 收藏不是';测试时不可排列
我正在做一些HTTP测试,其中一个测试有问题。发生的情况如下:Laravel 收藏不是';测试时不可排列,laravel,Laravel,我正在做一些HTTP测试,其中一个测试有问题。发生的情况如下: 相同端点 数据库中的相同数据 相同的PHP版本 相同的标题 不同的结果 代码如下: 一, 二, 以下是我从邮递员那里得到的信息: 一, 二, 以下是我在测试时得到的结果: 一, 二, TL;博士 如果您使用,请不要使用您的全局phpunit通过运行vendor/bin/phpunit使用项目的本地phpunit 详细版本 在研究这个奇怪的行为时,我得出结论,因为我知道类是illumb\Database\elount\Collec
- 相同端点
- 数据库中的相同数据
- 相同的PHP版本
- 相同的标题
- 不同的结果
vendor/bin/phpunit
使用项目的本地phpunit
详细版本
在研究这个奇怪的行为时,我得出结论,因为我知道类是illumb\Database\elount\Collection
,并且它扩展了实现Arrayable
的illumb\Support\Collection
,所以它不能仅仅是Arrayable
的一个实例
所以我决定用反射来检查它是什么类型的Arrayable
。这是我的密码:
$reflection = new \ReflectionClass(get_class($interestResults));
$implementedInterfacesList = array_keys($reflection->getInterfaces());
$isItArrayableByReflaction = in_array('Illuminate\Contracts\Support\Arrayable', $implementedInterfacesList);
$isItArrayableByInstanceOf = $interestResults instanceof \Illuminate\Contracts\Support\Arrayable;
dd($implementedInterfacesList, $isItArrayableByReflaction, $isItArrayableByInstanceOf);
令我惊讶的是,结果如下:
邮递员
array:8 [
0 => "JsonSerializable"
1 => "Illuminate\Contracts\Support\Jsonable"
2 => "Traversable"
3 => "IteratorAggregate"
4 => "Countable"
5 => "Illuminate\Contracts\Support\Arrayable"
6 => "ArrayAccess"
7 => "Illuminate\Contracts\Queue\QueueableCollection"
]
true
true
终端
array:8 [
0 => "JsonSerializable"
1 => "Tightenco\Collect\Contracts\Support\Jsonable"
2 => "Traversable"
3 => "IteratorAggregate"
4 => "Countable"
5 => "Tightenco\Collect\Contracts\Support\Arrayable"
6 => "ArrayAccess"
7 => "Illuminate\Contracts\Queue\QueueableCollection"
]
false
false
有趣的是,tighenco\Collect\Contracts\Support\Arrayable
,但作曲家为什么选择tighenco\Collect
composer why tightenco/collect
[InvalidArgumentException]
Could not find package "tightenco/collect" in your project
因此,错误不在我的项目上,也不在它的依赖项上。我已尝试运行本地phpunit(vendor/bin/phpunit
),并且:
太好了!问题“修复”,但作曲家为什么会这样
composer global why tightenco/collect
Changed current directory to /Users/guilherme/.composer
laravel/valet v2.0.10 requires tightenco/collect (^5.3)
就这样!运行全局phpunit composer时,将使用全局自动加载和
解决方案是什么?
是的,虽然我不认为它是一个解决方案,但我认为这是官方的一个:<强>使用项目的本地PHPUng/<强>运行<代码>供应商/BIN /PHPUng/<代码> < /P> 此外,您可以跳过需要
Arrayable
设置为illighted\Contracts\Support\Arrayable
的测试:
use Illuminate\Contracts\Support\Arrayable;
...
if (!collect() instanceof Arrayable) {
$skipMessage = <<<'MESSAGE'
This test has conflicts with tightenco/collect and it seems you are using it.
If you are running the testsuite with your global phpunit, please try running 'vendor/bin/phpunit'.
MESSAGE;
$this->markTestSkipped($skipMessage);
}
使用light\Contracts\Support\Arrayable;
...
如果(!collect()instanceof Arrayable){
$skipMessage=我会从phpunit和laravel存储库中打开一个问题交叉发布。@SparK是的,它看起来像是一个laravel testsuite错误。我不认为phpunit与此有关。我仍在调试它,如果我找到任何解决方案,我会通过在您的composer中注册一个名为test的脚本并调用phpunit,d来对laravel/Framework进行PR它是使用全局的还是本地的?我知道即使没有全局安装,它也可以找到本地的,但它是否将本地优先于全局的?对我来说没有意义的是使用Contracts\Support\Arrayable
而不指定项目名称,这会导致冲突。如果不是错误,那就是一种糟糕的做法。
$reflection = new \ReflectionClass(get_class($interestResults));
$implementedInterfacesList = array_keys($reflection->getInterfaces());
$isItArrayableByReflaction = in_array('Illuminate\Contracts\Support\Arrayable', $implementedInterfacesList);
$isItArrayableByInstanceOf = $interestResults instanceof \Illuminate\Contracts\Support\Arrayable;
dd($implementedInterfacesList, $isItArrayableByReflaction, $isItArrayableByInstanceOf);
array:8 [
0 => "JsonSerializable"
1 => "Illuminate\Contracts\Support\Jsonable"
2 => "Traversable"
3 => "IteratorAggregate"
4 => "Countable"
5 => "Illuminate\Contracts\Support\Arrayable"
6 => "ArrayAccess"
7 => "Illuminate\Contracts\Queue\QueueableCollection"
]
true
true
array:8 [
0 => "JsonSerializable"
1 => "Tightenco\Collect\Contracts\Support\Jsonable"
2 => "Traversable"
3 => "IteratorAggregate"
4 => "Countable"
5 => "Tightenco\Collect\Contracts\Support\Arrayable"
6 => "ArrayAccess"
7 => "Illuminate\Contracts\Queue\QueueableCollection"
]
false
false
composer why tightenco/collect
[InvalidArgumentException]
Could not find package "tightenco/collect" in your project
array:8 [
...
]
true
true
composer global why tightenco/collect
Changed current directory to /Users/guilherme/.composer
laravel/valet v2.0.10 requires tightenco/collect (^5.3)
use Illuminate\Contracts\Support\Arrayable;
...
if (!collect() instanceof Arrayable) {
$skipMessage = <<<'MESSAGE'
This test has conflicts with tightenco/collect and it seems you are using it.
If you are running the testsuite with your global phpunit, please try running 'vendor/bin/phpunit'.
MESSAGE;
$this->markTestSkipped($skipMessage);
}