Skip to content

Commit eb6ccb1

Browse files
authored
Merge pull request #1 from securenative/agent
implement dependencies discover and method replacement
2 parents 44cdc4a + 8506631 commit eb6ccb1

3 files changed

Lines changed: 76 additions & 2 deletions

File tree

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
"src/",
2121
"src/Enums",
2222
"src/Models"
23-
]
23+
],
24+
"files": ["vendor/antecedent/patchwork/Patchwork.php"]
2425
},
2526
"require": {
2627
"php": ">=7.2.0",
27-
"guzzlehttp/guzzle": "^6.0"
28+
"guzzlehttp/guzzle": "^6.0",
29+
"antecedent/patchwork": "~2.0"
2830
},
2931
"require-dev": {
3032
"phpunit/phpunit": "^8",

src/Agent.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
4+
namespace SecureNative\sdk;
5+
6+
use Patchwork;
7+
use Arrays;
8+
//use Patchwork\R;
9+
use function Patchwork\{redefine, relay, getMethod};
10+
//require '../vendor/antecedent/patchwork/Patchwork.php';
11+
12+
13+
class Agent
14+
{
15+
16+
public static function changeClassMethod($methodToReplace, $newMethod)
17+
{
18+
redefine($methodToReplace, $newMethod);
19+
}
20+
21+
public static function getDependencies($path){
22+
$strJsonFileContents = file_get_contents($path);
23+
$decoded = json_decode($strJsonFileContents, true);
24+
$dep = "";
25+
if(array_key_exists("require",$decoded)){
26+
foreach ($decoded["require"] as $key => $value)
27+
$dep = $dep."\n".$key."-".$value;
28+
}
29+
return $dep;
30+
}
31+
32+
}

tests/AgentTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
use SecureNative\sdk\Agent;
4+
use SecureNative\sdk\SecureNative;
5+
use PHPUnit\Framework\TestCase;
6+
use SecureNative\sdk\SecureNativeOptions;
7+
8+
final class AgentTest extends TestCase
9+
{
10+
const TEST_API_KEY = 'sample_key';
11+
/**
12+
* @before
13+
*/
14+
public static function initSDK()
15+
{
16+
SecureNative::init(TEST_API_KEY, new SecureNativeOptions());
17+
}
18+
19+
public function testApiKeyException()
20+
{
21+
$this->assertEquals($this->t1(),1 );
22+
Agent::changeClassMethod([$this, 't1'],[$this, 't2'] );
23+
$this->assertEquals($this->t1(),2 );
24+
}
25+
26+
public function testGetDependencies(){
27+
$dep = Agent::getDependencies("../composer.json");
28+
$this->assertTrue(strpos($dep, 'php')==1);
29+
}
30+
31+
public function t1(){
32+
return 1;
33+
}
34+
35+
public function t2(){
36+
return 2;
37+
}
38+
39+
40+
}

0 commit comments

Comments
 (0)