2
2
namespace Codeception \Module ;
3
3
4
4
use Codeception \Configuration ;
5
+ use Codeception \Exception \ConfigurationException ;
5
6
use Codeception \Exception \ModuleConfigException ;
6
7
use Codeception \Exception \ModuleException ;
7
8
use Codeception \Lib \Connector \Yii2 as Yii2Connector ;
@@ -356,10 +357,6 @@ public function _after(TestInterface $test)
356
357
$ this ->loadedFixtures = [];
357
358
}
358
359
359
- if ($ this ->client !== null && $ this ->client ->getApplication ()->has ('session ' , true )) {
360
- $ this ->client ->getApplication ()->session ->close ();
361
- }
362
-
363
360
$ this ->client ->resetApplication ();
364
361
parent ::_after ($ test );
365
362
}
@@ -406,17 +403,11 @@ public function _parts()
406
403
*/
407
404
public function amLoggedInAs ($ user )
408
405
{
409
- if (!$ this ->client ->getApplication ()->has ('user ' )) {
410
- throw new ModuleException ($ this , 'User component is not loaded ' );
406
+ try {
407
+ $ this ->client ->findAndLoginUser ($ user );
408
+ } catch (ConfigurationException $ e ) {
409
+ throw new ModuleException ($ this , $ e ->getMessage ());
411
410
}
412
- if ($ user instanceof \yii \web \IdentityInterface) {
413
- $ identity = $ user ;
414
- } else {
415
- // class name implementing IdentityInterface
416
- $ identityClass = $ this ->client ->getApplication ()->user ->identityClass ;
417
- $ identity = call_user_func ([$ identityClass , 'findIdentity ' ], $ user );
418
- }
419
- $ this ->client ->getApplication ()->user ->login ($ identity );
420
411
}
421
412
422
413
/**
@@ -659,10 +650,7 @@ public function amOnRoute($route, array $params = [])
659
650
*/
660
651
protected function clientRequest ($ method , $ uri , array $ parameters = [], array $ files = [], array $ server = [], $ content = null , $ changeHistory = true )
661
652
{
662
- if (is_array ($ uri )) {
663
- $ uri = $ this ->client ->getApplication ()->getUrlManager ()->createUrl ($ uri );
664
- }
665
- return parent ::clientRequest ($ method , $ uri , $ parameters , $ files , $ server , $ content , $ changeHistory );
653
+ return parent ::clientRequest ($ method , $ this ->client ->createUrl ($ uri ), $ parameters , $ files , $ server , $ content , $ changeHistory );
666
654
}
667
655
668
656
/**
@@ -676,13 +664,15 @@ protected function clientRequest($method, $uri, array $parameters = [], array $f
676
664
* @param $component
677
665
* @return mixed
678
666
* @throws ModuleException
667
+ * @deprecated in your tests you can use \Yii::$app directly.
679
668
*/
680
669
public function grabComponent ($ component )
681
670
{
682
- if (!$ this ->client ->getApplication ()->has ($ component )) {
683
- throw new ModuleException ($ this , "Component $ component is not available in current application " );
671
+ try {
672
+ return $ this ->client ->getComponent ($ component );
673
+ } catch (ConfigurationException $ e ) {
674
+ throw new ModuleException ($ this , $ e ->getMessage ());
684
675
}
685
- return $ this ->client ->getApplication ()->get ($ component );
686
676
}
687
677
688
678
/**
@@ -738,11 +728,11 @@ public function dontSeeEmailIsSent()
738
728
*/
739
729
public function grabSentEmails ()
740
730
{
741
- $ mailer = $ this ->grabComponent ('mailer ' );
742
- if (!$ mailer instanceof Yii2Connector \TestMailer) {
743
- throw new ModuleException ($ this , "Mailer module is not mocked, can't test emails " );
731
+ try {
732
+ return $ this ->client ->getEmails ();
733
+ } catch (ConfigurationException $ e ) {
734
+ throw new ModuleException ($ this , $ e ->getMessage ());
744
735
}
745
- return $ mailer ->getSentMessages ();
746
736
}
747
737
748
738
/**
@@ -763,33 +753,7 @@ public function grabLastSentEmail()
763
753
return end ($ messages );
764
754
}
765
755
766
- /**
767
- * Getting domain regex from rule host template
768
- *
769
- * @param string $template
770
- * @return string
771
- */
772
- private function getDomainRegex ($ template )
773
- {
774
- if (preg_match ('#https?://(.*)# ' , $ template , $ matches )) {
775
- $ template = $ matches [1 ];
776
- }
777
- $ parameters = [];
778
- if (strpos ($ template , '< ' ) !== false ) {
779
- $ template = preg_replace_callback (
780
- '/<(?:\w+):?([^>]+)?>/u ' ,
781
- function ($ matches ) use (&$ parameters ) {
782
- $ key = '# ' . count ($ parameters ) . '# ' ;
783
- $ parameters [$ key ] = isset ($ matches [1 ]) ? $ matches [1 ] : '\w+ ' ;
784
- return $ key ;
785
- },
786
- $ template
787
- );
788
- }
789
- $ template = preg_quote ($ template );
790
- $ template = strtr ($ template , $ parameters );
791
- return '/^ ' . $ template . '$/u ' ;
792
- }
756
+
793
757
794
758
/**
795
759
* Returns a list of regex patterns for recognized domain names
@@ -798,17 +762,7 @@ function ($matches) use (&$parameters) {
798
762
*/
799
763
public function getInternalDomains ()
800
764
{
801
- $ domains = [$ this ->getDomainRegex ($ this ->client ->getApplication ()->urlManager ->hostInfo )];
802
-
803
- if ($ this ->client ->getApplication ()->urlManager ->enablePrettyUrl ) {
804
- foreach ($ this ->client ->getApplication ()->urlManager ->rules as $ rule ) {
805
- /** @var \yii\web\UrlRule $rule */
806
- if (isset ($ rule ->host )) {
807
- $ domains [] = $ this ->getDomainRegex ($ rule ->host );
808
- }
809
- }
810
- }
811
- return array_unique ($ domains );
765
+ return $ this ->client ->getInternalDomains ();
812
766
}
813
767
814
768
private function defineConstants ()
@@ -826,11 +780,7 @@ private function defineConstants()
826
780
*/
827
781
public function setCookie ($ name , $ val , array $ params = [])
828
782
{
829
- // Sign the cookie.
830
- if ($ this ->client ->getApplication ()->request ->enableCookieValidation ) {
831
- $ val = $ this ->client ->getApplication ()->security ->hashData (serialize ([$ name , $ val ]), $ this ->client ->getApplication ()->request ->cookieValidationKey );
832
- }
833
- parent ::setCookie ($ name , $ val , $ params );
783
+ parent ::setCookie ($ name , $ this ->client ->hashCookieData ($ name , $ val ), $ params );
834
784
}
835
785
836
786
/**
@@ -840,8 +790,8 @@ public function setCookie($name, $val, array $params = [])
840
790
*/
841
791
public function createAndSetCsrfCookie ($ val )
842
792
{
843
- $ masked = $ this ->client ->getApplication ()-> security -> maskToken ($ val );
844
- $ name = $ this ->client ->getApplication ()-> request -> csrfParam ;
793
+ $ masked = $ this ->client ->maskToken ($ val );
794
+ $ name = $ this ->client ->getCsrfParamName () ;
845
795
$ this ->setCookie ($ name , $ val );
846
796
return [$ name , $ masked ];
847
797
}
0 commit comments