66
77use Cycle \Database \DatabaseInterface ;
88use Cycle \Database \DatabaseProviderInterface ;
9+ use Cycle \Database \Query \SelectQuery ;
910use JsonException ;
1011use PHPUnit \Framework \Constraint \Constraint ;
1112use Throwable ;
1213
13- use function array_key_first ;
14- use function array_keys ;
1514use function json_encode ;
1615use function sprintf ;
1716
@@ -30,11 +29,9 @@ public function __construct(DatabaseProviderInterface $database, array $data)
3029 $ this ->database = $ database ->database ();
3130 }
3231
33- /**
34- * @param string $table
35- */
36- public function matches ($ table ): bool
32+ public function matches (mixed $ table ): bool
3733 {
34+ /** @var SelectQuery $tableInterface */
3835 $ tableInterface = $ this ->database ->table ($ table );
3936
4037 try {
@@ -52,10 +49,9 @@ public function matches($table): bool
5249 public function failureDescription (mixed $ table ): string
5350 {
5451 return sprintf (
55- " a row in the table [%s] matches the attributes %s. \n\n %s " ,
52+ ' a row in the table [%s] matches the attributes %s. ' ,
5653 $ table ,
57- $ this ->toString (JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE ),
58- $ this ->getAdditionalInfo ($ table )
54+ $ this ->toString (JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE )
5955 );
6056 }
6157
@@ -66,34 +62,4 @@ public function toString(int $options = 0): string
6662 {
6763 return json_encode ($ this ->data , JSON_THROW_ON_ERROR | $ options );
6864 }
69-
70- protected function getAdditionalInfo (string $ table ): string
71- {
72- $ query = $ this ->database ->table ($ table );
73-
74- $ similarResults = $ query ->where (
75- array_key_first ($ this ->data ),
76- $ this ->data [array_key_first ($ this ->data )]
77- )->select (array_keys ($ this ->data ))->limit ($ this ->show )->get ();
78-
79- if ($ similarResults ->isNotEmpty ()) {
80- $ description = 'Found similar results: ' . json_encode ($ similarResults , JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE );
81- } else {
82- $ query = $ this ->database ->table ($ table );
83-
84- $ results = $ query ->select (array_keys ($ this ->data ))->limit ($ this ->show )->get ();
85-
86- if ($ results ->isEmpty ()) {
87- return 'The table is empty ' ;
88- }
89-
90- $ description = 'Found: ' . json_encode ($ results , JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE );
91- }
92-
93- if ($ query ->count () > $ this ->show ) {
94- $ description .= sprintf (' and %s others ' , $ query ->count () - $ this ->show );
95- }
96-
97- return $ description ;
98- }
9965}
0 commit comments