PHP code example of steffenbrand / non-static-php-jwt

1. Go to this page and download the library: Download steffenbrand/non-static-php-jwt library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

steffenbrand / non-static-php-jwt example snippets


$jwt = new \SteffenBrand\NonStaticPhpJwt\Jwt();

$key = 'example_key';
$token = [
    'iss' => 'http://example.org',
    'aud' => 'http://example.com',
    'iat' => 1356999524,
    'nbf' => 1357000000
];

$webToken = $jwt->encode($token, $key);
$decoded = $jwt->decode($webToken, $key, ['HS256']);

var_dump($decoded);
print_r((array) $decoded);

$jwt->decode($jwt, $key, ['HS256'], $leeway = 60);



declare(strict_types=1);

namespace SteffenBrand\NonStaticPhpJwt\Test;

use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\MethodProphecy;
use Prophecy\Prophecy\ObjectProphecy;
use SteffenBrand\NonStaticPhpJwt\Jwt;

class JwtTest extends TestCase
{
    /**
     * @var Jwt
     */
    private $jwt;

    protected function setUp()
    {
        parent::setUp();
        $this->jwt = $this->prophesize(Jwt::class);
    }

    public function getSut(): Dummy
    {
        return new Dummy($this->jwt->reveal());
    }

    public function testJwtEncodeCanBeProphecised(): void
    {
        $returnValue = 'string';
        $this->jwt->encode([], '')->shouldBeCalled()->willReturn($returnValue);

        $this->assertInstanceOf(ObjectProphecy::class, $this->jwt);
        $this->assertInstanceOf(MethodProphecy::class, $this->jwt->getMethodProphecies('encode')[0]);
        $this->assertEquals($returnValue, $this->getSut()->encode());
    }

    public function testJwtDecodeCanBeProphecised(): void
    {
        $returnValue = new \stdClass();
        $this->jwt->decode('', '')->shouldBeCalled()->willReturn($returnValue);

        $this->assertInstanceOf(ObjectProphecy::class, $this->jwt);
        $this->assertInstanceOf(MethodProphecy::class, $this->jwt->getMethodProphecies('decode')[0]);
        $this->assertEquals($returnValue, $this->getSut()->decode());
    }

    public function testJwtSignCanBeProphecised(): void
    {
        $returnValue = 'string';
        $this->jwt->sign('', '')->shouldBeCalled()->willReturn($returnValue);

        $this->assertInstanceOf(ObjectProphecy::class, $this->jwt);
        $this->assertInstanceOf(MethodProphecy::class, $this->jwt->getMethodProphecies('sign')[0]);
        $this->assertEquals($returnValue, $this->getSut()->sign());
    }
}

composer