-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔨 Refactoring for mini-program. (#632)
* Refactoring for mini-program. * Fix styles. * Fix styles again 💣 * Modify name. * naming. * PrefixedContainer Trait.
- Loading branch information
Showing
8 changed files
with
183 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the overtrue/wechat. | ||
* | ||
* (c) overtrue <i@overtrue.me> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
/** | ||
* Trait PrefixedContainer.php. | ||
* | ||
* Part of Overtrue\WeChat. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @author mingyoung <mingyoungcheung@gmail.com> | ||
* @copyright 2017 | ||
* | ||
* @see https://github.com/overtrue | ||
* @see http://overtrue.me | ||
*/ | ||
|
||
namespace EasyWeChat\Support\Traits; | ||
|
||
use EasyWeChat\Support\Str; | ||
use Pimple\Container; | ||
|
||
/** | ||
* Trait PrefixedContainer. | ||
*/ | ||
trait PrefixedContainer | ||
{ | ||
/** | ||
* Container. | ||
* | ||
* @var \Pimple\Container | ||
*/ | ||
protected $container; | ||
|
||
/** | ||
* ContainerAccess constructor. | ||
* | ||
* @param \Pimple\Container $container | ||
*/ | ||
public function __construct(Container $container) | ||
{ | ||
$this->container = $container; | ||
} | ||
|
||
/** | ||
* Gets a parameter or an object from pimple container. | ||
* | ||
* @param string $key The unique identifier for the parameter or object | ||
* | ||
* @return mixed The value of the parameter or an object | ||
* | ||
* @throws \InvalidArgumentException if the identifier is not defined | ||
*/ | ||
public function __get($key) | ||
{ | ||
$className = (new \ReflectionClass($this))->getShortName(); | ||
|
||
$name = Str::snake($className).'.'.$key; | ||
|
||
return $this->container->offsetGet($name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
use EasyWeChat\MiniProgram\Server\Guard; | ||
use EasyWeChat\Support\XML; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
class MiniProgramServerGuardTest extends TestCase | ||
{ | ||
public function getServer($queries = [], $content = null) | ||
{ | ||
$queries = array_merge([ | ||
'signature' => 'b4d6a65981dd51a8d6d022364500e4430fe5e577', | ||
'timestamp' => '1490925589', | ||
'nonce' => '1854495049', | ||
], $queries); | ||
|
||
return new Guard('Mi5u8if', new Request($queries, [], [], [], [], [], $content)); | ||
} | ||
|
||
public function testValidateRequest() | ||
{ | ||
$result = $this->getServer(['echostr' => '3804283725124844375'])->serve(); | ||
|
||
$this->assertEquals('3804283725124844375', $result->getContent()); | ||
} | ||
|
||
public function testUserEnterSession() | ||
{ | ||
$json = json_encode([ | ||
'ToUserName' => 'toUser', | ||
'FromUserName' => 'fromUser', | ||
'CreateTime' => 1482048670, | ||
'MsgType' => 'event', | ||
'Event' => 'user_enter_tempsession', | ||
'SessionFrom' => 'sessionFrom', | ||
]); | ||
$xml = '<xml> | ||
<ToUserName><![CDATA[toUser]]></ToUserName> | ||
<FromUserName><![CDATA[fromUser]]></FromUserName> | ||
<CreateTime>1482048670</CreateTime> | ||
<MsgType><![CDATA[event]]></MsgType> | ||
<Event><![CDATA[user_enter_tempsession]]></Event> | ||
<SessionFrom><![CDATA[sessionFrom]]></SessionFrom> | ||
</xml>'; | ||
|
||
$server = $this->getServer([], $json); | ||
|
||
$res = $server->setMessageHandler(function ($message) { | ||
$this->assertEquals('user_enter_tempsession', $message->Event); | ||
})->serve(); | ||
$this->assertEquals('success', $res->getContent()); | ||
|
||
$server = $this->getServer([], $xml); | ||
|
||
$res2 = $server->setMessageHandler(function ($message) { | ||
$this->assertEquals('user_enter_tempsession', $message->Event); | ||
})->serve(); | ||
$this->assertEquals('success', $res2->getContent()); | ||
} | ||
} |