Skip to content

Commit bacb51f

Browse files
committed
Connection Coverage
1 parent 8321ced commit bacb51f

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
3+
namespace Attestto\SolanaPhpSdk\Tests\Unit;
4+
5+
use Attestto\SolanaPhpSdk\Connection;
6+
use Attestto\SolanaPhpSdk\Exceptions\AccountNotFoundException;
7+
use Attestto\SolanaPhpSdk\Exceptions\GenericException;
8+
use Attestto\SolanaPhpSdk\Tests\TestCase;
9+
use Attestto\SolanaPhpSdk\SolanaRpcClient;
10+
use Attestto\SolanaPhpSdk\Transaction;
11+
use Attestto\SolanaPhpSdk\KeyPair;
12+
use Attestto\SolanaPhpSdk\Programs\SystemProgram;
13+
use PHPUnit\Framework\MockObject\Exception;
14+
use SodiumException;
15+
16+
17+
class ConnectionFeatureTest extends TestCase
18+
{
19+
/**
20+
* @throws AccountNotFoundException|Exception
21+
*/
22+
public function testGetAccountInfo()
23+
{
24+
$pubKey = '3Wnd5Df69KitZfUoPYZU438eFRNwGHkhLnSAWL65PxJX';
25+
$accountInfo = ['anything'];
26+
27+
$clientMock = $this->createMock(SolanaRpcClient::class);
28+
$clientMock->expects($this->once())
29+
->method('call')
30+
->with('getAccountInfo', [$pubKey, ["encoding" => "jsonParsed"]])
31+
->willReturn(['value' => $accountInfo]);
32+
33+
$connection = new Connection($clientMock);
34+
35+
$result = $connection->getAccountInfo($pubKey);
36+
$this->assertEquals($accountInfo, $result);
37+
}
38+
39+
/**
40+
* @throws Exception
41+
*/
42+
public function testGetAccountInfoThrowsException()
43+
{
44+
$pubKey = 'your_public_key_here';
45+
46+
$clientMock = $this->createMock(SolanaRpcClient::class);
47+
$clientMock->expects($this->once())
48+
->method('call')
49+
->with('getAccountInfo', [$pubKey, ["encoding" => "jsonParsed"]])
50+
->willReturn(['value' => null]);
51+
52+
$connection = new Connection($clientMock);
53+
54+
$this->expectException(AccountNotFoundException::class);
55+
$this->expectExceptionMessage("API Error: Account $pubKey not found.");
56+
$connection->getAccountInfo($pubKey);
57+
58+
}
59+
60+
/**
61+
* @throws GenericException
62+
* @throws SodiumException
63+
*/
64+
public function testSimulateTransaction()
65+
{
66+
$account1 = Keypair::generate();
67+
$account2 = Keypair::generate();
68+
$recentBlockhash = $account1->getPublicKey()->toBase58(); // Fake recentBlockhash
69+
70+
$transfer1 = SystemProgram::transfer($account1->getPublicKey(), $account2->getPublicKey(), 123);
71+
$transfer2 = SystemProgram::transfer($account2->getPublicKey(), $account1->getPublicKey(), 123);
72+
73+
$orgTransaction = new Transaction($recentBlockhash);
74+
$orgTransaction->add($transfer1, $transfer2);
75+
$orgTransaction->sign($account1, $account2);
76+
77+
$newTransaction = new Transaction($orgTransaction->recentBlockhash, null, null, $orgTransaction->signatures);
78+
$newTransaction->add($transfer1, $transfer2);
79+
80+
// TODO - Fix this test, call the method and compare the transactions
81+
82+
$this->assertEquals($orgTransaction, $newTransaction);
83+
84+
}
85+
86+
#[Test]
87+
public function testGetBalance()
88+
{
89+
$pubKey = '3Wnd5Df69KitZfUoPYZU438eFRNwGHkhLnSAWL65PxJX';
90+
$balance = 100;
91+
92+
$clientMock = $this->createMock(SolanaRpcClient::class);
93+
$clientMock->expects($this->once())
94+
->method('call')
95+
->with('getBalance', [$pubKey])
96+
->willReturn(['value' => $balance]);
97+
98+
$connection = new Connection($clientMock);
99+
100+
$result = $connection->getBalance($pubKey);
101+
$this->assertEquals($balance, $result);
102+
}
103+
104+
105+
106+
#[Test]
107+
public function testGetRecentBlockhash()
108+
{
109+
$commitment = 'confirmed';
110+
$blockhash = ['anything'];
111+
}
112+
113+
#[Test]
114+
public function testGetLatestBlockhash()
115+
{
116+
$commitment = 'confirmed';
117+
$blockhash = ['anything'];
118+
}
119+
120+
#[Test]
121+
public function testRequestAirDrop()
122+
{
123+
$pubKey = '3Wnd5Df69KitZfUoPYZU438eFRNwGHkhLnSAWL65PxJX';
124+
$lamports = 100;
125+
$transaction = ['anything'];
126+
127+
$clientMock = $this->createMock(SolanaRpcClient::class);
128+
$clientMock->expects($this->once())
129+
->method('call')
130+
->with('requestAirdrop', [$pubKey, $lamports])
131+
->willReturn(['value' => $transaction]);
132+
133+
$connection = new Connection($clientMock);
134+
135+
$result = $connection->requestAirdrop($pubKey, $lamports);
136+
$this->assertEquals($transaction, $result);
137+
}
138+
139+
140+
141+
}

tests/Unit/Readme.md

Whitespace-only changes.

0 commit comments

Comments
 (0)