Skip to content

Commit cf8f7b1

Browse files
authored
Merge pull request #3 from neighbourhoodie/ci-tests
Add CI tests
2 parents b6b2e65 + ec8865f commit cf8f7b1

File tree

3 files changed

+192
-190
lines changed

3 files changed

+192
-190
lines changed

.github/workflows/ci.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: phpseclib3 Tests
2+
3+
on: [push]
4+
5+
jobs:
6+
tests:
7+
name: Rector Tests
8+
runs-on: ubuntu-latest
9+
10+
strategy:
11+
fail-fast: false
12+
matrix:
13+
php-version: ['8.3', '8.4', '8.5']
14+
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v4
18+
19+
- name: Setup PHP
20+
uses: shivammathur/setup-php@v2
21+
with:
22+
php-version: ${{ matrix.php-version }}
23+
coverage: none
24+
25+
- name: Install dependencies
26+
run: composer install --no-interaction --prefer-dist
27+
28+
- name: Run Rector tests
29+
run: vendor/bin/phpunit tests

README.md

Lines changed: 163 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,167 @@ vendor/bin/rector process src
3434
```
3535
The files in the `src/` directory will either be full on modified or (in the case of `--dry-run`) the changes that would be made will be previewed.
3636

37-
## Rule specifics
37+
## Running the tests
3838

39-
Details on specific rules can be found [here](./src/README.MD).
39+
To run all Retor tests, run
40+
41+
```
42+
vendor/bin/phpunit tests
43+
```
44+
45+
To run all tests of a single rector rule, add --filter to the test command.
46+
47+
```
48+
vendor/bin/phpunit tests --filter CustomRectorTest
49+
```
50+
51+
### Test Fixtures
52+
53+
Next to the test case, there is `/Fixture` directory. It contains many test fixture files that verified the Rector rule work correctly in all possible cases.
54+
55+
There are 2 fixture formats:
56+
57+
A. `test_fixture.php.inc` - The Code Should Change
58+
59+
```php
60+
<code before>
61+
-----
62+
<code after>'
63+
```
64+
65+
B. `skip_rule_test_fixture.php.inc` - The Code Should Be Skipped
66+
67+
```php
68+
<code before>
69+
```
70+
71+
## Rules
72+
73+
### Public Key Loader
74+
75+
This rule is for `v2` -> `v3` upgrade.
76+
77+
This rule helps to load unencrypted and encrypted public / private keys.
78+
79+
In `v2` `loadKey()` returns true on success and false on failure. `v2` only supports RSA keys and `$rsa` is *not* immutable.
80+
And in `v3` `PublicKeyLoader` returns an immutable instance of either `\phpseclib3\Crypt\Common\PublicKey` or
81+
`\phpseclib3\Crypt\Common\PrivateKey`. An exception is thrown on failure.
82+
83+
It replaces
84+
```php
85+
use phpseclib\Crypt\RSA;
86+
87+
$rsa = new RSA();
88+
$rsa->loadKey('...');
89+
```
90+
with
91+
92+
```php
93+
use phpseclib3\Crypt\PublicKeyLoader;
94+
95+
$rsa = PublicKeyLoader::load('...');
96+
```
97+
98+
When `setPassword` is used, `$rsa->setPassword('password');` will be replaced with `$rsa = PublicKeyLoader::load('...', $password);`.
99+
100+
Additionally it replaces the following methods:
101+
102+
| v2 | v3 |
103+
|-------------------------------|---------------------------------------|
104+
| $rsa->getSize() | $rsa->getLength() |
105+
| $rsa->setHash('sha256'); | $rsa = $rsa->withHash('sha256') |
106+
| $rsa->setMGFHash('sha256'); | $rsa = $rsa->withMGFHash('sha256') |
107+
| $rsa->setSaltLength(10); | $rsa->withSaltLength(10) |
108+
| $rsa->setSignatureMode(RSA::SIGNATURE_PKCS1); | $rsa->withPadding(RSA::SIGNATURE_PKCS1); |
109+
| $rsa->setEncryptionMode(RSA::ENCRYPTION_PKCS1); | $rsa->withPadding(RSA::ENCYRPTION_PKCS1); |
110+
| $rsa->setEncryptionMode(RSA::ENCRYPTION_PKCS1); $rsa->setSignatureMode(RSA::SIGNATURE_PKCS1); | $rsa->withPadding(RSA::SIGNATURE_PKCS1 | RSA::ENCYRPTION_PKCS1);|
111+
112+
### Public Key Loader Chained
113+
114+
Does the same things as `PublicKeyLoader`, but chains the methods.
115+
116+
The chaining option writes everything into _one_ line.
117+
You can format them by running:
118+
119+
```sh
120+
vendor/bin/php-cs-fixer fix path/to/file.php --rules=binary_operator_spaces,method_chaining_indentation
121+
```
122+
123+
### Create Key
124+
125+
This rule is for `v2` -> `v3` upgrade.
126+
127+
It replaces
128+
```php
129+
use phpseclib\Crypt\RSA;
130+
131+
$rsa = new RSA();
132+
extract($rsa->createKey(2048));
133+
```
134+
135+
with
136+
137+
```php
138+
use phpseclib3\Crypt\RSA;
139+
140+
$privateKey = RSA::createKey(2048);
141+
$publicKey = $privateKey->getPublicKey();
142+
$privateKey = (string) $privateKey;
143+
$publicKey = (string) $publicKey;
144+
```
145+
146+
In `v2`, `$rsa->createKey()` returns an array with 3x parameters: `privatekey`, `publickey`, `partial`.
147+
`privatekey` and `public key` are strings, partial can be ignored.
148+
149+
The above `v3` example returns an immutable instance of `phpseclib3\Crypt\Common\PrivateKey`.
150+
151+
The public key can be extracted by: `$rsa->getPublicKey()`.
152+
153+
### HashLength
154+
155+
This rule is for `v2` -> `v3` upgrade.
156+
157+
In phpseclib `v2` getLength would sometimes return the length in bits and sometimes it'd return the length in bytes
158+
in phpseclib `v3` it was made consistent -
159+
getLength always returns the length in bits and getLengthInBytes always returns the length in bytes.
160+
161+
It replaces
162+
```php
163+
use phpseclib\Crypt\Hash;
164+
165+
$hash = new Hash('sha512/224');
166+
echo $hash->getLength();
167+
```
168+
with
169+
```php
170+
use phpseclib3\Crypt\Hash;
171+
172+
$hash = new Hash('sha512/224');
173+
echo $hash->getLengthInBytes();
174+
```
175+
176+
### SFTP File Size
177+
178+
This rule is for `v2` -> `v3` upgrade.
179+
180+
In phpseclib `v2` you have filemtime, fileatime, fileowner but, instead of filesize, you just have size?
181+
phpseclib `v3` fixes that.
182+
183+
It replaces
184+
```php
185+
use phpseclib\Net\SFTP;
186+
187+
$sftp = new SFTP('...');
188+
$sftp->login('username', 'password');
189+
echo $sftp->size('/path/to/filename.ext');
190+
```
191+
192+
with
193+
194+
```php
195+
use phpseclib3\Net\SFTP;
196+
197+
$sftp = new SFTP('...');
198+
$sftp->login('username', 'password');
199+
echo $sftp->filesize('/path/to/filename.ext');
200+
```

src/README.MD

Lines changed: 0 additions & 188 deletions
This file was deleted.

0 commit comments

Comments
 (0)