1+ <?php
2+
3+
4+ use Swaggest \JsonSchema \Context ;
5+ use Swaggest \JsonSchema \InvalidValue ;
6+ use Swaggest \JsonSchema \RemoteRef \Preloaded ;
7+ use Swaggest \JsonSchema \Schema ;
8+
9+ class AjvSchemasBench
10+ {
11+ private static $ cases ;
12+
13+ public function provide ()
14+ {
15+ foreach (self ::$ cases as $ name => $ tmp ) {
16+ yield $ name => ['name ' => $ name ];
17+ }
18+ }
19+
20+ /**
21+ * @ParamProviders({"provide"})
22+ */
23+ public function benchSpec ($ params )
24+ {
25+ $ case = self ::$ cases [$ params ['name ' ]];
26+
27+ $ actualValid = true ;
28+ try {
29+ $ options = $ this ->makeOptions (Schema::VERSION_DRAFT_07 );
30+ $ options ->schemasCache = self ::$ schemas ;
31+
32+ $ schema = Schema::import ($ case ['schema ' ], $ options );
33+
34+ $ options ->validateOnly = true ;
35+ $ schema ->in ($ case ['data ' ], $ options );
36+ } catch (InvalidValue $ exception ) {
37+ $ actualValid = false ;
38+ }
39+
40+ if ($ actualValid !== $ case ['isValid ' ]) {
41+ throw new Exception ('Assertion failed ' );
42+ }
43+ }
44+
45+ /** @var \SplObjectStorage */
46+ private static $ schemas ;
47+
48+ protected function makeOptions ($ version )
49+ {
50+ $ refProvider = static ::getProvider ();
51+
52+ $ options = new Context ();
53+ $ options ->setRemoteRefProvider ($ refProvider );
54+ $ options ->version = $ version ;
55+ $ options ->strictBase64Validation = true ;
56+
57+ return $ options ;
58+ }
59+
60+ public static function getProvider ()
61+ {
62+ static $ refProvider = null ;
63+
64+ if (null === $ refProvider ) {
65+ $ refProvider = new Preloaded ();
66+ $ refProvider
67+ ->setSchemaData (
68+ 'http://localhost:1234/integer.json ' ,
69+ json_decode (file_get_contents (__DIR__
70+ . '/../spec/JSON-Schema-Test-Suite/remotes/integer.json ' )))
71+ ->setSchemaData (
72+ 'http://localhost:1234/subSchemas.json ' ,
73+ json_decode (file_get_contents (__DIR__
74+ . '/../spec/JSON-Schema-Test-Suite/remotes/subSchemas.json ' )))
75+ ->setSchemaData (
76+ 'http://localhost:1234/name.json ' ,
77+ json_decode (file_get_contents (__DIR__
78+ . '/../spec/JSON-Schema-Test-Suite/remotes/name.json ' )))
79+ ->setSchemaData (
80+ 'http://localhost:1234/folder/folderInteger.json ' ,
81+ json_decode (file_get_contents (__DIR__
82+ . '/../spec/JSON-Schema-Test-Suite/remotes/folder/folderInteger.json ' )));
83+ }
84+
85+ return $ refProvider ;
86+ }
87+
88+ private static function provider ($ path )
89+ {
90+ $ testCases = array ();
91+
92+ if ($ handle = opendir ($ path )) {
93+ while (false !== ($ entry = readdir ($ handle ))) {
94+ if ($ entry != ". " && $ entry != ".. " ) {
95+ if ('.json ' !== substr ($ entry , -5 )) {
96+ continue ;
97+ }
98+ $ tests = json_decode (file_get_contents ($ path . '/ ' . $ entry ));
99+
100+ foreach ($ tests as $ test ) {
101+ foreach ($ test ->tests as $ c => $ case ) {
102+ $ name = $ entry . ' ' . $ test ->description . ': ' . $ case ->description . ' [ ' . $ c . '] ' ;
103+ if (!isset ($ test ->schema )) {
104+ if (isset ($ test ->schemas )) {
105+ foreach ($ test ->schemas as $ i => $ schema ) {
106+ $ testCases [$ name . '_ ' . $ i ] = array (
107+ 'schema ' => $ schema ,
108+ 'data ' => $ case ->data ,
109+ 'isValid ' => $ case ->valid ,
110+ 'name ' => $ name ,
111+ );
112+ }
113+ }
114+ continue ;
115+ }
116+ $ testCases [$ name ] = array (
117+ 'schema ' => $ test ->schema ,
118+ 'data ' => $ case ->data ,
119+ 'isValid ' => $ case ->valid ,
120+ 'name ' => $ name ,
121+ );
122+ }
123+ }
124+ }
125+ }
126+ closedir ($ handle );
127+ }
128+
129+ return $ testCases ;
130+ }
131+
132+ public static function init ()
133+ {
134+ self ::$ cases = self ::provider (__DIR__ . '/../spec/ajv/spec/tests/schemas ' );
135+ self ::$ schemas = new \SplObjectStorage ();
136+ }
137+ }
138+
139+ AjvSchemasBench::init ();
0 commit comments