小编Bol*_*lek的帖子

在特征中执行构造函数

我不想在特征(或使用特征时使用其他方法)中执行构造函数。可能吗?

trait test{
    public function __construct()
    {
        echo 'test';
    }
}

class myClass{
    use test;
    public function __construct(){
        echo 'myClass';
    }
}
new myClass();
Run Code Online (Sandbox Code Playgroud)

php traits

6
推荐指数
1
解决办法
1949
查看次数

Phpunit 只模拟测试类中的一种方法 - 使用 Mockery

我从一周开始学习 phpunit。我不知道如何只模拟测试类中的一种方法。(这只是示例,所以我没有写命名空间)。也许你可以帮助我

class SomeService
{
    public function firstMethod()
    {
        return 'smth';
    }
    public function secondMethd()
    {
        return $this->firstMethod() . ' plus some text example';
    }
}
Run Code Online (Sandbox Code Playgroud)

并测试:

class SomeServiceUnitTest extends TestCase
{
    private $someService;

    public function setUp()
    {
        parent::setUp();
        $this->someService = new SomeService();
    }

    public function tearDown()
    {
        $this->someService = null;
        parent::tearDown();
    }

    public function test_secondMethod()
    {
        $mock = Mockery::mock('App\Services\SomeService');
        $mock->shouldReceive('firstMethod')->andReturn('rerg');
        exit($this->walletService->secondMethd());
    }
}
Run Code Online (Sandbox Code Playgroud)

php phpunit unit-testing mockery

5
推荐指数
1
解决办法
6132
查看次数

开玩笑地嘲笑温斯顿记录器

我在想如何模拟运输。文件(来自温斯顿节点模块)。我正在使用 jest,所以__mocks__/winston.ts是自动加载的。我认为我不能嘲笑它,因为有new

// LoggerFactory.ts
import { transports, TransportInstance } from "winston";

...
    const transportList: TransportInstance[] = [
        new transports.File({
            name: `${tag}-error`,
            filename: `${dirname}${filename}.error.log`,
            json: false,
            level: "error",
            zippedArchive: false,
            maxFiles: 14,
            maxsize: 100000000
        }),
        new transports.File({
            name: `${tag}-info`,
            filename: `${dirname}${filename}.log`,
            json: false,
            level: "info",
            maxFiles: 10,
            zippedArchive: false,
            maxsize: 100000000
        })
    ];
...

// __mocks__/winston.ts
const winston = {
    ????
};
export default winston;
Run Code Online (Sandbox Code Playgroud)

错误:类型错误:无法读取未定义的属性“文件”

javascript mocking winston typescript jestjs

5
推荐指数
1
解决办法
5703
查看次数

访问自定义规则类中的另一个请求的输入-Laravel

我需要访问$request->important通过方法。我需要它来基于此值验证名称

class TestCustom implements Rule
{
    public function passes($attribute, $value)
    {
        // 
    }

    public function message()
    {
        return 'some txt';
    }
}
Run Code Online (Sandbox Code Playgroud)

像这样使用:

use App\Rules\TestCustom;

$request->validate([
    'name' => ['required', new TestCustom],
    'important' => ['required', 'string'],
]);
Run Code Online (Sandbox Code Playgroud)

validation laravel laravel-5

3
推荐指数
1
解决办法
965
查看次数

Eloquent json 字段 - 获取高于/低于某个值的记录

data我的表中有 json 类型的列。该查询不起作用:

Advert::where('data->price', '>', 2000)->get();
Run Code Online (Sandbox Code Playgroud)

作为回应,我得到了价格低于 2000 的元素。QueryBuilder 在数据库上运行它:

SELECT * FROM "adverts" WHERE "data"->>'price' > '2000'
Run Code Online (Sandbox Code Playgroud)

我不知道出了什么问题。看起来价格正在转换为字符串,但在数据库中保存为整数。

{"price": 500}
Run Code Online (Sandbox Code Playgroud)

postgresql laravel eloquent

2
推荐指数
1
解决办法
1121
查看次数

向Mockery添加接口以模拟(硬依赖性)

我需要通过重载来模拟CurrencyEnum,但这不是最终目的,因为我需要向该模拟中添加接口。这不起作用:

Mockery::mock('overload:'.CurrencyEnum::class);
Run Code Online (Sandbox Code Playgroud)

错误:(..) must be an instance of \BaseCurrency, instance of \CurrencyEnum given。我看着,但Mockery\Container::mock我不知道该怎么做。例如我要测试TestingClass::first()方法

class CurrencyEnum implements BaseCurrency
{
    /* methods */
}


class TestingClass
{
    public function first(string $currencySymbol)
    {
        $abc = 'some_string';

        return $this->second($abc, new CurrencyEnum($currencySymbol));
    }

    private function second(string $abc, BaseCurrency $currency)
    {
        /* code */
    }
}
Run Code Online (Sandbox Code Playgroud)

php phpunit unit-testing mockery

0
推荐指数
1
解决办法
251
查看次数