Skip to content

Commit a791344

Browse files
committed
add tests
1 parent 7fb9f8a commit a791344

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

tests/Unit/BumpCommandTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Artisan;
4+
use Farbcode\LaravelEvm\Contracts\RpcClient;
5+
use Farbcode\LaravelEvm\Contracts\Signer;
6+
use Farbcode\LaravelEvm\Contracts\FeePolicy;
7+
use Farbcode\LaravelEvm\Contracts\NonceManager;
8+
use Illuminate\Support\Facades\Event;
9+
10+
class BumpSigner implements Signer {
11+
public function getAddress(): string { return '0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef'; }
12+
public function privateKey(): string { return '0x'.str_repeat('11',32); }
13+
}
14+
class BumpRpc implements RpcClient {
15+
public array $sent = [];
16+
public function call(string $m, array $p = []): mixed {
17+
return match($m) {
18+
'eth_getTransactionCount' => '0x5', // latest and pending both 0x5 -> no pending
19+
'eth_gasPrice' => '0x3b9aca00',
20+
'eth_chainId' => '0x89', // 137
21+
'eth_sendRawTransaction' => ($this->sent[] = $p[0]) && '0xhash123',
22+
default => '0x1'
23+
};
24+
}
25+
public function callRaw(string $m, array $p = []): array { return ['result' => '0x1']; }
26+
public function health(): array { return ['chainId' => 137, 'block' => 1]; }
27+
public function getLogs(array $filter): array { return []; }
28+
}
29+
class BumpFees implements FeePolicy { public function suggest(callable $f): array { return [1_000_000_000, 50_000_000_000]; } public function replace(int $a,int $b): array { return [$a*2,$b*2]; } }
30+
class BumpNonce implements NonceManager { public function getPendingNonce(string $a, callable $b): int { return 5; } public function markUsed(string $a,int $n): void {} }
31+
32+
it('fails gracefully when no pending nonce', function() {
33+
app()->bind(Signer::class, fn() => new BumpSigner);
34+
app()->bind(RpcClient::class, fn() => new BumpRpc);
35+
app()->bind(FeePolicy::class, fn() => new BumpFees);
36+
app()->bind(NonceManager::class, fn() => new BumpNonce);
37+
$code = Artisan::call('evm:bump');
38+
expect($code)->toBe(1); // FAILURE
39+
});
40+
41+
it('broadcasts when explicit nonce provided', function() {
42+
app()->bind(Signer::class, fn() => new BumpSigner);
43+
app()->bind(RpcClient::class, fn() => new BumpRpc);
44+
app()->bind(FeePolicy::class, fn() => new BumpFees);
45+
app()->bind(NonceManager::class, fn() => new BumpNonce);
46+
$code = Artisan::call('evm:bump', ['--nonce' => 4, '--dry-run' => true]);
47+
expect($code)->toBe(0);
48+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
use Farbcode\LaravelEvm\Events\CallPerformed;
4+
use Farbcode\LaravelEvm\Contracts\RpcClient;
5+
use Farbcode\LaravelEvm\Contracts\Signer;
6+
use Farbcode\LaravelEvm\Contracts\AbiCodec;
7+
use Farbcode\LaravelEvm\Clients\ContractClientGeneric;
8+
use Illuminate\Support\Facades\Event;
9+
10+
class TestSigner implements Signer {
11+
public function getAddress(): string { return '0xdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef'; }
12+
public function privateKey(): string { return '0x'.str_repeat('aa',64); }
13+
}
14+
class TestRpc implements RpcClient {
15+
public function call(string $method, array $params = []): mixed {
16+
if ($method === 'eth_call') { return '0x0000000000000000000000000000000000000000000000000000000000000020'; }
17+
return '0x1';
18+
}
19+
public function callRaw(string $method, array $params = []): array { return ['result' => '0x1']; }
20+
public function health(): array { return ['chainId' => 137, 'block' => 1]; }
21+
public function getLogs(array $filter): array { return []; }
22+
}
23+
class TestAbi implements AbiCodec {
24+
public function encodeFunction(array|string $abi, string $fn, array $args): string { return '0xabcdef'; }
25+
public function callStatic(array|string $abi, string $fn, array $args, callable $ethCall): mixed { return $ethCall('0x'); }
26+
}
27+
28+
it('dispatches CallPerformed on read calls', function() {
29+
Event::fake([CallPerformed::class]);
30+
$client = new ContractClientGeneric(new TestRpc, new TestSigner, new TestAbi, 137, []);
31+
$client->at('0xcontract', []);
32+
$res = $client->call('foo', ['arg1']);
33+
expect($res)->toBeInstanceOf(\Farbcode\LaravelEvm\Support\CallResult::class);
34+
Event::assertDispatched(CallPerformed::class, fn(CallPerformed $e) => $e->function === 'foo' && $e->address === '0xcontract');
35+
});

tests/Unit/SendTransactionFailureTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public function health(): array
7575
{
7676
return ['chainId' => 137, 'block' => 123];
7777
}
78+
public function getLogs(array $filter): array { return []; }
7879
}
7980

8081
it('emits TxFailed on initial broadcast error', function () {

tests/Unit/SendTransactionJobTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public function health(): array
8585
{
8686
return ['chainId' => 137, 'block' => 123];
8787
}
88+
public function getLogs(array $filter): array { return []; }
8889
}
8990

9091
it('emits replacement events and eventually mines', function () {

0 commit comments

Comments
 (0)